rmarkDown参数覆盖全局列表
我在rmarkDown中使用params
。但是,当使用参数并确保一切都起作用时,我将参数重建为我的全局env中的列表,并通过块零件运行文档,以确保其创建正确的报告。我遇到了一个问题,即YAML中的参数在我的全球环境中覆盖了我的列表。如果您在不编织的情况下运行第一个块,它将创建值为6的参数列表。然后,如果您运行第二个块(Ctrl + Shift + Enter),它将运行整个块。但是它将用yaml中的默认值覆盖参数列表。但是,当我逐行运行IT时(CTRL + Enter)时,此问题不会发生。当我运行Ctrl +Shift +Enter时,该列表被覆盖时会发生什么?
这也发生在.qmd
文件中。
---
title: "test"
author: "Michael"
output: html_document
params:
id: "4"
---
```{r setup, include=TRUE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE
)
params <- list(id = 6)
```
```{r}
library(dplyr)
mtcars %>%
filter(cyl == params$id)
```
I am using params
in Rmarkdown. However when using params and making sure everything works, I reconstruct params as a list in my global env and run the document chunk by chunk to make sure it is creating the correct report. I ran across an issue where the params in the yaml was overwriting my list in my global environment. if you run the first chunk without knitting it will createa the params list with the value being 6. then if you run the second chunk (ctrl + shift +enter) it will run the whole chunk. BUT it will overwrite the params list with the default value in the yaml. However when I run it line by line (ctrl + enter) this issue does not happen. What is happening when I run ctrl +shift +enter that is make the list being overwritten?
This also happens in .qmd
files.
---
title: "test"
author: "Michael"
output: html_document
params:
id: "4"
---
```{r setup, include=TRUE}
knitr::opts_chunk$set(
echo = FALSE,
message = FALSE,
warning = FALSE
)
params <- list(id = 6)
```
```{r}
library(dplyr)
mtcars %>%
filter(cyl == params$id)
```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在交互式会话中运行
params&lt; - list(id = 6)
来解决这个问题(在RMD和QMD文件中)。因此,这对我有用:由于编织不是互动的,因此创建参数对象被忽略。
由于块选项还采用代码并进行评估,因此您甚至可以为这样的交互式会话运行特定的设置块:
在Sidenote上:我什至无法在没有条件的情况下编织文件,因为我会遇到此错误:
You can get around that issue (in both rmd and qmd files) by only running
params <- list(id = 6)
when you are in an interactive session. So this works for me:Since knitting is not interactive, creating the params object gets ignored.
Since chunk option also take code and evaluate it, you can even run a specific setup chunk for interactive sessions like this:
On a sidenote: I could not even knit the file without the condition, as I would run into this error:
可以使用
eval = false
(编织时未运行)的一个不同选项,以便可以使用yaml
id编织它,但是如果您想使用创建的参数您可以运行那行代码:output:
A different option could be using
eval=FALSE
(not runned when knitting) in your params chunk so you could knit it using theyaml
id, but if you want to use the created param you could run that line of code:Output: