Dear R experts This probably seems very easy to you guys, but I'm a beginner and would be really glad if someone helped me with this: I am trying to automate the execution of an R script (let's call it "myscript.R") by passing a variable from a bash script to myscript.R. I know I can use the command Rscript, but I don't know how to declare in bash which variable will be accessed by the "commandArgs" command in myscript.R. So my bash script looks about like this: #!/bin/bash VARIABLES=( a b c d ) for i in ${VARIABLES[@]}; do VARIABLENAME=$i Rscript -e 'source("myscript.R")' done In myscript.R, I would like to use the current VARIABLENAME when executing the program, i.e., myscript <- function() { args <- commandArgs(TRUE) # args should now be set to either a,b,c, or d load(paste("/home/user/../../", args, ".RData", sep="")) # this defines the path to the data file that will be used in this run ...further commands... } At the moment, myscript.R doesn't seem to be executed at all when I execute the bash script. Any help will be greatly appreciated! Thanks, sophie -- View this message in context: http://r.789695.n4.nabble.com/export-variable-from-bash-to-R-tp4647749.html Sent from the R help mailing list archive at Nabble.com.
W dniu 29.10.2012 11:43, sophie pisze:> Dear R experts > > This probably seems very easy to you guys, but I'm a beginner and would be > really glad if someone helped me with this: > I am trying to automate the execution of an R script (let's call it > "myscript.R") by passing a variable from a bash script to myscript.R. > I know I can use the command Rscript, but I don't know how to declare in > bash which variable will be accessed by the "commandArgs" command in > myscript.R. > > So my bash script looks about like this: > > #!/bin/bash > > VARIABLES=( a b c d ) > > for i in ${VARIABLES[@]}; do > VARIABLENAME=$i > Rscript -e 'source("myscript.R")' > doneVARIABLES=(a b c d) for i in ${VARIABLES[@]} do Rscript myscript.R $i done> In myscript.R, I would like to use the current VARIABLENAME when executing > the program, i.e., > myscript <- function() { > > args <- commandArgs(TRUE) # args should now be set to either a,b,c, or d > load(paste("/home/user/../../", args, ".RData", sep="")) # this defines > the path to the data file that will be used in this run > > ...further commands... > } > > At the moment, myscript.R doesn't seem to be executed at all when I execute > the bash script.When loading the script with "source", the output won't show up unless you explicitely print it (see ?source). Maybe it's executed but you just don't see the output?> Any help will be greatly appreciated!-- Best regards, Krzysztof Mitko
On Mon, Oct 29, 2012 at 10:43 AM, sophie <melanie.bieli at bluewin.ch> wrote:> Dear R experts > > This probably seems very easy to you guys, but I'm a beginner and would be > really glad if someone helped me with this: > I am trying to automate the execution of an R script (let's call it > "myscript.R") by passing a variable from a bash script to myscript.R. > I know I can use the command Rscript, but I don't know how to declare in > bash which variable will be accessed by the "commandArgs" command in > myscript.R. > > So my bash script looks about like this: > > #!/bin/bash > > VARIABLES=( a b c d ) > > for i in ${VARIABLES[@]}; do > VARIABLENAME=$i > Rscript -e 'source("myscript.R")' > done > > In myscript.R, I would like to use the current VARIABLENAME when executing > the program, i.e., > myscript <- function() { > > args <- commandArgs(TRUE) # args should now be set to either a,b,c, or d > load(paste("/home/user/../../", args, ".RData", sep="")) # this defines > the path to the data file that will be used in this run > > ...further commands... > } > > At the moment, myscript.R doesn't seem to be executed at all when I execute > the bash script. > Any help will be greatly appreciated!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
On Tue, Oct 30, 2012 at 2:03 PM, sophie <melanie.bieli at bluewin.ch> wrote:> 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 didn't hang, it was probably sitting there waiting for you to say q() - I was just using it interactively.> 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?Does it produce any printed output? Can you add some print statements to see where it gets to? $ cat myscript.R print("Hello world") $ Rscript -e 'source("myscript.R")' [1] "Hello world" - here at least I know its finding the right script. Barry
Hi Barry, I already tried adding print commands, but even if I put the print command right under "myscript <- function()", there is no printed output in the shell... sophie -- View this message in context: http://r.789695.n4.nabble.com/export-variable-from-bash-to-R-tp4647749p4647897.html Sent from the R help mailing list archive at Nabble.com.
On Tue, Oct 30, 2012 at 4:34 PM, sophie <melanie.bieli at bluewin.ch> wrote:> Hi Barry, > > I already tried adding print commands, but even if I put the print command > right under "myscript <- function()", there is no printed output in the > shell...If your script is just: myscript <- function(){ # some stuff } Then all it is doing is defining a function. It won't actually call the function unless you call it with something like: myscript() after you've defined it. Or you could just not bother wrapping the code in a function at all. Barry
Hi Barry, OK, now I absolutely do feel like the newbie I am :-) I added the function call, and - what a surprise - everything is working fine. Thanks a lot, sophie -- View this message in context: http://r.789695.n4.nabble.com/export-variable-from-bash-to-R-tp4647749p4647912.html Sent from the R help mailing list archive at Nabble.com.