2

I have a function created in R for test purpose that will get two values, add them and assign the result to a variable and the result will be stored in a csv file. Below is my r code

  f2 <- function(x, y) {
  args=(commandArgs(TRUE))
  z1 <- x + y
  z2 <- x + 2*y
  b <- list(z1, z2) 
  write.csv(b,"testfun.csv",row.names=F)
  print(z1)
  print(z2)
  print(b)
}

The code is working fine and i am able to pass the values to my function through r console. However I want to do this using R CMD and not in a r console. This is because i have a web application that will invoke this function on click of a button.

I tried to invoke the function using the below code

Rscript test.R 5 100

and

R CMD BATCH --no-save --no-restore '--args x=3 y=5' test.R test.out &

However the function is not getting invoked and i am not getting the output. When i run the above R CMD BATCH statement i'm getting an error like ''--args'' no such file or directory.

Fatal error: cannot open file ''--args': No such file or directory

So i moved the '--args x=3 y=5' values after the test.R something like

R CMD BATCH --no-save --no-restore test.R '--args x=3 y=5' test.out &

On executing the above i'm getting my r sript saved in the directory and not the my expected output. I tried all the ways from the below link

Passing command line arguments to R CMD BATCH

Can anyone shed some light over this? I need to invoke my function along with some parameter through rscript or r cmd.

Community
  • 1
  • 1
Karthik Venkatraman
  • 1,619
  • 4
  • 25
  • 55
  • You have different script names in your attempts. Please fix it. –  Nov 02 '15 at 08:06
  • Apologize.. Fixed the script name – Karthik Venkatraman Nov 02 '15 at 09:54
  • Any help for my post.... :( – Karthik Venkatraman Nov 05 '15 at 06:47
  • Late response: I think if your `test.R` file does only contain the R code from your question it does nothing but creating a function - **but never calls it**! Add a call to function at the end and process the args correctly. `args` is a list of character vectors that must be executed with `eval(parse(args[[i]]))` if you want to execute the code in the arguments... – R Yoda Feb 25 '16 at 16:31
  • For more information see: http://stackoverflow.com/questions/14167178/passing-command-line-arguments-to-r-cmd-batch – R Yoda Feb 25 '16 at 16:39
  • I can't get to --args to work either – Carl Jul 05 '17 at 17:48

0 Answers0