2

I am using R in ubuntu. The environment variable value returned is wrong. I had the environment variable set to a value. After a disk crash, I changed its value. However the R will always return the old value. I don't know where R cached the old value and insists on returning the old value.

The environment variable's new value

echo $WFDATA
/current/environment/value

Start a new R terminal

R
> Sys.getenv('WFDATA')
[1] "/old/environment/value"

Now try to set the environment variable to the new value

> Sys.setenv(WFDATA='/current/environment/value')
> Sys.getenv('WFDATA')
[1] "/current/environment/value"
> q()
Save workspace image? [y/n/c]: y

Start a new R terminal and you still get the old value

> Sys.getenv('WFDATA')
[1] "/old/environment/value"
> 

Not sure why R is so stubborn as to remember the old value. Any one can suggest a way to fix this problem? This problem appears in both the R session and in the Rscript file.

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56

1 Answers1

2

I figured out the source of my problem. Someone else set up the R environment in the root place. This way, the environment is always the hardcoded value. I am using Linux, this should apply to most every one.

in the /etc/R directory
ls
ldpaths  Makeconf  Renviron  Renviron.site  repositories  Rprofile.site

You can set environment variable in the Renviron and Renviron.site These will be seen by all R session on this machine. The format is the same as those used by the shell.

VARNAME=somevalue

In the user's Home directory you can overwrite these values by re-defining KEY=value pairs inside the '.Renviron' file. New environment variables can be added to this file. I have not confirmed that R will learn and remember the environment variables from the user's shell. Other's can update it later.

Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56