Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown accessing parameter from bash chunk

Tags:

bash

r

r-markdown

I created an RMarkdown file file.Rmd with parameters.

I know how to access parameters within a r chunk but not from a bash chunk

If there is absolutely no way to do so, I will write the parameters in a file through r chunk and then read it from bash chunk...

---
output: html_document
params:
  myParam1:
    label: "Choose 1st parameter"
    value: 20
    input: slider
    min: 0
    max: 100
  myParam2:
    label: "Choose 2nd parameter"
    value: "Hello"
    input: text
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo=FALSE}
print(paste("1st parameter :",params$myParam1))
print(paste("2nd parameter :",params$myParam2))
```

```{bash}
# Don't know how to get parameters here
echo $params
```

Thanks

like image 838
qfazille Avatar asked Apr 20 '17 10:04

qfazille


2 Answers

I've seen a few options

  1. Use Sys.setenv to export variables from R to bash, so add this line to an R chunk.

    Sys.setenv(params = params$myParam1)

  2. Use the runr package

like image 121
Chris S. Avatar answered Oct 23 '22 15:10

Chris S.


To apply the export-to-envirnoment idea from the accepted answer to all params, just add the following do.call loop to an R chunk before the bash chunk:

```{r, echo=FALSE, message=FALSE}
for (key in names(params)) {
  do.call('Sys.setenv', params[key])
}
```
like image 26
teichert Avatar answered Oct 23 '22 17:10

teichert