[英]Retrieve and execute example code from an R package function as a codeblock in R-markdown
I want to extract the example code from an R
package and run it in an rmarkdown
file automatically. 我想从R
包中提取示例代码并自动在rmarkdown
文件中运行它。
I am able to extract the code using the function utils::example
as follows. 我能够使用函数utils::example
提取代码,如下所示。
example("geom_histogram", package = "ggplot2", ask = F,
prompt.prefix = "", give.lines = TRUE)[-(1:5)]
I have tried to use chunk options results="asis"
as follows, but the result is given as code output rather than code chunk. 我尝试使用chunk options results="asis"
如下,但结果是作为代码输出而不是代码块给出的。
```{r,echo = FALSE, results="asis"}
cat("```{r}")
library(ggplot2)
cat(paste(example("geom_histogram", package = "ggplot2", ask = F,
prompt.prefix = "", give.lines = TRUE)[-(1:5)], collapse = "\n"))
cat("```")
```
I would like to have the code as a code block and the output from the same as in http://ggplot2.tidyverse.org/reference/geom_histogram.html . 我想将代码作为代码块,输出与http://ggplot2.tidyverse.org/reference/geom_histogram.html中的相同。 How to achieve this? 怎么做到这一点?
Updated answer: 更新的答案:
You can create a function to extract code and use it as a code
argument in chunk option. 您可以创建一个函数来提取代码并将其用作块选项中的code
参数。
# Function saved in functions.R file
getCode <- function(myFunction, myPackage) {
example(myFunction, myPackage, ask = FALSE, character.only = TRUE,
prompt.prefix = "", give.lines = TRUE)[-(1:5)]
}
Your Rmd
( myFile.Rmd
) should look like this: 你的Rmd
( myFile.Rmd
)应该是这样的:
```{r, meta, include = FALSE} myPackage <- "ggplot2" myFunction <- "geom_histogram" source("functions.R") ``` ```{r, intro, echo = FALSE, results = "asis"} cat("#", myPackage, "\\n") cat("##", myFunction, "\\n") library(myPackage, character.only = TRUE) ``` ```{r, runCode, code = getCode(myFunction, myPackage)} ```
Knit Rmd
with: knitr::knit2html("myFile.Rmd")
for a result like this: Knit Rmd
with: knitr::knit2html("myFile.Rmd")
,结果如下:
Previous answer: 上一个答案:
Write extracted code to a dummy file ( foo.R
) and use it as a code
argument in chunk option. 将提取的代码写入虚拟文件( foo.R
)并将其用作块选项中的code
参数。
Example file ( myFile.Rmd
): 示例文件( myFile.Rmd
):
```{r, meta, include = FALSE} library(ggplot2) ``` ```{r, getCode, include = FALSE} code <- example("geom_histogram", package = "ggplot2", ask = FALSE, prompt.prefix = "", give.lines = TRUE)[-(1:5)] write.table(code, "foo.R", quote = FALSE, row.names = FALSE, col.names = FALSE) ``` ```{r, runCode, code = readLines("foo.R")} ```
knit file with knitr::knit2html("myFile.Rmd")
for a result like this: 使用knitr::knit2html("myFile.Rmd")
编织文件,得到如下结果:
We can also remove hard-coded variables to have a more flexible output: 我们还可以删除硬编码变量以获得更灵活的输出:
```{r, meta, include = FALSE} myPackage <- "ggplot2" myFunction <- "geom_histogram" library(myPackage, character.only = TRUE) ``` ```{r, getCode, include = FALSE} code <- example(myFunction, myPackage, ask = FALSE, character.only = TRUE, prompt.prefix = "", give.lines = TRUE)[-(1:5)] write.table(code, "foo.R", quote = FALSE, row.names = FALSE, col.names = FALSE) ``` ```{r, intro, echo = FALSE, results = "asis"} cat("#", myPackage, "\\n") cat("##", myFunction, "\\n") ``` ```{r, runCode, code = readLines("foo.R")} ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.