RMarkdown Chunk - 不要回显最后一个表达式
RMarkdown Chunk - Don't echo last expression
我在 RMarkdown 文档中有一个块,如下所示:
```{r, echo=-4}
a <- 1
b <- 2
x <- a + b
print(paste(c("`x` is equal to ", x), collapse=""))
```
当我编织这个时,它没有显示第 4 个表达式,正如预期和记录的那样 here。
a <- 1
b <- 2
x <- a + b
## [1] "`x` is equal to 3"
但是,如果我在块中添加另一行,我必须更新 echo
参数,否则它会隐藏错误的表达式:
```{r, echo=-4}
a <- 1
b <- 2
c <- 3
x <- a + b + c
print(paste(c("`x` is equal to ", x), collapse=""))
```
输出:
a <- 1
b <- 2
c <- 3
print(paste(c("`x' is equal to ", x), collapse=""))
## [1] "`x` is equal to 6"
RMarkdown 块中是否有编程方式指定您不想在块中回显 最后一个表达式 而无需手动计算总行数?
不,没有办法做到这一点。如果您查看 knitr
源代码,您会看到它使用
获取代码行的子集
code = code[options$echo]
所以它对 select 个条目使用常规的 R 索引。 R 无法在常量值或值向量中表示 "last element",因此 rmarkdown
和 knitr
无法表示 "last expression"。使用@atiretoo 的建议(将它放在一个单独的块中)似乎是你最好的选择。
我喜欢
```{r}
a <- 1
b <- 2
x <- a + b
```
`x` is equal to `r x`
结果输出更清晰。
这里有一个技巧:使用 knitr
hooks 标记要从输出中删除的行,并在打印输出时将其删除。
以下解决方案实现了一个名为 rm.last
的新块选项,它删除了源代码的最后 n
行。
knitr
有一个 built-in 源代码块,因此您必须使用 knitr::knit_hooks$get('source')
.
检索它
---
title: "Hack the output with hooks"
---
```{r setup, include=FALSE}
knitr::opts_hooks$set(rm.last = function(options) {
options$code <-
paste0(
options$code,
c(rep("", length(options$code) - options$rm.last),
rep(" # REMOVE", options$rm.last)
)
)
options
})
builtin_source_hook <- knitr::knit_hooks$get('source')
knitr::knit_hooks$set(source = function(x, options) {
if (!is.null(options$rm.last))
x <- grep("# REMOVE$", x, invert = TRUE, value = TRUE)
if (length(x) > 0) {
return(builtin_source_hook(x, options))
} else {
invisible(NULL)
}
})
```
```{r, rm.last=1}
a <- 1
b <- 2
x <- a + b
print(paste(c("`x` is equal to ", x), collapse=""))
```
但是,如果你的最后一行代码是print(...)
,你显然必须遵循@atiretoo 的回答。
我在 RMarkdown 文档中有一个块,如下所示:
```{r, echo=-4}
a <- 1
b <- 2
x <- a + b
print(paste(c("`x` is equal to ", x), collapse=""))
```
当我编织这个时,它没有显示第 4 个表达式,正如预期和记录的那样 here。
a <- 1
b <- 2
x <- a + b
## [1] "`x` is equal to 3"
但是,如果我在块中添加另一行,我必须更新 echo
参数,否则它会隐藏错误的表达式:
```{r, echo=-4}
a <- 1
b <- 2
c <- 3
x <- a + b + c
print(paste(c("`x` is equal to ", x), collapse=""))
```
输出:
a <- 1
b <- 2
c <- 3
print(paste(c("`x' is equal to ", x), collapse=""))
## [1] "`x` is equal to 6"
RMarkdown 块中是否有编程方式指定您不想在块中回显 最后一个表达式 而无需手动计算总行数?
不,没有办法做到这一点。如果您查看 knitr
源代码,您会看到它使用
code = code[options$echo]
所以它对 select 个条目使用常规的 R 索引。 R 无法在常量值或值向量中表示 "last element",因此 rmarkdown
和 knitr
无法表示 "last expression"。使用@atiretoo 的建议(将它放在一个单独的块中)似乎是你最好的选择。
我喜欢
```{r}
a <- 1
b <- 2
x <- a + b
```
`x` is equal to `r x`
结果输出更清晰。
这里有一个技巧:使用 knitr
hooks 标记要从输出中删除的行,并在打印输出时将其删除。
以下解决方案实现了一个名为 rm.last
的新块选项,它删除了源代码的最后 n
行。
knitr
有一个 built-in 源代码块,因此您必须使用 knitr::knit_hooks$get('source')
.
---
title: "Hack the output with hooks"
---
```{r setup, include=FALSE}
knitr::opts_hooks$set(rm.last = function(options) {
options$code <-
paste0(
options$code,
c(rep("", length(options$code) - options$rm.last),
rep(" # REMOVE", options$rm.last)
)
)
options
})
builtin_source_hook <- knitr::knit_hooks$get('source')
knitr::knit_hooks$set(source = function(x, options) {
if (!is.null(options$rm.last))
x <- grep("# REMOVE$", x, invert = TRUE, value = TRUE)
if (length(x) > 0) {
return(builtin_source_hook(x, options))
} else {
invisible(NULL)
}
})
```
```{r, rm.last=1}
a <- 1
b <- 2
x <- a + b
print(paste(c("`x` is equal to ", x), collapse=""))
```
但是,如果你的最后一行代码是print(...)
,你显然必须遵循@atiretoo 的回答。