Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different results when R script is automated

The following command executes ghostscript on a pdf file. (the pdf_file variable contains the path to that pdf)

bbox <- system(paste( "C:/gs/gs8.64/bin/gswin32c.exe -sDEVICE=bbox -dNOPAUSE -dBATCH -f", pdf_file, "2>&1" ), intern=TRUE)

After execution bbox includes the following character string.

GPL Ghostscript 8.64 (2009-02-03)
Copyright (C) 2009 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
%%BoundingBox: 36 2544 248 2825
%%HiResBoundingBox: 36.395015 2544.659922 247.070032 2824.685914
Error: /undefinedfilename in (2>&1)
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
   --dict:1147/1684(ro)(G)--   --dict:1/20(G)--   --dict:69/200(L)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 8.64: Unrecoverable error, exit code 1

This string is then manipulated in order for the BoundingBox dimensions (36 2544 248 2825) to be isolated and used for cropping the pdf file. So far everything works ok.

However, when I schedule this script in Task Manager (using Rscript.exe or Rcmd.exe BATCH), or when the script is inside an R chunk and I press knit HTML, bbox gets the following character string which lacks the BoundingBox information, and makes it unusable:

GPL Ghostscript 8.64 (2009-02-03)
Copyright (C) 2009 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
Error: /undefinedfilename in (2>&1)
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
   --dict:1147/1684(ro)(G)--   --dict:1/20(G)--   --dict:69/200(L)--
Current allocation mode is local
Last OS error: No such file or directory

How can I get over this problem and have the script run automated?

(The script comes from the accepted answer to that question)

like image 584
George Dontas Avatar asked Feb 14 '13 08:02

George Dontas


People also ask

Can you automate an R script?

Schedule R scripts/processes with the Windows task scheduler. This allows R users working on Windows to automate R processes on specific timepoints from R itself.

How do I know if an R script is running?

In RStudio, select "New Script" from the "File" menu. You'll now see a "Script" panel appear. Try typing a command into this panel and then press the "Run" button shown below. You should see the results of running the script appear in the console panel.

How do I run an R script all at once?

To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).


3 Answers

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"
like image 97
ixe013 Avatar answered Oct 22 '22 14:10

ixe013


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.

like image 45
KenS Avatar answered Oct 22 '22 16:10

KenS


try this.

Set the output file using an environmental 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%"
like image 1
Fred F Avatar answered Oct 22 '22 15:10

Fred F