我有兴趣为knitr制作一个引擎,该引擎在将代码块发送给代码块进行评估之前,可以通过以下步骤对其进行预处理:
options$code
evaluate::evaluate()
评估代码knitr::engine_output()
格式化输出我发现,在这些自定义块中创建的任何变量在文档的其余部分中均不可用。进行了一些修补之后,我发现如果我走了调用栈并找到了knitr的最后一个调用位置,我可以将envir
参数的值用作evaluate::evaluate()
的环境。但是,这感觉很hack。 在自定义块引擎中是否有更好的方法来匹配文档的环境?
将软盘添加到所有评论的引擎
knitr::knit_engines$set(flip =
function(options) {
# pre-process code
code <- gsub("(#+?)", "\\1 (╯°□°)╯︵", options$code)
# Find environment <--------------- IS THERE A BETTER WAY?
#
# grabbing the call stack
cstack <- vapply(sys.calls(), function(i) paste(as.character(deparse(i)), collapse = "\n"), character(1))
fstack <- sys.frames()
# Finding the last instance of the knit function and grabbing the envir variable
knitting <- rev(grep("knit(", cstack, fixed = TRUE))[1]
e <- get("envir", fstack[[knitting]])
OUT <- evaluate::evaluate(code, envir = e)
knitr::engine_output(options, out = OUT)
}
)
tmp <- tempfile(fileext = ".Rmd")
tmpout <- tempfile(fileext = ".md")
txt <- "---\noutput: md_document\n---\n\n```{r}\na <- 'A'\na\n```\n\n```{flip}\nb <- paste(a, 'and B') # FLIPPIN\nb\n```\n\nSponsored by the letters `r try(b)`\n"
cat(txt, file = tmp)
rmarkdown::render(tmp, output_file = tmpout, envir = new.env())
#> processing file: file3230dc4500b.Rmd
#> output file: file3230dc4500b.knit.md
#> /usr/bin/pandoc +RTS -K512m -RTS file3230dc4500b.utf8.md --to markdown_strict --from markdown+autolink_bare_uris+tex_math_single_backslash --output /tmp/Rtmpzc5qWO/file32306aeaf291.md --standalone
#>
#> Output created: /tmp/Rtmpzc5qWO/file32306aeaf291.md
cat(readLines(tmp), sep = "\n")
#> ---
#> output: md_document
#> ---
#>
#> ```{r}
#> a <- 'A'
#> a
#> ```
#>
#> ```{flip}
#> b <- paste(a, 'and B') # FLIPPIN
#> b
#> ```
#>
#> Sponsored by the letters `r try(b)`
cat(readLines(tmpout), sep = "\n")
#> a <- 'A'
#> a
#> #> [1] "A"
#>
#> b <- paste(a, 'and B') # (╯°□°)╯︵ FLIPPIN
#> b
#> #> [1] "A and B"
#>
#> Sponsored by the letters A and B
由reprex package(v0.3.0)在2020-06-16创建
knitr::knit_global()
,它恰好是评估内联代码:knitr::knit_global()
时的环境。>https://github.com/yihui/knitr/blob/0daf31be36eed8d8ec0ba51eedee909283afc45d/R/hooks.R#L13