Different results when R script is automated Different results when R script is automated windows windows

Different results when R script is automated


The 2>&1 you add at the end of the command is sent to the ghostscript interpreter, not the shell. Ghostscript interprets it a file, hence the error. I used procmon to look at the process creation:

stderr redirection is treated as a file by ghostscript

To make the shell interpret it, you must prefix the command with cmd /c, like this

> bbox <- system(paste("cmd /c C:/Progra~1/gs/gs9.07/bin/gswin64c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH -q -f",pdf_file,"2>&1"), intern=TRUE)> print (bbox)[1] "%%BoundingBox: 28 37 584 691"                                  "%%HiResBoundingBox: 28.997999 37.511999 583.991982 690.839979"


The output of the device is going to stdout, the error is going to stderr. In the terminal these are obviously both sent to the terminal and displayed together,in the second case they clearly aren't and the stdout is going missing.

This isn't too surprising since you are getting en error message on (2>&1). This looks like it is redirecting stdout to a file, but there are 2 problems. Firstly you haven't supplied a filename for the output to be sent to, and secondly, you aren't running in the command shell, so the command processor doesn't do the redirection.

I know nothing about R, so I can't tell you how to do that, but you should start by removing the '2>&1' from the command line anyway. You might also like to consider using a version of Ghostscript less than 4 years old. The current version is 9.07 and has just been released.


try this.

Set the output file using anenvironmental variable

Then use the %envvar% notation, which based on the link above would be %TODAY% which would be replaced with the file name friday. The -f isn't needed, but shouldn't hurt. If you want to route the output, set a second env variable and route it >%outenv%.

This way you can make a simple system call (see link for using variable rather than fixed strings),

Sys.setenv(envvar= "pdf.file")  Sys.setenv(outenv= "out.file")"C:/gs/gs8.64/bin/gswin32c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH %envvar% >%outenv%"