I am runnning a R script, but facing an error :
Rscript example.R “a,b,c”
I am running above code where a,b,c are elements of character vector passed as argument. The above code works fine if i pass numeric values (eg 1,5,6)
Code is :
library("optparse")
library("tldutils") # eval_string
# install.packages("tldutils", repos="http://R-Forge.R-project.org")
option_list <- list(
make_option(c("-c", "--count"), type="character", default="5",
help='Vector of numbers separated by commas and surrounded by ""',
metavar="number")
)
args <- parse_args(OptionParser(option_list = option_list))
print(args$c)
eval_string(sprintf("foo = c(%s)", args$c))
print(foo)
Error is :
Error in eval(expr, envir, enclos) : object 'a' not found
Calls: eval_string -> eval.parent -> eval -> eval
Execution halted
Please help me, where i need to edit in code?
The “a,b,c” you are passing to the script get interpreted as variable names, not as strings.
Edit: anyway, I don’t see why you want to pass data as an argument instead of
a) reading it from standard input
or better
b) reading it from a file using for example
read.table