Programming with R
Command-Line Programs
Learning Objectives
- Use the values of command-line arguments in a program.
- Handle flags and files separately in a command-line program.
- Read data from standard input in a program so that it can be used in a pipeline.
The R Console and other interactive tools like RStudio are great for prototyping code and exploring data, but sooner or later we will want to use our program in a pipeline or run it in a shell script to process thousands of data files. In order to do that, we need to make our programs work like other Unix command-line tools. For example, we may want a program that reads a data set and prints the average inflammation per raptor:
$ Rscript readings.R --mean data/inflammation-01.csv
5.45
5.425
6.1
...
6.4
7.05
5.9
but we might also want to look at the minimum of the first four lines
$ head -4 data/inflammation-01.csv | Rscript readings.R --min
or the maximum inflammations in several files one after another:
$ Rscript readings.R --max data/inflammation-*.csv
Our overall requirements are:
- If no filename is given on the command line, read data from standard input.
- If one or more filenames are given, read data from them and report statistics for each file separately.
- Use the
--min
,--mean
, or--max
flag to determine what statistic to print.
To make this work, we need to know how to handle command-line arguments in a program, and how to get at standard input. We’ll tackle these questions in turn below.
Command-Line Arguments
Using the text editor of your choice, save the following line of code in a text file called session-info.R
:
sessionInfo()
The function, sessionInfo
, outputs the version of R you are running as well as the type of computer you are using (as well as the versions of the packages that have been loaded). This is very useful information to include when asking others for help with your R code.
Now we can run the code in the file we created from the Unix Shell using Rscript
:
Rscript session-info.R
R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets base
Now let’s create another script that does something more interesting. Write the following lines in a file named print-args.R
:
args <- commandArgs()
cat(args, sep = "\n")
The function commandArgs
extracts all the command line arguments and returns them as a vector. The function cat
, similar to the cat
of the Unix Shell, outputs the contents of the variable. Since we did not specify a filename for writing, cat
sends the output to standard output, which we can then pipe to other Unix functions. Because we set the argument sep
to "\n"
, which is the symbol to start a new line, each element of the vector is printed on its own line. Let’s see what happens when we run this program in the Unix Shell:
Rscript print-args.R
/usr/lib/R/bin/exec/R
--slave
--no-restore
--file=print-args.R
From this output, we learn that Rscript
is just a convenience command for running R scripts. The first argument in the vector is the path to the R
executable. The following are all command-line arguments that affect the behavior of R. From the R help file:
--slave
: Make R run as quietly as possible--no-restore
: Don’t restore anything that was created during the R session--file
: Run this file--args
: Pass these arguments to the file being run
Thus running a file with Rscript is an easier way to run the following:
R --slave --no-restore --file=print-args.R --args
/usr/lib/R/bin/exec/R
--slave
--no-restore
--file=print-args.R
--args
If we run it with a few arguments, however:
Rscript print-args.R first second third
/usr/lib/R/bin/exec/R
--slave
--no-restore
--file=print-args.R
--args
first
second
third
then commandArgs
adds each of those arguments to the vector it returns. Since the first elements of the vector are always the same, we can tell commandArgs
to only return the arguments that come after --args
. Let’s update print-args.R
and save it as print-args-trailing.R
:
args <- commandArgs(trailingOnly = TRUE)
cat(args, sep = "\n")
And then run print-args-trailing
from the Unix Shell:
Rscript print-args-trailing.R first second third
first
second
third
Now commandArgs
returns only the arguments that we listed after print-args-trailing.R
.
With this in hand, let’s build a version of readings.R
that always prints the per-raptor (per-row) mean of a single data file. The first step is to write a function that outlines our implementation, and a placeholder for the function that does the actual work. By convention this function is usually called main
, though we can call it whatever we want. Write the following code in a file called readings-01.R
:
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
filename <- args[1]
dat <- read.csv(file = filename, header = FALSE)
mean_per_raptor <- apply(dat, 1, mean)
cat(mean_per_raptor, sep = "\n")
}
This function gets the name of the file to process from the first element returned by commandArgs
. Here’s a simple test to run from the Unix Shell:
Rscript readings-01.R data/inflammation-01.csv
There is no output because we have defined a function, but haven’t actually called it. Let’s add a call to main
and save it as readings-02.R
:
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
filename <- args[1]
dat <- read.csv(file = filename, header = FALSE)
mean_per_raptor <- apply(dat, 1, mean)
cat(mean_per_raptor, sep = "\n")
}
main()
Rscript readings-02.R data/inflammation-01.csv
5.45
5.425
6.1
5.9
5.55
6.225
5.975
6.65
6.625
6.525
6.775
5.8
6.225
5.75
5.225
6.3
6.55
5.7
5.85
6.55
5.775
5.825
6.175
6.1
5.8
6.425
6.05
6.025
6.175
6.55
6.175
6.35
6.725
6.125
7.075
5.725
5.925
6.15
6.075
5.75
5.975
5.725
6.3
5.9
6.75
5.925
7.225
6.15
5.95
6.275
5.7
6.1
6.825
5.975
6.725
5.7
6.25
6.4
7.05
5.9
Challenge - A simple command line program
- Write a command-line program that does addition and subtraction. Hint: Everything argument read from the command-line is interpreted as a character string. You can convert from a string to a number using the function
as.numeric
.
Rscript arith.R 1 + 2
3
Rscript arith.R 3 - 4
-1
- What goes wrong if you try to add multiplication using
*
to the program?
- Using the function
list.files
introduced in a previous lesson, write a command-line program,find-pattern.R
, that lists all the files in the current directory that contain a specific pattern:
# For example, searching for the pattern "print-args" returns the two scripts we
# wrote earlier
Rscript find-pattern.R print-args
print-args.R
print-args-trailing.R
Handling Multiple Files
The next step is to teach our program how to handle multiple files. Since 60 lines of output per file is a lot to page through, we’ll start by using three smaller files, each of which has three days of data for two raptors. Let’s investigate them from the Unix Shell:
ls data/small-*.csv
data/small-01.csv
data/small-02.csv
data/small-03.csv
cat data/small-01.csv
0,0,1
0,1,2
Rscript readings-02.R data/small-01.csv
0.3333333
1
Using small data files as input also allows us to check our results more easily: here, for example, we can see that our program is calculating the mean correctly for each line, whereas we were really taking it on faith before. This is yet another rule of programming: test the simple things first.
We want our program to process each file separately, so we need a loop that executes once for each filename. If we specify the files on the command line, the filenames will be returned by commandArgs(trailingOnly = TRUE)
. We’ll need to handle an unknown number of filenames, since our program could be run for any number of files.
The solution is to loop over the vector returned by commandArgs(trailingOnly = TRUE)
. Here’s our changed program, which we’ll save as readings-03.R
:
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
for (filename in args) {
dat <- read.csv(file = filename, header = FALSE)
mean_per_raptor <- apply(dat, 1, mean)
cat(mean_per_raptor, sep = "\n")
}
}
main()
and here it is in action:
Rscript readings-03.R data/small-01.csv data/small-02.csv
0.3333333
1
13.66667
11
Note: at this point, we have created three versions of our script called readings-01.R
, readings-02.R
, and readings-03.R
. We wouldn’t do this in real life: instead, we would have one file called readings.R
that we committed to version control every time we got an enhancement working. For teaching, though, we need all the successive versions side by side.
Challenge - A command line program with arguments
- Write a program called
check.R
that takes the names of one or more inflammation data files as arguments and checks that all the files have the same number of rows and columns. What is the best way to test your program?
Handling Command-Line Flags
The next step is to teach our program to pay attention to the --min
, --mean
, and --max
flags. These always appear before the names of the files, so let’s save the following in readings-04.R
:
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
action <- args[1]
filenames <- args[-1]
for (f in filenames) {
dat <- read.csv(file = f, header = FALSE)
if (action == "--min") {
values <- apply(dat, 1, min)
} else if (action == "--mean") {
values <- apply(dat, 1, mean)
} else if (action == "--max") {
values <- apply(dat, 1, max)
}
cat(values, sep = "\n")
}
}
main()
And we can confirm this works by running it from the Unix Shell:
Rscript readings-04.R --max data/small-01.csv
1
2
but there are several things wrong with it:
main
is too large to read comfortably.If
action
isn’t one of the three recognized flags, the program loads each file but does nothing with it (because none of the branches in the conditional match). Silent failures like this are always hard to debug.
This version pulls the processing of each file out of the loop into a function of its own. It also checks that action
is one of the allowed flags before doing any processing, so that the program fails fast. We’ll save it as readings-05.R
:
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
action <- args[1]
filenames <- args[-1]
stopifnot(action %in% c("--min", "--mean", "--max"))
for (f in filenames) {
process(f, action)
}
}
process <- function(filename, action) {
dat <- read.csv(file = filename, header = FALSE)
if (action == "--min") {
values <- apply(dat, 1, min)
} else if (action == "--mean") {
values <- apply(dat, 1, mean)
} else if (action == "--max") {
values <- apply(dat, 1, max)
}
cat(values, sep = "\n")
}
main()
This is four lines longer than its predecessor, but broken into more digestible chunks of 8 and 12 lines.
Challenge - Shorter command line arguments
Rewrite this program so that it uses
-n
,-m
, and-x
instead of--min
,--mean
, and--max
respectively. Is the code easier to read? Is the program easier to understand?Separately, modify the program so that if no action is specified (or an incorrect action is given), it prints a message explaining how it should be used.
Handling Standard Input
The next thing our program has to do is read data from standard input if no filenames are given so that we can put it in a pipeline, redirect input to it, and so on. Let’s experiment in another script, which we’ll save as count-stdin.R
:
lines <- readLines(con = file("stdin"))
count <- length(lines)
cat("lines in standard input: ")
cat(count, sep = "\n")
This little program reads lines from the program’s standard input using file("stdin")
. This allows us to do almost anything with it that we could do to a regular file. In this example, we passed it as an argument to the function readLines
, which stores each line as an element in a vector. Let’s try running it from the Unix Shell as if it were a regular command-line program:
Rscript count-stdin.R < data/small-01.csv
lines in standard input: 2
Note that because we did not specify sep = "\n"
when calling cat
, the output is written on the same line.
A common mistake is to try to run something that reads from standard input like this:
Rscript count-stdin.R data/small-01.csv
i.e., to forget the <
character that redirect the file to standard input. In this case, there’s nothing in standard input, so the program waits at the start of the loop for someone to type something on the keyboard. We can type some input, but R keeps running because it doesn’t know when the standard input has ended. If you ran this, you can pause R by typing ctrl
+z
(technically it is still paused in the background; if you want to fully kill the process follow these instructions).
We now need to rewrite the program so that it loads data from file("stdin")
if no filenames are provided. Luckily, read.csv
can handle either a filename or an open file as its first parameter, so we don’t actually need to change process
. That leaves main
, which we’ll update and save as readings-06.R
:
main <- function() {
args <- commandArgs(trailingOnly = TRUE)
action <- args[1]
filenames <- args[-1]
stopifnot(action %in% c("--min", "--mean", "--max"))
if (length(filenames) == 0) {
process(file("stdin"), action)
} else {
for (f in filenames) {
process(f, action)
}
}
}
process <- function(filename, action) {
dat <- read.csv(file = filename, header = FALSE)
if (action == "--min") {
values <- apply(dat, 1, min)
} else if (action == "--mean") {
values <- apply(dat, 1, mean)
} else if (action == "--max") {
values <- apply(dat, 1, max)
}
cat(values, sep = "\n")
}
main()
Let’s try it out. Instead of calculating the mean inflammation of every raptor, we’ll only calculate the mean for the first 10 raptors (rows):
head data/inflammation-01.csv | Rscript readings-06.R --mean
5.45
5.425
6.1
5.9
5.55
6.225
5.975
6.65
6.625
6.525
And now we’re done: the program now does everything we set out to do.
Challenge - Implementing wc in R
- Write a program called
line-count.R
that works like the Unixwc
command: - If no filenames are given, it reports the number of lines in standard input.
- If one or more filenames are given, it reports the number of lines in each, followed by the total number of lines.