简体   繁体   中英

Set environment variable in bash in Rmarkdown

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')
```

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)
```

Since afaik R checks the ENV variables at startup and your bash session creates the vars you are interested in only after that session has started that is gonna be a tough one. I guess even restarting your R session will not help since that also woudl refresh your bash env I guess.
I would suggest to put the values you are interested in into a file eg in your temporary directory and later read them back into your session, when needed.
Another choice would be to adjust .Renviron accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM