Set Environment Variables for System() in R

Accessing environment variables set in R session from shell

The environmental variable dies with the process.

Each process has its own set of environmental variables, inherited from the parent process. When you create the environmental variable BLAH, you create it in the environment of the R process you're running, but not in the environment of the parent process.

If you want another process to access this environmental variable, you'll need to start the process from within R. Then the child process will inherit BLAH. This documentation for Sys.setenv mentions this:

Sys.setenv sets environment variables (for other processes called from within R or future calls to Sys.getenv from this R process).

For example:

Sys.setenv(BLAH="blah")
system("echo $BLAH")
# blah

set environment variables for system() in R?

I think you are confusing the issue. I fear this may be about login shells versus non-login shells. See the bash manual page for the fine print ... which has driven me bonkers in the past.

That said, if you can set environment variables system-wide, you have a few options:

  • /etc/environment is a very good place as it is shell-agnostic should you ever use a different shell
  • for login versus non-login shells, the one way to get complete control that I found suitable was to put my changes into something like ~/.local_bashrc
  • the add . ~/.local_bashrc from and and all of

    • ~./bashrc
    • ~/.bash_profile
    • ~/.profile`

    etc pp.

You can precede the sourcing with a echo Hello from FILE where you replace FILE with the name of the file. That shows you the difference between shells starting from login (eg via gdm et al), via ssh connection, via new xterm etc terminals and so on.

How/When is the HOME environment variable set within R

R startup is somewhat complicated, but it is pretty well documented. The usual starting place is help("Startup"). The answer to your question is not documented there, but you will find this clue in the See also section:

For the definition of the ‘home’ directory on Windows see the ‘rw-FAQ’
Q2.14. It can be found from a running R by Sys.getenv("R_USER")

and indeed the cited FAQ at https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f gives us the answer:

The home directory is set as follows: If environment variable R_USER
is set, its value is used. Otherwise if environment variable HOME is
set, its value is used. After those two user-controllable settings, R
tries to find system-defined home directories. It first tries to use
the Windows "personal" directory (typically
C:\Users\username\Documents). If that fails, if both environment
variables HOMEDRIVE and HOMEPATH are set (and they normally are), the
value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current
working directory is used.

setting environment variables programmatically

You can use do.call to call the function with that named argument:

args = list(var.value)
names(args) = var.name
do.call(Sys.setenv, args)

Set environment variable in bash in Rmarkdown

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

How to unfold user and environment variable in R language?

Pretty much the same!

path.expand("~")
#[1] "/Users/Simon"

path.expand will expand a path name by replacing a leading tilde by the user's home directory (if defined on that platform).

And Sys.getenv() to get the value of environmental variables defined on your system, e.g.

#  Path to R home directory
Sys.getenv( "R_HOME" )
#[1] "/Library/Frameworks/R.framework/Resources"

# Path to default R library
Sys.getenv("R_LIBS")
#[1] "~/R64Libs"

To see the available environment variables...

head( names(Sys.getenv()) )
#[1] "__CF_USER_TEXT_ENCODING" "Apple_PubSub_Socket_Render" "Apple_Ubiquity_Message"
#[4] "COMMAND_MODE" "DISPLAY" "EDITOR"

Defining a new environmental variable

To set an environment variable to make it always available to R you need to set that variable in a file called .Renviron which by default is located in your {$HOME} directory. So for instance to make the environment variable R_WORKSPACE available I add the line

R_WORKSPACE = ~/Documents/R/StackOverflow

To /Users/Simon/.Renivron. Then when I load up R you see that path expansion is done automatically...

#  Clean workspace - commented out so you don't wipe your session!
# rm( list = ls() )

# See that variable is now available in R
Sys.getenv( "R_WORKSPACE" )
[1] "~/Documents/R/StackOverflow"

See the answer here for a bit more info and options.



Related Topics



Leave a reply



Submit