如何判断代码是否在knitr/rmarkdown上下文中执行?

时间:2022-10-21 06:12:38

Based on some simple tests, interactive() is true when running code within rmarkdown::render() or knitr::knit2html(). That is, a simple .rmd file containing

基于一些简单的测试,当在rmarkdown:::render()或knitr:::knit2html()中运行代码时,interactive()是正确的。也就是说,一个简单的.rmd文件包含

```{r}
print(interactive())
```

gives an HTML file that reports TRUE.

给出一个报告为真的HTML文件。

Does anyone know of a test I can run within a code chunk that will determine whether it is being run "non-interactively", by which I mean "within knit2html() or render()"?

是否有人知道我可以在一个代码块中运行一个测试,该代码块将决定它是“非交互式”运行,我的意思是“在knit2html()或render()中运行”?

3 个解决方案

#1


14  

As Yihui suggested on github isTRUE(getOption('knitr.in.progress')) can be used to detect whether code is being knitted or executed interactively.

正如Yihui在github isTRUE(getOption,“knit .in.progress”)上所建议的,可以用来检测代码是被编织还是被交互执行。

#2


3  

A simpler suggestion for rolling your own: see if you can access current chunk options.

一个更简单的建议是:查看是否可以访问当前的块选项。

```{r, echo = FALSE}
inside_knitr = function() {
    length(knitr::opts_current$get()) > 0
}
```

```{r}
inside_knitr()
```

There are, of course, many things you could check. I like the idea of the current chunk options, another possibility is below. I'm not really sure about the pros/cons of either.

当然,你可以检查很多东西。我喜欢当前区块选项的想法,另一种可能性是在下面。我也不太清楚两者的利弊。

```{r}
!is.null(knitr::opts_knit$get("out.format"))
```

#3


2  

I suspect (?) you might just need to roll your own.

我想你可能只需要自己动手。

If so, here's one approach which seems to perform just fine. It works by extracting the names of all of the functions in the call stack, and then checks whether any of them are named "knit2html" or "render". (Depending on how robust you need this to be, you could do some additional checking to make sure that these are really the functions in the knitr and rmarkdown packages, but the general idea would still be the same.)

如果是这样,这里有一种方法似乎运行良好。它通过提取调用堆栈中所有函数的名称来工作,然后检查它们中的任何一个是否被命名为“knit2html”或“render”。(根据您需要的健壮程度,您可以进行一些额外的检查,以确保这些功能实际上是knitr和rmarkdown包中的功能,但总体思路仍然是相同的。)

```{r, echo=FALSE}
isNonInteractive <- function() {
    ff <- sapply(sys.calls(), function(f) as.character(f[[1]]))
    any(ff %in% c("knit2html", "render"))
}
```

```{r}
print(isNonInteractive())
```

#1


14  

As Yihui suggested on github isTRUE(getOption('knitr.in.progress')) can be used to detect whether code is being knitted or executed interactively.

正如Yihui在github isTRUE(getOption,“knit .in.progress”)上所建议的,可以用来检测代码是被编织还是被交互执行。

#2


3  

A simpler suggestion for rolling your own: see if you can access current chunk options.

一个更简单的建议是:查看是否可以访问当前的块选项。

```{r, echo = FALSE}
inside_knitr = function() {
    length(knitr::opts_current$get()) > 0
}
```

```{r}
inside_knitr()
```

There are, of course, many things you could check. I like the idea of the current chunk options, another possibility is below. I'm not really sure about the pros/cons of either.

当然,你可以检查很多东西。我喜欢当前区块选项的想法,另一种可能性是在下面。我也不太清楚两者的利弊。

```{r}
!is.null(knitr::opts_knit$get("out.format"))
```

#3


2  

I suspect (?) you might just need to roll your own.

我想你可能只需要自己动手。

If so, here's one approach which seems to perform just fine. It works by extracting the names of all of the functions in the call stack, and then checks whether any of them are named "knit2html" or "render". (Depending on how robust you need this to be, you could do some additional checking to make sure that these are really the functions in the knitr and rmarkdown packages, but the general idea would still be the same.)

如果是这样,这里有一种方法似乎运行良好。它通过提取调用堆栈中所有函数的名称来工作,然后检查它们中的任何一个是否被命名为“knit2html”或“render”。(根据您需要的健壮程度,您可以进行一些额外的检查,以确保这些功能实际上是knitr和rmarkdown包中的功能,但总体思路仍然是相同的。)

```{r, echo=FALSE}
isNonInteractive <- function() {
    ff <- sapply(sys.calls(), function(f) as.character(f[[1]]))
    any(ff %in% c("knit2html", "render"))
}
```

```{r}
print(isNonInteractive())
```