Skip to content

export variable from bash to R

2 messages · Barry Rowlingson, sophie

#
On Mon, Oct 29, 2012 at 10:43 AM, sophie <melanie.bieli at bluewin.ch> wrote:
Another replier has already suggested using commandArgs to get extra
arguments on the command line, but if you really want to push bash
variables through to R then you have to export them from bash and get
them using Sys.getenv in R:

$ export VARIABLENAME=a
$ R --slave
Sys.getenv("VARIABLENAME")
[1] "a"

(using R --slave to cut out all the startup messages etc)

Barry
#
Hi Barry

Thank you very much for your reply. I changed my scripts according to your
suggestions - this is how they look now:

#!/bin/bash

VARIABLES=( a b c d )

for i in ${VARIABLES[@]}; do
      export VARIABLENAME=$i
      Rscript -e 'source("myscript.R")'
done 

and in the R program, I used
args <- Sys.getenv("VARIABLENAME")  instead of args <- commandArgs(TRUE)

But when I add the "R --slave", the program hangs, so I left it out (you
said it was just to cut out the startup messages, so I assumed it shouldn't
really matter). It's still not working, though. There is no error message,
but the R program still doesn't seem to be executed (@Krzysztof: The R
program makes plots - since these plots are not produced,  I figured that
something with the call of myscript.R is not working)

Any ideas or suggestions?




--
View this message in context: http://r.789695.n4.nabble.com/export-variable-from-bash-to-R-tp4647749p4647866.html
Sent from the R help mailing list archive at Nabble.com.