: knitr hooks, , , .
chunk rm.last, n .
knitr , 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.