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.