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

R Markdown chunk option evaluated even though suppressed with the eval.after knit option #9407

Open
3 tasks done
wlandau opened this issue May 25, 2021 · 5 comments
Open
3 tasks done

Comments

@wlandau
Copy link

@wlandau wlandau commented May 25, 2021

System details

RStudio Edition : Desktop
RStudio Version : 1.4.869
OS Version      : Mac OS 10.15.7
R Version       : 4.0.2

Steps to reproduce the problem

I created the following minimal R Markdown report.

---
title: "test"
output: html_document
---

```{r setup}
eval_after <- knitr::opts_knit$get("eval.after")
knitr::opts_knit$set(eval.after = c(eval_after, "pattern"))
```

```{r, pattern = map(data), eval = FALSE}
1
```

When I click the green arrows in sequential order, the R console prints "Error in map(data) : could not find function "map".

Screen Shot 2021-05-25 at 10 32 21 AM

Describe the problem in detail

I am trying to create an R-Markdown interface for the targets R package: ropensci/targets#469. As part of this new feature, I need to suppress the automatic evaluation of certain custom knitr chunk options, including pattern and cue. I can do so when running the report using the Knit button in the IDE. However, when I click the green arrows for the chunks in the report above, the R console prints "Error in map(data) : could not find function "map", and the error interrupts the custom knitr language engines I am trying to define.

When I do a traceback, the error seems to come from.rs.scalarListFromList(). The function succeeds when expressions = FALSE. Is there anyway to make sure expressions = FALSE when I run these kinds of chunks interactively?

> .rs.scalarListFromList(list(pattern = quote(map(data))), expressions = FALSE)
> .rs.scalarListFromList(list(pattern = quote(map(data))), expressions = TRUE)
Error in map(data) : could not find function "map"

Describe the behavior you expected

The pattern chunk option should be treated as a language object and not evaluated as R code.

  • I have read the guide for submitting good bug reports.
  • I have installed the latest version of RStudio, and confirmed that the issue still persists.
  • [N/A] If I am reporting an RStudio crash, I have included a diagnostics report.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
@cderv
Copy link
Collaborator

@cderv cderv commented May 25, 2021

@wlandau just curious to know more: eval.after is used to evaluate an option after a chunk has been evaluated. In you case, it allows to suppress the evaluation of the chunk option because you set eval = FALSE. This means (1) evaluate pattern after code evaluation (2) but do not execute the chunk - side effect is to not evaluate pattern.

So, for the IDE to get the same as knitr behavior, it seems it would mean to respect eval = FALSE even interactively. This is in a way related to another issue #6675 (comment)

Try 'Run All' (CTRL + ALT + R), and you will get not error I think (because eval = FALSE is respected).

I just wanted to clarify so that this goes in the right category regarding better respect of knitr option to have better coherent behavior between knitting and interectively evaluating.

@wlandau
Copy link
Author

@wlandau wlandau commented May 25, 2021

eval.after is used to evaluate an option after a chunk has been evaluated. In you case, it allows to suppress the evaluation of the chunk option because you set eval = FALSE. This means (1) evaluate pattern after code evaluation (2) but do not execute the chunk - side effect is to not evaluate pattern.

Yes. I have custom chunk options pattern and cue (and potentially more) and my goal is to completely stop these options from being evaluated as R code in all cases. I think I achieved this outside the IDE: eval.after defers evaluation, and then the engine has full control over how the options are evaluated. From the targets source:

options$eval <- FALSE
knitr::engine_output(options = options, code = options$code, out = out)

But I do not know how to stop the IDE from evaluating these options.

Try 'Run All' (CTRL + ALT + R), and you will get not error I think (because eval = FALSE is respected).

In my case, I would actually prefer that users do not need to set eval = FALSE. Ideally, my custom engines should never evaluate these chunks, and eval should control whether to write the target to an R script for the pipeline. When I try "Run All" on the code below, map(data) is evaluated, which breaks the engine.

---
title: "test"
output: html_document
---

```{r setup}
eval_after <- knitr::opts_knit$get("eval.after")
knitr::opts_knit$set(eval.after = c(eval_after, "pattern", "cue"))
knitr::knit_engines$set(tar_target = function(options) {
  print(options$code)
  options$eval <- FALSE
  knitr::engine_output(options = options, code = options$code, out = "done")
})
```

```{tar_target x, pattern = map(data)}
code
```

@cderv
Copy link
Collaborator

@cderv cderv commented May 25, 2021

Oh I see ! The part about the engine having full control helped me understand. eval = FALSE has nothing to do with it then. Run All will indeed run chunk with eval = TRUE. Thanks for the additional information.

@ronblum ronblum added this to the Later milestone May 25, 2021
@cderv
Copy link
Collaborator

@cderv cderv commented May 26, 2021

Also related to #8149 I believe.

Trying to cross link every issue in this "better knitr option support" topic

@nickriches
Copy link

@nickriches nickriches commented Jul 28, 2022

I am also in a similar situation. I am using KnitR in RStudio. Blocks are still being evaluated, even when eval = FALSE has been specified. This is surely a fundamental issue with KnitR within RStudio, and I am surprised it has not been addressed.

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

4 participants