-1

I need to share config variables between various R and bash programmes. They all share various resources, especially a GRASS database.

I started by creating a bash script that sets up shell variables and then runs the R programme. This way R is blind to the shell variables:

$ cat testVars.R
Sys.getenv(c("WDIR","GDIR"))
$ cat testVars.sh
#!/bin/sh
WDIR="/Work/Project/"
GDIR=$WDIR"GRASSDATA" 
Rscript testVars.R
$ ./testVars.sh
WDIR GDIR 
  ""   "" 

I then tried using the readRenviron function in R, thinking it could be used to source a bash file setting up variables. However, this results in a different problem, R is unable to replace and concatenate variables like bash does:

$ cat testVars.R
readRenviron("./testVars.sh")
Sys.getenv(c("WDIR","GDIR"))
$ cat testVars.sh
#!/bin/sh
WDIR="/Work/Project/"
GDIR=$WDIR"GRASSDATA" 
$ Rscript testVars.R
            WDIR             GDIR 
"/Work/Project/" "$WDIRGRASSDATA"  

YAML is supported to some extent in both languages, but it suffers from the same lack of replacement and concatenation facilities. For instance, with YAML I would need to repeat the working directory countless times in the configuration file.

So here is what I am looking for: a configuration format than can be used by both R and bash and also allows variable concatenation.

Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86

1 Answers1

2

I think all you need is to export variables in bash to make them accessible to R.

$ export TEST_VAR=42
$ Rscript -e "Sys.getenv('TEST_VAR')"
[1] "42"

Then concatenation can be handled with paste() or paste0().

jey1401
  • 361
  • 2
  • 8