[英]Include R script with markdown tags as external file in Rmarkdown file
suppose I have an.R file tmp.R
with markdown tags, eg假设我有一个带有 markdown 标签的 .R 文件tmp.R
,例如
#' # header
5+1
and I want to include that into a (much larger) .Rmd
file as external source.我想将它包含在一个(更大的) .Rmd
文件中作为外部源。 R code and markdown tags shall be evaluated as if it were rendered directly. R 代码和 markdown 标签应像直接渲染一样进行评估。 How I would do that?我会怎么做?
---
title: "Untitled"
output: html_document
---
I played around with several options, including我玩了几个选项,包括
```{r, results='asis'}
source("tmp.R")
```
or或者
```{r, results='asis'}
knitr::spin("tmp.R')
```
and several others.和其他几个。 Unfortunately I didn't find a solution on stackoverflow, including this , this , this , this or this question.不幸的是,我没有在 stackoverflow 上找到解决方案,包括this 、 this 、 this 、 this或this question。
I'm not totally sure if I understand your question, but it sounds to me that you were looking for knitr::spin_child()
, which converts an R script to Rmd and knit it as a child document:我不完全确定我是否理解您的问题,但听起来您正在寻找knitr::spin_child()
,它将 R 脚本转换为 Rmd 并将其编织为子文档:
```{r}
knitr::spin_child('tmp.R')
```
You need to write the code in the temp.R
file, in such a way, that it can directly be evaluated in the chunk.您需要在temp.R
文件中编写代码,以便可以直接在块中对其进行评估。
So for tmp.R use:所以对于 tmp.R 使用:
cat("# header \n\n")
cat(4+3)
Then you can include this in the R-Markdown file with:然后您可以将其包含在 R-Markdown 文件中:
```{r, results='asis'}
source("tmp.R")
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.