According to section 11.1 of the R Markdown Cookbook, you can use variables from previous chunks as chunk options in R Markdown documents. The cookbook provides the following example:
---
output: html_document
---
```{r}
my_width <- 7
```
```{r, fig.width=my_width}
plot(cars)
```
Beautiful! ... when the output is an html document.
While the above works when output is set to html_document
, it does not when the output is set as output: html_notebook
in the front matter. That results in an RStudio error:
Error creating notebook: invalid quartz() device size. See line 9.
Why is there an error for html_notebook but not html_document?
Related questions on Posit Community
This unanswered question is about R Markdown documents rather than notebooks.
This question has me wondering whether chunk options are run before other code in R Notebooks. I would think that defining the variables in the setup chunk would solve this, but it does not.
cderv
August 16, 2023, 11:33am
2
Thanks for the report.
I think this would be an issue in RStudio IDE which handle the Notebook format. I believe this is covered by
opened 10:58AM - 16 Apr 20 UTC
bug
r markdown
Whilst the `eval` chunk option works fine the `knitr` they do not see to work as… expected interactively in Rstudio. Weirdly explicitly specifying a literal TRUE / FALSE works but using a variable that contains a TRUE / FALSE value does not.
---
params:
condition: FALSE
---
```{r eval = FALSE}
print("Hello World!") # Does NOT run on 'run all above'.
```
```{r eval = params$condition}
print("Hello World!") # DOES run on 'run all above'.
```
```{r}
# Hit run all above on me
```
Though it might be a peculiarity of `params` so tested with an ordinary variable:
```{r}
tmp <- FALSE
```
```{r, eval = tmp}
print("Hello World!") # DOES run on 'run all above'.
```
```{r}
# Hit run all above on me
```
### System details
RStudio Edition : Desktop
RStudio Version : 1.3.938
OS Version : Ubuntu 18.04.4
R Version : 3.6.3
---
- [x] I have read the guide to submitting good bug reports at https://github.com/rstudio/rstudio/wiki/Writing-Good-Bug-Reports .
- [x] I have installed the latest version of RStudio and confirmed that the issue still persists.
- [x] If I am reporting a RStudio crash, I have included a diagnostics report. https://support.rstudio.com/hc/en-us/articles/200321257-Running-a-Diagnostics-Report
- [x] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
and by
opened 03:23AM - 22 Oct 20 UTC
enhancement
r markdown
Currently, (at least some) Rmarkdown options set in a setup chunk [are not respe… cted when running chunks interactively](https://github.com/rstudio/rstudio/issues/7493#issuecomment-666909584). This necessitates setting chunk options in **every chunk**, which are respected when running interactively. This repetition is tedious and has greater potential for mistakes to be introduced.
There are two main reasons I think this should be added.
1. To provide consistency between running code interactively and knitting an entire document. It is hard(er) to compose a document and make full use of knitr's features/options when one must fully knit a document to use them. Frequent knitting is fine when the analysis runs quickly, but can easily become cumbersome (sure, caching helps here).
2. In support of [R Notebooks](https://blog.rstudio.com/2016/10/05/r-notebooks/), which seem to be a response in some ways to my point about frequent knitting being cumbersome. These are great, but they seem to rely heavily on existing Rmarkdown code and so this issue applies to them as well.
you should subscribed and add to those issue maybe.
system
Closed
September 30, 2023, 11:34am
3
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.