繁体   English   中英

使用knitr / rmarkdown将R脚本集合编译为HTML

[英]Compile a collection of R scripts to HTML with knitr/rmarkdown

我在目录中有一组R脚本,想要将它们全部编译为HTML,就像我可以手动使用R Studio Compile Notebook一样,但是可以控制CSS样式。

每个文件都有一个最小的YAML标头和一些标记,如下所示,我不想在带有块选项的脚本中引入额外的复杂性,除非绝对必要。

#' ---
#' title: "Tests for association in two-way tables"
#' author: "Michael Friendly"
#' ---

#' ## Load and examine data
library(vcdExtra)
data(SexualFun)
SexualFun                # show the table
...

我对于使用哪种工具以这种方式编译单个文件感到困惑spin | knit2htmlmarkdownToHTML等,一旦我得到这个权利,我可以使用类似于apply()这样做对我的文件,列表files <- list.files(pattern="*.R$")

这是我尝试通过一个附加控件来编译一个文件的方法:

library(knitr)
options(markdown.HTML.options = markdownHTMLOptions(stylesheet = "markdown.css"))
knit_theme$set("seashell")
opts_chunk$set(fig.align="center")
spinone <- function(file, outdir="output/") {
    fn <- knitr:::sans_ext(file)
    #+ suppressmessages, include=FALSE
    knitr::opts_chunk$set(warning = FALSE, message = FALSE)
    spin(file)
    Rmd <- knitr:::sub_ext(file, "Rmd")
    out <- paste0(outdir, knitr:::sub_ext(file, "html"))
    knit2html(Rmd, output=out)
}

但是它不能正常工作。 我得到:

> spinone("sexfun-chisq.R")
processing file: sexfun-chisq.Rmd
  ...
  |.................................................................| 100%
  ordinary text without R code

output file: sexfun-chisq.md

Error in readLines(if (is.character(input2)) { : 
  cannot open the connection
In addition: Warning message:
In readLines(if (is.character(input2)) { :
  cannot open file 'sexfun-chisq.Rmd': No such file or directory

实际上,产生了sexfun-chisq.html ,但是它没有YAML标头,也不使用我的样式选项。

使用spin(file, knit = FALSE)以便不会删除Rmd文件(请参阅?spinprecious参数)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM