2

I am attempting my first Rmarkdown Parameterized report. The first section will download a dataset from one of two sources (and internal vs external database) based on the params 'dataSource' selected by the user. The Rscript/code required is different for the different options.

title: "Optionally DL from external vs internal database"
output: html_document
params:
  dataSource: 
    label: "Select the database for download:"
    value: IntD
    input: radio
    choiceNames: [External DB, Internal DB]
    choiceValues: [ExtD, IntD]
    inline: TRUE

The following code does work, but is there a way to optionally run an entire Rchunk based on a params selection (some code put directly in the Rchunk header, for example)?

```{r downloadData, include=TRUE}

#Can I run different rchunk or code based on the params$dataSource value??
#Can this be done directly in the Rchunk header?

if(params$dataSource=="IntD"){
 source(here::here("R", "Intdownload.R")
}else{
 source(here::here("R", "Extdownload.R")
}
```

Other options I've explored (and am working on understanding how to implement) are listed below. Are any of these better options to investigate if download code is likely to be used in other reports/Rscript/Rmarkdown documents? I am a bit confused as to when these might be better than using 'source'?

  • creating seperate chunks in an external Rscript and use read_chunk to read in the external script, then call each chunk based on the params option selected.
  • Creating a separate Rmarkdown document for datadownload, and then calling that within this Rmarkdown document using child. Does this option essentially allow you to create 2 reports (1 by calling a child Rmarkdown doc, and another for main Rmarkdown document) while using objects created in the child document in the main document?
Jmac
  • 233
  • 1
  • 8
  • Does this answer your question? https://stackoverflow.com/questions/39550732/is-there-a-way-to-have-conditional-markdown-chunk-execution-in-rmarkdown – Wil Mar 20 '19 at 21:54
  • You can pass a variable to `eval` in the chunk options. I did this recently with a parameterized document where different combinations of chunks needed to be run or skipped. The variable `params$to_eval` was a named list of true/false for each chunk, included like this: `eval=to_eval[["health"]]` – camille Mar 21 '19 at 03:36
  • @camille - interesting, but I need to evaluate based on the value of another params value (in the case which database was used). I found the following allowed me to optionally evaluate the chunk based on a params value. `{r downloadData, eval=FALSE || params$dataSource=="IntD"}` – Jmac Mar 21 '19 at 22:13
  • @Wil - Not exactly what I was looking for, but you did answer a question I was trying to find a solution for. Thanks. – Jmac Mar 21 '19 at 22:19
  • You may want to review ?`||`, but I believe `eval=FALSE || params$dataSource==“IntD”` will always proceed to take the value of `params$dataSource==“IntD”`, and `FALSE ||` is unnecessary. Do you find that the code fails unless you include the `FALSE ||” component? – Wil Mar 22 '19 at 04:28
  • @Wil - You are correct the `FALSE || ` is not needed. I had taken the idea from elsewhere - it did seem odd, but perhaps their condition needed it. – Jmac Mar 22 '19 at 17:37

0 Answers0