在自定义引擎中匹配knitr文档环境

问题描述 投票:1回答:1

我有兴趣为knitr制作一个引擎,该引擎在将代码块发送给代码块进行评估之前,可以通过以下步骤对其进行预处理:

  1. 预处理options$code
  2. 在父环境中使用evaluate::evaluate()评估代码
  3. 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创建

r r-markdown knitr environment evaluate
1个回答
0
投票
事实证明,在{knitr}中有一个函数专门针对这种情况:knitr::knit_global(),它恰好是评估内联代码:knitr::knit_global()时的环境。>

https://github.com/yihui/knitr/blob/0daf31be36eed8d8ec0ba51eedee909283afc45d/R/hooks.R#L13

© www.soinside.com 2019 - 2024. All rights reserved.