thenonacmguy
thenonacmguy

Reputation: 51

How to provide file input from command line arguments in R

I want my R script to accept data from a .csv file. Is there a way to do this from the command prompt.

Just like if I write Rscript myscript.R 20, it passes a value 20 as input. I want to know if specifying the absolute address of the csv file will allow my script to use the data inside the csv file. If not what do I have to do to achieve what I want?

Upvotes: 0

Views: 1646

Answers (1)

sgibb
sgibb

Reputation: 25736

Have a look at ?commandArgs. Minimal example:

#!/usr/bin/Rscript
print(commandArgs(trailingOnly=TRUE))

Run it:

./myscript.R yourcsvfile.csv
[1] "yourcsvfile.csv"

Maybe you will be interested in the getopt package, too.

Upvotes: 1

Related Questions