我的 Rmarkdown 文档看起来像这样。
---
yaml metadata
---
```{r}
x <- 10
```
Some code explanation
```{r}
y <- 10
```
Some more code explanation
```{r}
z <- x + y
```
The final output
```
# 10
```
Run Code Online (Sandbox Code Playgroud)
由于我遵循的是文学编程的概念,如何打印这些拼接在一起的多个代码块,所以我可以在没有代码解释的情况下将整个工作代码打印出来如下。另外,我可以选择特定的代码块而不是全部并将它们打印出来吗?
x <- 10
y <- 10
z <- x + y
Run Code Online (Sandbox Code Playgroud)
一个技巧是使用knitr的ref.label=""块选项(它需要一个或多个块标签)。它要求您标记您的块(至少是您想要重复的块)。为了演示,我已经“隐藏” ( echo=FALSE) 块之一以显示输出可以偏移(如在/sf/answers/2117015211/ 中),尽管它仍然就地执行。
---
output: md_document
---
```{r block1}
x <- 10
```
Some code explanation, next code block hidden here but still evaluated
```{r block2, echo = FALSE}
y <- 10
```
Some more code explanation
```{r block3}
z <- x + y
```
The final output
```
# 10
```
# Annex 1
Each block individually:
```{r showblock1, ref.label='block1', eval=FALSE}
```
```{r showblock2, ref.label='block2', eval=FALSE}
```
```{r showblock3, ref.label='block3', eval=FALSE}
```
# Annex 2
If you want them more compactly concatenated:
```{r showblocks, ref.label=c('block1','block2','block3'), eval=FALSE}
Run Code Online (Sandbox Code Playgroud)
渲染时生成此降价文件:
x <- 10
Some code explanation, next code block hidden here but still evaluated
Some more code explanation
z <- x + y
The final output
# 10
Annex 1
=======
Each block individually:
x <- 10
y <- 10
z <- x + y
Annex 2
=======
If you want them more compactly concatenated:
x <- 10
y <- 10
z <- x + y
Run Code Online (Sandbox Code Playgroud)
你可以渲染成你想要的任何格式,结果应该是相似的。
| 归档时间: |
|
| 查看次数: |
936 次 |
| 最近记录: |