Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional chunk evaluation only works for 'run all above' with explict TRUE/FALSE #6675

Open
4 tasks done
RichardJActon opened this issue Apr 16, 2020 · 6 comments
Open
4 tasks done

Comments

@RichardJActon
Copy link

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

@ronblum
Copy link
Contributor

ronblum commented Apr 16, 2020

@RichardJActon Thank you for filing the issue! I can reproduce what you've described, but sometimes it ends up working; I haven't figured out the pattern, though. In addition to the described behavior, I see the following:

  • sometimes running from the last chunk doesn't work either
  • sometimes running from the second chunk runs the first chunk
  • sometimes running from the second chunk does not run the first chunk
  • sometimes when running from the second chunk and the first chunk doesn't run, an error message is sent to the Terminal pane (not Console):
print("Hello World!") # DOES run on 'run all above'.
Rons-Workhorse:~ ronblum$ print("Hello World!") # DOES run on 'run all above'.
bash: !": event not found

[The word sometimes will not appear any further.]
The second set of code works as expected. We'll review the bug as we continue development of RStudio.

Note: I tested this in RStudio Desktop 1.3.938 on MacOS 10.15.4 and RStudio Server on Ubuntu 18.0.4.

@stale
Copy link

stale bot commented Feb 5, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, per https://github.com/rstudio/rstudio/wiki/Issue-Grooming. Thank you for your contributions.

@stale stale bot added the stale Issues which have been closed automatically due to inactivitiy. label Feb 5, 2021
@mikebessuille mikebessuille added triage and removed stale Issues which have been closed automatically due to inactivitiy. labels Feb 5, 2021
@mikebessuille
Copy link
Contributor

This is an advanced feature request, where the IDE would have to evaluate the variables that are part of the chunk when deciding whether to run the chunk. Right now we don't run any of your R code when making that decision. This is out of scope.

@mikebessuille
Copy link
Contributor

After further discussion, we've changed our minds about this and will consider it in the future. There have been a large number of duplicate issues logged, and several upvotes, making this issue something we want to consider.

@apreshill
Copy link

Hi @mikebessuille & @ronblum -

Just a note that this workflow is documented in "R Markdown: The Definitive Guide" here as well:
https://bookdown.org/yihui/rmarkdown/params-use.html#params-use

---
params:
  printcode: false  # or set it to true
---

```{r, setup, include=FALSE}
# set this option in the first code chunk in the document
knitr::opts_chunk$set(echo = params$printcode)
```

@cderv
Copy link
Collaborator

cderv commented Mar 24, 2021

We have another example of this behavior in

This is confusing for users because something that works when you knit does not work the same interactively in the IDE.

As a side effect of this issue above, eval = is ignored and it seems it makes bash chunk behave as R chunks

---
title: "test"
output: html_document
params:
  run: FALSE
---

# eval = FALSE

* won't evaluate when knitted
* won't evaluate when running all chunks ('Run All', 'Run all of the above', ...)
* will evaluate with 'Run current Chunk' (green arrow)

```{r, eval=FALSE}
cat('successful')
```

# eval = params$run

* won't evaluate when knitted
* will evaluate when running all chunks ('Run All', 'Run all of the above', ...)
* will evaluate with 'Run current Chunk' (green arrow)

```{r, eval=params$run}
cat('successful')
```

# other engine 

* Will evaluate correctly as bash chunk

```{bash, eval=TRUE}
Rscript -e "cat('successful')"
```

* Will be evaluated as a R chunk leading to an error
```
#> Error: unexpected string constant in "Rscript -e "cat('successful')""
```

```{bash, eval=params$run}
Rscript -e "cat('successful')"
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants