Reputation: 183
I have a bash script that creates a csv file and an R file that creates graphs from that.
At the end of the bash script I call Rscript Graphs.R 10
The response I get is as follows:
Error in is.vector(X) : subscript out of bounds
Calls: print ... <Anonymous> -> lapply -> FUN -> lapply -> is.vector
Execution halted
The first few lines of my Graphs.R are:
#!/bin/Rscript
args <- commandArgs(TRUE)
CorrAns = args[1]
No idea what I am doing wrong? The advice on the net appears to me to say that this should work. Its very hard to make sense of commandArgs
Upvotes: 17
Views: 20293
Reputation: 183
Rscript args.R 10
where 10 is the numeric value we want to pass to the R script.
print(as.numeric(commandArgs(TRUE)[1])
prints out the value which can then be assigned to a variable.
Upvotes: 1
Reputation: 138517
With the following in args.R
print(commandArgs(TRUE)[1])
and the following in args.sh
Rscript args.R 10
I get the following output from bash args.sh
[1] "10"
and no error. If necessary, convert to a numberic type using as.numeric(commandArgs(TRUE)[1])
.
Upvotes: 16
Reputation: 176738
Just a guess, perhaps you need to convert CorrAns
from character to numeric, since Value section of ?CommandArgs
says:
A character vector containing the name of the executable and the user-supplied command line arguments.
UPDATE: It could be as easy as:
#!/bin/Rscript
args <- commandArgs(TRUE)
(CorrAns = args[1])
(CorrAns = as.numeric(args[1]))
Upvotes: 6
Reputation: 138517
Reading the docs, it seems you might need to remove the TRUE
from the call to commandArgs()
as you don't call the script with --args
. Either that, or you need to call Rscript Graphs.R --args 10
.
Usage
commandArgs(trailingOnly = FALSE)
Arguments
trailingOnly
logical. Should only arguments after--args
be returned?
Upvotes: 1