Search code examples
rcommandsystem

R - send two-line command to system


I am using a ImageMagick command on a OS X system to create a 2x2 panel consisting of four .png figures that I have on my disk:

convert \( image1.png image2.png -append \) \
\( image3.png image4.png -append \) +append result.png

When pasted to my terminal, the above command gets adjusted by the system, receiving a > to the beginning of the second line:

convert \( image1.png image2.png -append \) \
> \( image3.png image4.png -append \) +append result.png

The resulting figure looks like this:

enter image description here

I am trying to reproduce this last command from within R by using the following commands:

> line1 <- paste0(" convert \\(", " img1.png img2.png -append \\)", " \\")
> line2 <- paste0(" < \\(", " img3.png img4.png -append \\) ", "+append result.png")
> cat(paste(line1, line2, sep='\n'))
 convert \( img1.png img2.png -append \) \
 < \( img3.png img4.png -append \) +append result.png

Apparently, the result from cat is what I need, but I can't find the right way to send it to the system. I tried to combine it with system, but I get an error:

system(cat(paste(line1, line2, sep='\n')))
Error in system(cat(paste(line1, line2, sep = "\n"))) : 
  non-empty character argument expected

The question is: how can I use R to send that specific two-line command to the system?


Solution

  • Based on the comments to this question, I ended up finding the answer. This is the code I used from within R:

    system(paste0("convert \\( img1.png img2.png -append \\) \\( img3.png img4.png -append \\) +append result.png"))