rmarkDown参数覆盖全局列表

发布于 2025-02-01 17:02:46 字数 660 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风情万种。 2025-02-08 17:02:46

您可以通过在交互式会话中运行params&lt; - list(id = 6)来解决这个问题(在RMD和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
 )
 if (interactive()) params <- list(id = 6)
 ```
 
 ```{r}
 library(dplyr)
 mtcars %>% 
     filter(cyl == params$id)
 ```

由于编织不是互动的,因此创建参数对象被忽略。

由于块选项还采用代码并进行评估,因此您甚至可以为这样的交互式会话运行特定的设置块:

```{r setup, include=TRUE, eval=interactive()}

在Sidenote上:我什至无法在没有条件的情况下编织文件,因为我会遇到此错误:

Quitting from lines 10-16 (test.qmd) 
Error in eval(expr, envir, enclos) : 
  cannot change value of locked binding for 'params'

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:

 ---
 title: "test"
 author: "Michael"
 output: html_document
 params:
  id: "4"
 ---
 
 ```{r setup, include=TRUE}
 knitr::opts_chunk$set(
     echo = FALSE,
     message = FALSE,
     warning = FALSE
 )
 if (interactive()) params <- list(id = 6)
 ```
 
 ```{r}
 library(dplyr)
 mtcars %>% 
     filter(cyl == params$id)
 ```

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:

```{r setup, include=TRUE, eval=interactive()}

On a sidenote: I could not even knit the file without the condition, as I would run into this error:

Quitting from lines 10-16 (test.qmd) 
Error in eval(expr, envir, enclos) : 
  cannot change value of locked binding for 'params'
梦里泪两行 2025-02-08 17:02:46

可以使用eval = false(编织时未运行)的一个不同选项,以便可以使用yaml id编织它,但是如果您想使用创建的参数您可以运行那行代码:

---
title: "test"
author: "Michael"
output: html_document
params:
 id: "4"
---

```{r setup, include=TRUE, eval=FALSE}
knitr::opts_chunk$set(
    echo = FALSE,
    message = FALSE,
    warning = FALSE
)
params <- list(id = 6)

```

```{r}
library(dplyr)
mtcars %>% 
    filter(cyl == params$id)
```

output:

A different option could be using eval=FALSE (not runned when knitting) in your params chunk so you could knit it using the yaml id, but if you want to use the created param you could run that line of code:

---
title: "test"
author: "Michael"
output: html_document
params:
 id: "4"
---

```{r setup, include=TRUE, eval=FALSE}
knitr::opts_chunk$set(
    echo = FALSE,
    message = FALSE,
    warning = FALSE
)
params <- list(id = 6)

```

```{r}
library(dplyr)
mtcars %>% 
    filter(cyl == params$id)
```

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文