neversaint
neversaint

Reputation: 64054

How to make R script takes input from pipe and user given parameter

I have the following R script (myscript.r)

#!/usr/bin/env Rscript
dat <- read.table(file('stdin'), sep=" ",header=FALSE)
# do something with dat
# later with user given "param_1" 

With that script we can run it the following way;

$ cat data_no_*.txt | ./myscript.r

What I want to do is to make the script takes additional parameter from user:

$ cat data_no_*.txt | ./myscript.r param_1

What should I do to modify the myscript.r to accommodate that?

Upvotes: 3

Views: 1808

Answers (2)

flodel
flodel

Reputation: 89097

For very basic usage, have a look at ?commandArgs.

For more complex usage, two popular packages for command-line arguments and options parsing are getopt and optparse. I use them all the time, they get the job done. I also see argparse, argparser, and GetoptLong but have never used them before. One I missed: Dirk recommended that you look at docopt which does seem very nice and easy to use.

Finally, since you seem to be passing arguments via pipes you might find this OpenRead() function useful for generalizing your code and allowing your arguments to be pipes or files.


I wanted to test docopt so putting it all together, your script could look like this:

#!/usr/bin/env Rscript

## Command-line parsing ##

'usage: my_prog.R [-v -m <msg>] <param> <file_arg> 

options:
 -v        verbose
 -m <msg>  Message' -> doc

library(docopt)
opts <- docopt(doc)

if (opts$v) print(str(opts)) 
if (!is.null(opts$message)) cat("MESSAGE: ", opts$m)

## File Read ##

OpenRead <- function(arg) {
   if (arg %in% c("-", "/dev/stdin")) {
      file("stdin", open = "r")
   } else if (grepl("^/dev/fd/", arg)) {
      fifo(arg, open = "r")
   } else {
      file(arg, open = "r")
   }
}

dat.con <- OpenRead(opts$file_arg)
dat <- read.table(dat.con, sep = " ", header = FALSE)

# do something with dat and opts$param

And you can test running:

echo "1 2 3" | ./test.R -v -m HI param_1 -

or

./test.R -v -m HI param_1 some_file.txt

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368439

We built littler to support just that via its r executable.

Have a look at its examples, it may fit your bill.

Upvotes: 2

Related Questions