Chapter 10 R & the Shell

In this chapter, we will learn to

  • execute shell commands from R
  • execute shell script in RStudio
  • execute shell commands in RMarkdown
  • execute R commands & script in the shell

10.1 R

Now, let us turn our attention to executing commands from R using system2(). Here we will focus on the following

  • execute a command without arguments
  • execute commands with arguments
  • redirect output

Let us try to execute a command without any additional arguments. We will execute the ls command to list all files and directories. Use system2() and specify the command using the command argument. Whenever you are trying to execute a command from R, the first argument or input should be the command and it must be enclosed in quotes.

##  [1] "01-intro.Rmd"           "02-navigate.Rmd"       
##  [3] "03-file-management.Rmd" "04-io.Rmd"             
##  [5] "05-regex.Rmd"           "06-data-transfer.Rmd"  
##  [7] "07-sudo.Rmd"            "08-file-compress.Rmd"  
##  [9] "09-sys-info.Rmd"        "10-others.Rmd"         
## [11] "12-conclusion.Rmd"      "README.md"             
## [13] "_bookdown.yml"          "_output.yml"           
## [15] "analysis.R"             "bash-intro.Rmd"        
## [17] "bash-intro.Rproj"       "cline"                 
## [19] "docs"                   "examples"              
## [21] "img"                    "imports_blorr.txt"     
## [23] "imports_olsrr.txt"      "index.Rmd"             
## [25] "mypackage"              "myproject"             
## [27] "release.txt"            "style.css"             
## [29] "zip_example.zip"

Great! Now, how do we specify the options? The additional options of a command must be stored as a character vector and specified using the args argument. In the below example, we delete the examples folder we created earlier while decompressing the zip_example.zip file.

## character(0)

In some cases, we might want to redirect the output. Let us say we are writing message to a file using the echo command. In this cases, we want the output to be redirected to the release.txt file. The stdout argument can be used to redirect output to a file or the R console. In the below example, we redirect the output to a file.

In the next example, we redirect the output to the R console by setting the value of the stdout argument to TRUE. If you are curious, set the value to FALSE and see what happens.

## Warning in system2(command = "diff", args = c("imports_olsrr.txt",
## "imports_blorr.txt"), : running command '"diff" imports_olsrr.txt
## imports_blorr.txt' had status 1
##  [1] "1,4c1,5"      "< car "       "< checkmate"  "< cli"       
##  [5] "< clisymbols" "---"          "> car"        "> checkmate" 
##  [9] "> cli"        "> clisymbols" "> caret"     
## attr(,"status")
## [1] 1

The run() command from the processx package can be used to execute shell commands as well.

10.2 RStudio

In RStudio, commands can be executed from shell scripts by pressing Ctrl + Enter. Instead of sending the command to the R console, it is redirected to the terminal where it is executed as shown below.

10.3 RMarkdown

RMarkdown supports bash, sh and awk. This post was initially created using sh as the underlying operating system is Windows. Later, we used bash after installing the Windows subsystem for Linux. Click here to learn more.

10.4 Rscript

In this section, we will learn to execute R commands and scripts in the command line using:

  • R -e
  • Rscript -e
  • R CMD BATCH

The -e option allows us to specify R expression(s). R -e will launch R and then execute the code specified within quotes. Use semi-colon to execute multiple expressions as shown below. You will be able to run the below commands only if you are able to launch R from the command line. Windows users need to ensure that R is added to the path environment.

## 
## R version 3.6.1 (2019-07-05) -- "Action of the Toes"
## Copyright (C) 2019 The R Foundation for Statistical Computing
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## 
## R is free software and comes with ABSOLUTELY NO WARRANTY.
## You are welcome to redistribute it under certain conditions.
## Type 'license()' or 'licence()' for distribution details.
## 
##   Natural language support but running in an English locale
## 
## R is a collaborative project with many contributors.
## Type 'contributors()' for more information and
## 'citation()' on how to cite R or R packages in publications.
## 
## Type 'demo()' for some demos, 'help()' for on-line help, or
## 'help.start()' for an HTML browser interface to help.
## Type 'q()' to quit R.
## 
## > head(mtcars); tail(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
##                 mpg cyl  disp  hp drat    wt qsec vs am gear carb
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
## Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
## Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
## Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
## Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2
## > 
## >

Rscript -e will run code without launching R.

##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

We can use Rscript to execute a R script as well. In the below example, we execute the code in analysis.R file.

##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

If you are more interested, try the littler package as well.