r - 是否可以创建自渲染 Rmarkdown 文档?

标签 r r-markdown knitr executable shebang

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

#!/usr/bin/env Rscript

<some R code here>
运行 ./file.R将执行代码。
但是可以用 R-markdown 来做同样的事情吗?以便使用名为 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 session ,因此,在它之后写入的任何内容都将被忽略。这种效果也意味着编码的高度灵活性,因为您几乎可以在此之前编写任何有效的 R 代码 q("no") .上面的代码只是创建了另一个临时 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.

关于r - 是否可以创建自渲染 Rmarkdown 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64505450/

相关文章:

r - 选择表示两个位置之间范围的行,以便仅包括至少包含另一个表的一个位置的间隔

带 stringr 的正则表达式::如何查找模式的第一个实例

r - 渲染 rmarkdown 文档时保留辅助 TeX 文件

r - knitr::include_graphics in bookdown,不渲染图像

r - 在 rmarkdown 中隐藏代码

r - 如何在 Bookdown 中配置 Twitter 按钮?

R&quosures - 如何获取作为函数参数传递的向量中包含的符号名称?

regex - 为什么以及何时在 R 中使用可选的 perl 参数

r - 如何在 R 中执行词形还原?

r - 添加表格标题编号 R-Markdown