2

This is related to an existing post. Since I'm a beginner and cannot add comment, I'm asking a question and hope someone can link this to the relevant post.

I want to not execute certain chunks based on a parameter limit (TRUE or FALSE). Both @eipi10 and @xitrium's solutions work in not creating the output for the commented chunks, but the render actually still executes the commented chunks, which in my case will lead to error (due to only a subset of data is used based on another parameter and some analysis will run into missing data issue) and stop the run. Instead of using many tryCatch, I want to specify an entire block of chunks not be executed. How to achieve it?

Pumbaa
  • 70
  • 7
  • 1
    One of the [`knitr` chunk options](https://yihui.name/knitr/options/#code-evaluation) is `eval=`, which can be `TRUE`, `FALSE`, or some R variable defined in a previous chunk. Or an integer (see the docs). You can also use the same variable with other options such as `echo=`. – r2evans Jun 15 '19 at 01:58
  • Thanks @r2evans. Do I understand it correctly that I need to add `eval=FALSE` to every chunk that I don't want to run, and then change it back to `eval=TRUE` when the other parameter setting is to use the full dataset? – Pumbaa Jun 15 '19 at 02:05
  • Or ... you can set the chunks with `eval=limit`. – r2evans Jun 15 '19 at 02:19
  • 1
    Ah yes! Using `eval=params$limit` works! How do I mark your comment as answer? – Pumbaa Jun 15 '19 at 03:11

2 Answers2

4

One of the knitr chunk options is eval=, which controls whether a chunk is evaluated. From that page:

  • eval: (TRUE; logical) whether to evaluate the code chunk; it can also be a numeric vector to select which R expression(s) to evaluate, e.g. eval=c(1, 3, 4) or eval=-(4:5).

One benefit of this is that it can use R variables in real-time, either generated within a previous chunk (eval=limit) or passed to a parameterized R-markdown document (as eval=params$limit).

Depending on your global options, you might always want/need to suppress printing the chunk, you can add echo=params$limit as well.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Thanks for the instruction. Got it. I don't have enough reputation to up-vote yet, but hope I will have soon. Your answer is perfect. I like the option to have numeric vector as well. One minor comment on the parameter name (and it's due to my initial intention) - with the way it's used now, it will be more intuitive to be eval=params$execute (TRUE or FALSE). Thanks again! – Pumbaa Jun 16 '19 at 03:46
  • Two of the hardest things ... *variable naming and cache invalidation*. I know that's a bad paraphrasing, but it's so true. Not to mention `camelCase` versus `under_scores` or `alllowercase` ... – r2evans Jun 16 '19 at 03:52
  • Please don't take that comment as commanding or criticizing, it's intended as a friendly informing of common netiquette on SO, something that is not necessarily abundantly clear to all new users (I now notice that you've been a member for longer than I had thought ... SO puts a "new contributor" label on low-rep users, which I should not take as "new to SO"). I was probably too quick to post that suggestion, I'm on travel right now and a little off-kilter. I should have waited (days?) before posting it. Cheers! – r2evans Jun 16 '19 at 03:58
  • 1
    Not at all. As far as I'm concerned, I'm a new user. I didn't even remember that I signed up more than a year ago! But this was my first post in stakoverflow, so thanks for your convenient instruction. – Pumbaa Jun 17 '19 at 11:28
0

param: chunk_reporte_analitico=TRUE

TomTom
  • 11
  • 2