0

I am trying to create a process mainly using R, where a R variable will be sent to bash and then another Rscript file will read that value from bash, do some calculation based on that.

My primary R code looks like below -

library(parallel) 
mclapply(letters, 
                    function(Let) 
                        return(system("/usr/lib/R/bin/Rscript '/opt/R/Try.R' $Let")), mc.cores = 6)

So, basically in above code, the individual letters are being sent to bash through variable Let, and then the 2nd R process 'Try.R' will read that value and print it.

My 'Try.R' looks like below -

GetValue = commandArgs(TRUE)

print(GetValue); flush.console()

quit(save = "no")

With above implementation, it doesnt look like the value of 'Let' variable is getting passed to bash. Can someone please help me with right approach. The codes above I put just for explanation purpose to describe what I am trying to do, and hence can be executed directly in R without passing to bash, however my actual process is quite complex so needs this kind of implementation.

Any pointer will be highly appreciated.

I am using Ubuntu 18 machine with latest R.

Bogaso
  • 2,838
  • 3
  • 24
  • 54

1 Answers1

1

If you are only going to execute the bash file (and not read in the output), then the below will work:

First I write the R script:

BASH = c("GetValue = commandArgs(TRUE)",
"print(GetValue); flush.console()","quit(save='no')")

writeLines(BASH,"./Try.R")

And I execute the system call like you did, except to have the command line as a character that includes the variable, and run that:

library(parallel) 
mclapply(letters,function(Let){
CMD = paste("Rscript ./Try.R",Let)
return(system(CMD))}, 
mc.cores = 6)
StupidWolf
  • 45,075
  • 17
  • 40
  • 72