[英]Include R script with markdown tags as external file in Rmarkdown file
假設我有一個帶有 markdown 標簽的 .R 文件tmp.R
,例如
#' # header
5+1
我想將它包含在一個(更大的) .Rmd
文件中作為外部源。 R 代碼和 markdown 標簽應像直接渲染一樣進行評估。 我會怎么做?
---
title: "Untitled"
output: html_document
---
我玩了幾個選項,包括
```{r, results='asis'}
source("tmp.R")
```
或者
```{r, results='asis'}
knitr::spin("tmp.R')
```
和其他幾個。 不幸的是,我沒有在 stackoverflow 上找到解決方案,包括this 、 this 、 this 、 this或this question。
我不完全確定我是否理解您的問題,但聽起來您正在尋找knitr::spin_child()
,它將 R 腳本轉換為 Rmd 並將其編織為子文檔:
```{r}
knitr::spin_child('tmp.R')
```
您需要在temp.R
文件中編寫代碼,以便可以直接在塊中對其進行評估。
所以對於 tmp.R 使用:
cat("# header \n\n")
cat(4+3)
然后您可以將其包含在 R-Markdown 文件中:
```{r, results='asis'}
source("tmp.R")
```
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.