Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variable in bash in Rmarkdown

Tags:

bash

r

r-markdown

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')
```
like image 519
Patrick Avatar asked Mar 06 '20 18:03

Patrick


People also ask

How do I set environment variables in bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.

How do I remove an environment variable in R?

When you want to clear a single variable from the R environment you can use the “rm()” command followed by the variable you want to remove.


1 Answers

Exported values in bash are available in the same session only. For that reason, R will only be able to see that value, if it was started within the same session. If that would be the case for you then your question already contains the answer how it should work. This is proven to work, see this similar question.

However, if the R process or its child processes are started in its own shell, than the session variable, such as MY_OTHER_PARAM will likely be unknown to that R process. This is likely caused by how rmarkdown/knitr handles external commands. You can see this issue with the following snippet yourself: the second bash session doesn't know the exported variable, too. Others have reported this problem as well.

```{bash}
export MY_OTHER_PARAM="param value"
echo $MY_OTHER_PARAM
```
```{bash}
echo $MY_OTHER_PARAM
```

So, the easiest solution is to store the information in a specific file and read it in subsequent sessions:

```{bash}
echo 'export MY_OTHER_PARAM="param value"' > .rvars
source .rvars
echo $MY_OTHER_PARAM
```
```{bash}
source .rvars
echo $MY_OTHER_PARAM
```

If we already use files, I'd skip the environment as return path.

```{bash}
echo 'param value' > .myparam
cat .myparam
```
```{bash}
echo $(cat .myparam)
```

```{r}
string <- paste(readLines(".myparam"), collapse=" ")
print(string)
```
like image 145
blubase Avatar answered Oct 20 '22 10:10

blubase