R Markdown:在 R 中创建部分 headers 并在同一部分打印 R 输出
R Markdown: Create section headers in R and print R output in same section
我在 R 中有一个 for
循环,我想在其中创建 R Markdown 部分 headers 并显示(即 print
)R 函数的输出。如果我在代码块的开头使用 {r results='asis'}
,我 可以 使部分 headers 工作,但 R 输出 不会 显示正常。如果我不使用 results='asis'
,那么会显示 R 输出,但 headers 部分将不起作用。如何获得 R-created 部分 headers 和 R 输出是相同的代码块?
下面是一个简短的 R Markdown 脚本来演示该问题。 (它仅使用矩阵的显示作为各种 R 函数的占位符,例如 summary(lm(...))
;我对显示矩阵并不特别感兴趣。)knitr
-ed 输出的图像跟随脚本。
---
title: "R Markdown Test"
output:
html_document:
number_sections: yes
theme: default
toc: yes
toc_depth: 4
toc_float: no
code_folding: hide
---
# With*out* `results='asis'`
```{r}
for ( i in 1:2 ) {
cat("\n## Subsection",i,"\n")
# knitr::asis_output( cat("\n## Subsection",i,"\n") ) # does not work
print( matrix( i*(1:6), nrow=2 ) )
}
```
# With `results='asis'`
```{r results='asis'}
for ( i in 1:2 ) {
cat("\n## Subsection",i,"\n")
print( matrix( i*(1:6), nrow=2 ) )
}
```
输出如下所示:
感谢您的帮助!
一个response to my question at RStudio Community pointed me to the topic of child documents。通过遵循子文档的示例,我能够为我的问题创建一个解决方案。我很抱歉该解决方案并不优雅(IMO),但至少它有效。
对于更详细的循环内容,人们可能希望将循环内容放入一个单独的文本文件中,如前面链接的示例所示。 编辑:我在这个答案的末尾附加了一个使用单独的子文档的示例。
以下是使用子文档的解决方案。这是附加到我原来的代码中的代码 post,因此该部分在输出中编号为 3。
# Using child documents
Adapted from https://bookdown.org/yihui/rmarkdown-cookbook/child-document.html#child-document
```{r results='asis'}
res <- lapply( 1:2 , function(i) {
knitr::knit_child( text=c(
'## Subsection `r i`',
'',
'```{r}',
'print( matrix(i*(1:6),nrow=2) )',
'```',
''
) , envir=environment() , quiet=TRUE )
})
cat( unlist(res) , sep='\n' )
```
结果如下所示:
感谢 Christophe Dervieux 的指点。
编辑:下图显示了使用单独子文档的示例:
我在 R 中有一个 for
循环,我想在其中创建 R Markdown 部分 headers 并显示(即 print
)R 函数的输出。如果我在代码块的开头使用 {r results='asis'}
,我 可以 使部分 headers 工作,但 R 输出 不会 显示正常。如果我不使用 results='asis'
,那么会显示 R 输出,但 headers 部分将不起作用。如何获得 R-created 部分 headers 和 R 输出是相同的代码块?
下面是一个简短的 R Markdown 脚本来演示该问题。 (它仅使用矩阵的显示作为各种 R 函数的占位符,例如 summary(lm(...))
;我对显示矩阵并不特别感兴趣。)knitr
-ed 输出的图像跟随脚本。
---
title: "R Markdown Test"
output:
html_document:
number_sections: yes
theme: default
toc: yes
toc_depth: 4
toc_float: no
code_folding: hide
---
# With*out* `results='asis'`
```{r}
for ( i in 1:2 ) {
cat("\n## Subsection",i,"\n")
# knitr::asis_output( cat("\n## Subsection",i,"\n") ) # does not work
print( matrix( i*(1:6), nrow=2 ) )
}
```
# With `results='asis'`
```{r results='asis'}
for ( i in 1:2 ) {
cat("\n## Subsection",i,"\n")
print( matrix( i*(1:6), nrow=2 ) )
}
```
输出如下所示:
感谢您的帮助!
一个response to my question at RStudio Community pointed me to the topic of child documents。通过遵循子文档的示例,我能够为我的问题创建一个解决方案。我很抱歉该解决方案并不优雅(IMO),但至少它有效。
对于更详细的循环内容,人们可能希望将循环内容放入一个单独的文本文件中,如前面链接的示例所示。 编辑:我在这个答案的末尾附加了一个使用单独的子文档的示例。
以下是使用子文档的解决方案。这是附加到我原来的代码中的代码 post,因此该部分在输出中编号为 3。
# Using child documents
Adapted from https://bookdown.org/yihui/rmarkdown-cookbook/child-document.html#child-document
```{r results='asis'}
res <- lapply( 1:2 , function(i) {
knitr::knit_child( text=c(
'## Subsection `r i`',
'',
'```{r}',
'print( matrix(i*(1:6),nrow=2) )',
'```',
''
) , envir=environment() , quiet=TRUE )
})
cat( unlist(res) , sep='\n' )
```
结果如下所示:
感谢 Christophe Dervieux 的指点。
编辑:下图显示了使用单独子文档的示例: