I would like to set an environment variable within an Rmarkdown bash chunk and access it in later R chunks. Essentially, I would like to do the opposite of this question, which has been answered many times: RMarkdown accessing parameter from bash chunk
I can pass a parameter into the bash chunk using R:
Sys.setenv(MY_PARAM = 'param value')
And access it in bash:
echo $MY_PARAM
param value
But when I try to store an environment variable in bash I am unable to access it in R later:
Bash again:
export MY_PARAM2="param value"
echo $MY_PARAM2
param value
A later R chunk:
Sys.getenv('MY_PARAM2')
[1] " "
I would be open to any other ideas on how to pass variables out of the bash code chunk for use in later chunks. Here's the whole thing for replication in Rmarkdown:
```{r}
Sys.setenv(MY_PARAM = 'param value')
```
```{bash}
echo $MY_PARAM
```
```{bash}
export MY_OTHER_PARAM="param value"
echo $MY_OTHER_PARAM
```
```{r}
Sys.getenv('MY_OTHER_PARAM')
```