wkde
wkde

Reputation: 453

Passing arguments to an R script from command lines when the argument is a string

I want to pass arguments to an R script from command lines when the argument is a string I know that if the argument is a numeric value, I can write something in the command line :

R CMD BATCH "--args CHR=1" rfile.R test.Rout

But I want to input a file name such as "file1.txt" in r command. If I put

R CMD BATCH "--args CHR=1 file="file1.txt" rfile.R test.Rout

It does not work. How can I fix it?

Upvotes: 1

Views: 2806

Answers (1)

JBGruber
JBGruber

Reputation: 12478

Here is a simple R script which would take string inputs:

args <- commandArgs(trailingOnly = TRUE)
cat(args, sep = "\n")

I save the file as "test.R" in my home directory. In the command line I can then use:

Rscript test.R "file.txt"

The " are optional if your string does not have whitespace. But this would be recognised as two inputs:

Rscript test.R file 1.txt

There is a nice little tutorial from which I took this here.

Upvotes: 4

Related Questions