forked from swcarpentry/r-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05-cmdline.html
483 lines (467 loc) · 24.6 KB
/
05-cmdline.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Programming with R</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Programming with R</h1></a>
<h2 class="subtitle">Command-Line Programs</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Use the values of command-line arguments in a program.</li>
<li>Handle flags and files separately in a command-line program.</li>
<li>Read data from standard input in a program so that it can be used in a pipeline.</li>
</ul>
</div>
</section>
<p>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 patient:</p>
<pre><code>$ Rscript readings.R --mean data/inflammation-01.csv
5.45
5.425
6.1
...
6.4
7.05
5.9</code></pre>
<p>but we might also want to look at the minimum of the first four lines</p>
<pre><code>$ head -4 data/inflammation-01.csv | Rscript readings.R --min</code></pre>
<p>or the maximum inflammations in several files one after another:</p>
<pre><code>$ Rscript readings.R --max data/inflammation-*.csv</code></pre>
<p>Our overall requirements are:</p>
<ol style="list-style-type: decimal">
<li>If no filename is given on the command line, read data from <a href="reference.html#standard-input-(stdin)">standard input</a>.</li>
<li>If one or more filenames are given, read data from them and report statistics for each file separately.</li>
<li>Use the <code>--min</code>, <code>--mean</code>, or <code>--max</code> flag to determine what statistic to print.</li>
</ol>
<p>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.</p>
<h3 id="command-line-arguments">Command-Line Arguments</h3>
<p>Using the text editor of your choice, save the following line of code in a text file called <code>session-info.R</code>:</p>
<pre class="output"><code>sessionInfo()
</code></pre>
<p>The function, <code>sessionInfo</code>, 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.</p>
<p>Now we can run the code in the file we created from the Unix Shell using <code>Rscript</code>:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript session-info.R</code></pre>
<pre class="output"><code>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
</code></pre>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pushpin"></span>Tip</h2>
</div>
<div class="panel-body">
<p>If that did not work, remember that you must be in the correct directory. You can determine which directory you are currently in using <code>pwd</code> and change to a different directory using <code>cd</code>. For a review, see this <a href="https://swcarpentry.github.io/shell-novice/01-filedir.html">lesson</a>.</p>
</div>
</aside>
<p>Now let’s create another script that does something more interesting. Write the following lines in a file named <code>print-args.R</code>:</p>
<pre class="output"><code>args <- commandArgs()
cat(args, sep = "\n")
</code></pre>
<p>The function <code>commandArgs</code> extracts all the command line arguments and returns them as a vector. The function <code>cat</code>, similar to the <code>cat</code> of the Unix Shell, outputs the contents of the variable. Since we did not specify a filename for writing, <code>cat</code> sends the output to <a href="reference.html#standard-output-(stdout)">standard output</a>, which we can then pipe to other Unix functions. Because we set the argument <code>sep</code> to <code>"\n"</code>, 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:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript print-args.R</code></pre>
<pre class="output"><code>/usr/lib/R/bin/exec/R
--slave
--no-restore
--file=print-args.R
</code></pre>
<p>From this output, we learn that <code>Rscript</code> is just a convenience command for running R scripts. The first argument in the vector is the path to the <code>R</code> executable. The following are all command-line arguments that affect the behavior of R. From the R help file:</p>
<ul>
<li><code>--slave</code>: Make R run as quietly as possible</li>
<li><code>--no-restore</code>: Don’t restore anything that was created during the R session</li>
<li><code>--file</code>: Run this file</li>
<li><code>--args</code>: Pass these arguments to the file being run</li>
</ul>
<p>Thus running a file with Rscript is an easier way to run the following:</p>
<pre class="sourceCode r"><code class="sourceCode r">R --slave --no-restore --file=print-args.R --args</code></pre>
<pre class="output"><code>/usr/lib/R/bin/exec/R
--slave
--no-restore
--file=print-args.R
--args
</code></pre>
<p>If we run it with a few arguments, however:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript print-args.R first second third</code></pre>
<pre class="output"><code>/usr/lib/R/bin/exec/R
--slave
--no-restore
--file=print-args.R
--args
first
second
third
</code></pre>
<p>then <code>commandArgs</code> adds each of those arguments to the vector it returns. Since the first elements of the vector are always the same, we can tell <code>commandArgs</code> to only return the arguments that come after <code>--args</code>. Let’s update <code>print-args.R</code> and save it as <code>print-args-trailing.R</code>:</p>
<pre class="output"><code>args <- commandArgs(trailingOnly = TRUE)
cat(args, sep = "\n")
</code></pre>
<p>And then run <code>print-args-trailing</code> from the Unix Shell:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript print-args-trailing.R first second third</code></pre>
<pre class="output"><code>first
second
third
</code></pre>
<p>Now <code>commandArgs</code> returns only the arguments that we listed after <code>print-args-trailing.R</code>.</p>
<p>With this in hand, let’s build a version of <code>readings.R</code> that always prints the per-patient (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 <code>main</code>, though we can call it whatever we want. Write the following code in a file called <code>readings-01.R</code>:</p>
<pre class="output"><code>main <- function() {
args <- commandArgs(trailingOnly = TRUE)
filename <- args[1]
dat <- read.csv(file = filename, header = FALSE)
mean_per_patient <- apply(dat, 1, mean)
cat(mean_per_patient, sep = "\n")
}
</code></pre>
<p>This function gets the name of the file to process from the first element returned by <code>commandArgs</code>. Here’s a simple test to run from the Unix Shell:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript readings<span class="fl">-01.</span>R data/inflammation<span class="fl">-01.</span>csv</code></pre>
<p>There is no output because we have defined a function, but haven’t actually called it. Let’s add a call to <code>main</code> and save it as <code>readings-02.R</code>:</p>
<pre class="output"><code>main <- function() {
args <- commandArgs(trailingOnly = TRUE)
filename <- args[1]
dat <- read.csv(file = filename, header = FALSE)
mean_per_patient <- apply(dat, 1, mean)
cat(mean_per_patient, sep = "\n")
}
main()
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">Rscript readings<span class="fl">-02.</span>R data/inflammation<span class="fl">-01.</span>csv</code></pre>
<pre class="output"><code>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
</code></pre>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Challenge - A simple command line program</h2>
</div>
<div class="panel-body">
<ul>
<li>Write a command-line program that does addition and subtraction. <strong>Hint:</strong> Everything argument read from the command-line is interpreted as a character <a href="reference.html#string">string</a>. You can convert from a string to a number using the function <code>as.numeric</code>.</li>
</ul>
<pre class="sourceCode r"><code class="sourceCode r">Rscript arith.R <span class="dv">1</span> +<span class="st"> </span><span class="dv">2</span></code></pre>
<pre class="output"><code>3
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">Rscript arith.R <span class="dv">3</span> -<span class="st"> </span><span class="dv">4</span></code></pre>
<pre class="output"><code>-1
</code></pre>
<ul>
<li>What goes wrong if you try to add multiplication using <code>*</code> to the program?</li>
</ul>
<!-- second-answer -->
<!-- The * is a wildcard character in the Unix Shell. -->
<!-- Thus all the files in the current working directory are included as arguments to arith.R. -->
<ul>
<li>Using the function <code>list.files</code> introduced in a previous <a href="03-loops-R.html">lesson</a>, write a command-line program, <code>find-pattern.R</code>, that lists all the files in the current directory that contain a specific pattern:</li>
</ul>
<pre class="sourceCode r"><code class="sourceCode r"><span class="co"># For example, searching for the pattern "print-args" returns the two scripts we</span>
<span class="co"># wrote earlier</span>
Rscript find-pattern.R print-args</code></pre>
<pre class="output"><code>print-args.R
print-args-trailing.R
</code></pre>
</div>
</section>
<h3 id="handling-multiple-files">Handling Multiple Files</h3>
<p>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 patients. Let’s investigate them from the Unix Shell:</p>
<pre class="sourceCode r"><code class="sourceCode r">ls data/small-<span class="er">*</span>.csv</code></pre>
<pre class="output"><code>data/small-01.csv
data/small-02.csv
data/small-03.csv
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">cat data/small<span class="fl">-01.</span>csv</code></pre>
<pre class="output"><code>0,0,1
0,1,2
</code></pre>
<pre class="sourceCode r"><code class="sourceCode r">Rscript readings<span class="fl">-02.</span>R data/small<span class="fl">-01.</span>csv</code></pre>
<pre class="output"><code>0.3333333
1
</code></pre>
<p>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.</p>
<p>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 <code>commandArgs(trailingOnly = TRUE)</code>. We’ll need to handle an unknown number of filenames, since our program could be run for any number of files.</p>
<p>The solution is to loop over the vector returned by <code>commandArgs(trailingOnly = TRUE)</code>. Here’s our changed program, which we’ll save as <code>readings-03.R</code>:</p>
<pre class="output"><code>main <- function() {
args <- commandArgs(trailingOnly = TRUE)
for (filename in args) {
dat <- read.csv(file = filename, header = FALSE)
mean_per_patient <- apply(dat, 1, mean)
cat(mean_per_patient, sep = "\n")
}
}
main()
</code></pre>
<p>and here it is in action:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript readings<span class="fl">-03.</span>R data/small<span class="fl">-01.</span>csv data/small<span class="fl">-02.</span>csv</code></pre>
<pre class="output"><code>0.3333333
1
13.66667
11
</code></pre>
<p><strong>Note</strong>: at this point, we have created three versions of our script called <code>readings-01.R</code>, <code>readings-02.R</code>, and <code>readings-03.R</code>. We wouldn’t do this in real life: instead, we would have one file called <code>readings.R</code> 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.</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Challenge - A command line program with arguments</h2>
</div>
<div class="panel-body">
<ul>
<li>Write a program called <code>check.R</code> 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?</li>
</ul>
</div>
</section>
<h3 id="handling-command-line-flags">Handling Command-Line Flags</h3>
<p>The next step is to teach our program to pay attention to the <code>--min</code>, <code>--mean</code>, and <code>--max</code> flags. These always appear before the names of the files, so let’s save the following in <code>readings-04.R</code>:</p>
<pre class="output"><code>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()
</code></pre>
<p>And we can confirm this works by running it from the Unix Shell:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript readings<span class="fl">-04.</span>R --max data/small<span class="fl">-01.</span>csv</code></pre>
<pre class="output"><code>1
2
</code></pre>
<p>but there are several things wrong with it:</p>
<ol style="list-style-type: decimal">
<li><p><code>main</code> is too large to read comfortably.</p></li>
<li><p>If <code>action</code> 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). <a href="reference.html#silent-failure">Silent failures</a> like this are always hard to debug.</p></li>
</ol>
<p>This version pulls the processing of each file out of the loop into a function of its own. It also checks that <code>action</code> is one of the allowed flags before doing any processing, so that the program fails fast. We’ll save it as <code>readings-05.R</code>:</p>
<pre class="output"><code>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()
</code></pre>
<p>This is four lines longer than its predecessor, but broken into more digestible chunks of 8 and 12 lines.</p>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pushpin"></span>Tip</h2>
</div>
<div class="panel-body">
<p>R has a package named <a href="http://cran.r-project.org/web/packages/argparse/index.html">argparse</a> that helps handle complex command-line flags (it utilizes a <a href="http://docs.python.org/dev/library/argparse.html">Python module</a> of the same name). We will not cover this package in this lesson but when you start writing programs with multiple parameters you’ll want to read through the package’s <a href="http://cran.r-project.org/web/packages/argparse/vignettes/argparse.pdf">vignette</a>.</p>
</div>
</aside>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Challenge - Shorter command line arguments</h2>
</div>
<div class="panel-body">
<ul>
<li><p>Rewrite this program so that it uses <code>-n</code>, <code>-m</code>, and <code>-x</code> instead of <code>--min</code>, <code>--mean</code>, and <code>--max</code> respectively. Is the code easier to read? Is the program easier to understand?</p></li>
<li><p>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.</p></li>
</ul>
</div>
</section>
<h3 id="handling-standard-input">Handling Standard Input</h3>
<p>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 <code>count-stdin.R</code>:</p>
<pre class="output"><code>lines <- readLines(con = file("stdin"))
count <- length(lines)
cat("lines in standard input: ")
cat(count, sep = "\n")
</code></pre>
<p>This little program reads lines from the program’s standard input using <code>file("stdin")</code>. 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 <code>readLines</code>, 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:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript count-stdin.R <<span class="st"> </span>data/small<span class="fl">-01.</span>csv</code></pre>
<pre class="output"><code>lines in standard input: 2
</code></pre>
<p>Note that because we did not specify <code>sep = "\n"</code> when calling <code>cat</code>, the output is written on the same line.</p>
<p>A common mistake is to try to run something that reads from standard input like this:</p>
<pre class="sourceCode r"><code class="sourceCode r">Rscript count-stdin.R data/small<span class="fl">-01.</span>csv</code></pre>
<p>i.e., to forget the <code><</code> 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 <code>ctrl</code>+<code>z</code> (technically it is still paused in the background; if you want to fully kill the process follow these <a href="http://linux.about.com/library/cmd/blcmdl_kill.htm">instructions</a>).</p>
<p>We now need to rewrite the program so that it loads data from <code>file("stdin")</code> if no filenames are provided. Luckily, <code>read.csv</code> can handle either a filename or an open file as its first parameter, so we don’t actually need to change <code>process</code>. That leaves <code>main</code>, which we’ll update and save as <code>readings-06.R</code>:</p>
<pre class="output"><code>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()
</code></pre>
<p>Let’s try it out. Instead of calculating the mean inflammation of every patient, we’ll only calculate the mean for the first 10 patients (rows):</p>
<pre class="sourceCode r"><code class="sourceCode r">head data/inflammation<span class="fl">-01.</span>csv |<span class="st"> </span>Rscript readings<span class="fl">-06.</span>R --mean</code></pre>
<pre class="output"><code>5.45
5.425
6.1
5.9
5.55
6.225
5.975
6.65
6.625
6.525
</code></pre>
<p>And now we’re done: the program now does everything we set out to do.</p>
<section class="challenge panel panel-success">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pencil"></span>Challenge - Implementing wc in R</h2>
</div>
<div class="panel-body">
<ul>
<li>Write a program called <code>line-count.R</code> that works like the Unix <code>wc</code> command:</li>
<li>If no filenames are given, it reports the number of lines in standard input.</li>
<li>If one or more filenames are given, it reports the number of lines in each, followed by the total number of lines.</li>
</ul>
</div>
</section>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-pushpin"></span>Key Points</h2>
</div>
<div class="panel-body">
<ul>
<li>Use <code>commandArgs(trailingOnly = TRUE)</code> to obtain a vector of the command-line arguments that a program was run with.</li>
<li>Avoid silent failures.</li>
<li>Use <code>file("stdin")</code> to connect to a program’s standard input.</li>
<li>Use <code>cat(vec, sep = "\n")</code> to write the elements of <code>vec</code> to standard output, one per line.</li>
</ul>
</div>
</aside>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/r-novice-inflammation">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>