Run DOS program from R

So this command line runs fine in DOS, and the path to npx is in the path variable

npx @squoosh/cli --mozjpeg {quality:75} test.PNG

But I want to call it from R

This doesn't work:

system("npx @squoosh/cli --mozjpeg {quality:75} test.PNG") It just returns "127"

Nor does this:

shell("npx @squoosh/cli --mozjpeg {quality:75} test.PNG") Returns "'npx' is not recognized as an internal or external command, operable program or batch file."


Solution 1:

Try system2 instead, it's the recommended way for system calls.

txt <- "npx @squoosh/cli --mozjpeg {quality:75} test.PNG"
txt <- scan(text = txt, what = character())
cmd <- txt[1]
args <- txt[-1]
system2(cmd, args)

Or simply

system2(command = txt[1], args = txt[-1])