rOpenSci | How to Use System Commands in your R Script or Package

Have you ever found a command-line tool that’s perfect for getting your job done, and wanted to use it from an R script or package? E.g. some sort of scientific software providing a specific functionality made available though a command-line interface (CLI)?


This is a companion discussion topic for the original entry at https://ropensci.org/blog/2021/09/13/system-calls-r-package/

Thanks for writing this blogpost.

Reading this led me to closely look into the source code of system(), and left me wondering what the input argument is for. In the source code, we writeLines(input, f). Also, the help page:

if a character vector is supplied, this is copied one string per line to a temporary file, and the standard input of command is redirected to the file.

What is the role of input in system()? How is it different from the command argument?

It is the stdin stream of the program so for example you can pipe data to your program:

x <- 'foo\nbar\nbaz\n'
system("grep ba", input = x)

That would be the equivalent of a shell command

echo 'foo\nbar\nbaz\n' | grep ba
2 Likes