r-是否可以创建一个自呈现的Rmarkdown文档



对于典型的R脚本,可以使用shebang语法在中运行代码。使用文件file.R

#!/usr/bin/env Rscript
<some R code here>

运行./file.R将执行代码。

但R降价也能做到这一点吗?因此,使用名为file.Rmd:的文件

#!/usr/bin/env <some command>
<some markdown>
<some R code here>
<some markdown again>

运行./file.Rmd会产生file.md

您可以将Rmd文件视为Rscript。例如,假设你的Rmd文件看起来像这个

---
title: "Untitled"
author: "ekoam"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

然后,您可以在Rmd文件中准备以下代码

#!/usr/bin/env Rscript
args <- commandArgs()
fname <- normalizePath(sub("--file=", "", args[grepl("--file=", args)]))
thisfile <- readLines(fname)
newfname <- paste0(tempdir(), "/", basename(fname))
writeLines(thisfile[-1:-which(thisfile == "q("no")")], newfname)
rmarkdown::render(newfname, output_dir = dirname(fname))
q("no")

这里的诀窍是q("no")。这一行终止了R会话,因此,在它之后写的任何内容都将被忽略。这样的效果也意味着编码的高度灵活性,因为在q("no")之前几乎可以编写任何有效的R代码。上面的代码只是创建另一个临时Rmd文件,其内容与q("no")之后的内容相同。然后,我们rmarkdown::render那个临时文件,并将输出转储到当前目录。

完整的Rmd文件看起来像这个

#!/usr/bin/env Rscript
args <- commandArgs()
fname <- normalizePath(sub("--file=", "", args[grepl("--file=", args)]))
thisfile <- readLines(fname)
newfname <- paste0(tempdir(), "/", basename(fname))
writeLines(thisfile[-1:-which(thisfile == "q("no")")], newfname)
rmarkdown::render(newfname, output_dir = dirname(fname))
q("no")
---
title: "Untitled"
author: "ekoam"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

免责声明:我对Linux了解不多,也没有安装R的Linux机器来测试我的答案。如果这不正常,请告诉我。如果这能激励某人写出更好的答案,我很乐意删除这一条。


让我向后看:

  • 要编织文档,我们需要调用rmarkdown::render()
  • 要从命令行运行此命令,我们可以使用RScript -e "rmarkdown::render('document.Rmd')",例如,请参阅此处
  • 可能您不想硬编码到Rscript的路径,而是使用类似#!/usr/bin/env Rscript的shebang
  • 这种方法的问题在于,它不允许将参数传递给Rscript
  • 因此,我建议使用";包装器";在负责调用CCD_ 15的shebang中
  • 以RMD文件的名称为参数,所需的R代码变为rmarkdown::render(commandArgs(trailingOnly=TRUE)[1])

逐步:

  1. 创建包装程序(在$PATH中的任何目录中(并使其可执行:

    touch KnitWrapper.sh
    chmod +x KnitWrapper.sh
    
  2. 将以下内容写入KnitWrapper.sh:

    #!/usr/bin/env bash
    Rscript -e "rmarkdown::render(commandArgs(trailingOnly=TRUE)[1])" $1
    
  3. 确保Rscript在您的$PATH中。如果您更新R,请相应地更新您的$PATH

  4. 在RMD文件中,添加shebang#!/usr/bin/env KnitWrapper.sh

  5. 使您的RMD文件可执行并运行它:

    chmod +x yourDocument.Rmd
    ./yourDocument.Rmd
    

不幸的是,上述内容大多未经测试。如果它不起作用,请告诉我,这样我就可以修复或删除这个答案。

最新更新