防止 R Notebook 交互式中的块评估 [英] Prevent chunk evaluation in R Notebook interactive

查看:69
本文介绍了防止 R Notebook 交互式中的块评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用块选项 eval=FALSE 可以在 RMarkdown 文件或 R Notebook 编织时抑制块评估.有没有办法在 RStudio 中交互式运行文档期间使其适用(即,使运行所有块"跳过某些块)?

Using the chunk option eval=FALSE one can suppress chunk evaluation in an RMarkdown file or R Notebook when it is knit. Is there a way to make this apply during interactive running of the document in RStudio (i.e., making "run all chunks" skip certain chunks)?

我的分析开始时有一些块需要一段时间才能运行,后面的部分不依赖于这些块.我希望能够获取代码的重要部分,这样我就可以继续编写下游的内容,而不必逐块手动编写,这样我就可以避免工作区中不需要的部分以供进一步编写.

I've got some chunks in the beginning of my analysis that take a while to run, which the later sections don't depend on. I want to be able to source the important parts of the code so I can continue writing the downstream stuff, without having to do it manually chunk-by-chunk so I can avoid the parts I don't need in my workspace for further writing.

我已经使用逻辑参数设置了 rmarkdown 文档,用于更改需要运行代码的哪些部分 - 我的意思是这些作为代码实际完成和使用时的控制标志,但我希望我可以使用这些相同的参数来排除块以交互模式运行(即,类似于 eval=params$run_part1).

I've set up the rmarkdown document with logical parameters meant to change which parts of the code need to get run - I meant these as control flags for when the code is actually finished and getting used, but I was hoping I could use those same parameters to exclude chunks from running in interactive mode (i.e., something like eval=params$run_part1).

推荐答案

设置 knitr::opts_chunkknitr::opts_hooks 只在编织时帮助你,而不是交互模式,所以虽然我可能是错的,但我会暂时说你不能用动态块选项控制这种行为(还).

Setting knitr::opts_chunk and knitr::opts_hooks only help you when knitting, not in interactive mode, so while I could be wrong I'm going to tentatively say you can't control that behavior with dynamic chunk options (yet).

作为一种解决方法,您可以使用 interactive()if 块,以便代码仅在编织时运行.它也可以很好地与您的逻辑参数相吻合,尽管必须在括号块中感到痛苦.

As a workaround you could use interactive() and if blocks so that the code is only run when knitted. It would also mesh well with your logical parameters, despite the pain of having to be in a bracket block.

---
title: "R Notebook"
output:
  html_document: default
  html_notebook: default
---

```{r}
if (!interactive()) {
  print("long running code")
}
```

```{r}
print(2)
```

```{r}
print(3)
```

按在上面运行所有块":

Pressing "Run All Chunks Above":

针织:

这篇关于防止 R Notebook 交互式中的块评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆