R, passing variables to a system command R, passing variables to a system command shell shell

R, passing variables to a system command


Let's say we have the variable x that we want to pass on to dmtxwrite, you can pass it on like:

x = 10system(sprintf("dmtxwrite %s -o image.png", x))

or alternatively using paste:

system(paste("dmtxwrite", x, "-o image.png"))

but I prefer sprintf in this case.


Also making use of base::system2 may be worth considering as system2 provides args argument that can be used for that purpose. In your example:

my_r_variable <- "a"system2(    'echo',    args = c(my_r_variable, '-o image.png'))

would return:

 a -o image.png

which is equivalent to running echo in the terminal. You may also want to redirect output to text files:

system2(    'echo',    args = c(my_r_variable, '-o image.png'),    stdout = 'stdout.txt',    stderr = 'stderr.txt')