Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

r - command line arguments in bash to Rscript

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...