2

I am writing a custom knitr language engine to support https://github.com/ropensci/targets/discussions/469, and I would like users to be able to write R Markdown code chunks like this:


```{tar_target analysis, pattern = map(data)}
run_analysis(data)
```

I want to avoid evaluating the pattern argument because map(data) is a DSL in tar_target() and not actual R code. But when I run the chunk, I get a "could not find function 'map'" error before control passes to the engine. Is it possible for me as the developer to call substitute() on the necessary options before knitr tries to evaluate them?

EDIT: thinking about supporting a wrapper to control special arguments to tar_target():


```{tar_target analysis, tar_args = tar_args(pattern = map(data), error = "workspace")}
run_analysis(data)
```
8
  • 1
    You can use matching code fences with more than three backticks around your RMarkdown code chunk. May 25, 2021 at 11:43
  • 1
    I’ve only had a cursory glance at the issue, would it be possible to export map (and maybe other functions) which, when invoked, return an unevaluated expression, similar to what ~ does? That way you wouldn’t prevent knitr evaluating the arguments but the outcome would be the same: you can still access the unevaluated version. Of course the challenge then is to expose these functions only to knitr, not to user code. May 25, 2021 at 11:45
  • Thanks for the input. What do you think about the tar_args() wrapper in the edit to the OP? Would that be too awkward or burdensome for the user?
    – landau
    May 25, 2021 at 11:58
  • Honestly, if the wrapper could be avoided that would obviously be better. No idea whether that’s achievable with knitr. May 25, 2021 at 12:17
  • Hmm... Looks like if pattern is a language object, then knitr automatically converts it to "NA", even inside a tar_args list. So the user might just have to quote arguments on a case-by-case basis if we use chunk options. The only other alternative I can think of is to avoid chunk options altogether and use some kind of unidiomatic custom inline YAML to specify arguments to tar_target().
    – landau
    May 25, 2021 at 12:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.