Ani*_*nil 13 r r-markdown
如何从 RMarkdown (.Rmd) 文件中提取所有代码(块)并将它们转储到纯 R 脚本中?
基本上我想做这个问题中描述的补充操作,它使用块选项来提取 Rmd 的文本(即非代码)部分。
所以具体来说我想从一个 Rmd 文件开始,如下所示
---
title: "My RMarkdown Report"
author: "John Appleseed"
date: "19/02/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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>.
Some text description here.
```{r cars}
a = 1
print(a)
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Some more comments here.
Run Code Online (Sandbox Code Playgroud)
对于仅包含上述代码部分的 R 脚本,如下所示:
---
title: "My RMarkdown Report"
author: "John Appleseed"
date: "19/02/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
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>.
Some text description here.
```{r cars}
a = 1
print(a)
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Some more comments here.
Run Code Online (Sandbox Code Playgroud)
Wal*_*ldi 17
您可以使用knitr::purl,请参阅将 Markdown 转换为 R 脚本:
knitr::purl(input = "Report.Rmd", output = "Report.R",documentation = 0)
Run Code Online (Sandbox Code Playgroud)
给出Report.R:
knitr::opts_chunk$set(echo = TRUE)
a = 1
print(a)
summary(cars)
plot(pressure)
Run Code Online (Sandbox Code Playgroud)
另一种方法是将purl钩子设置在您的Rmd:
```{r setup, include=FALSE}
knitr::knit_hooks$set(purl = knitr::hook_purl)
knitr::opts_chunk$set(echo = TRUE)
```
Run Code Online (Sandbox Code Playgroud)
然后在编织时会生成 R 脚本。您可以使用 chunk 选项排除一些块purl = FALSE。