windows - R 脚本自动化时的不同结果

标签 windows r redirect automation ghostscript

以下命令在 pdf 文件上执行 ghostscript。 (pdf_file 变量包含该 pdf 的路径)

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

执行后bbox包含如下字符串。

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

然后操作此字符串,以便隔离 BoundingBox 尺寸 (36 2544 248 2825) 并用于裁剪 pdf 文件。到目前为止一切正常。

但是,当我在任务管理器中安排此脚本(使用 Rscript.exe 或 Rcmd.exe BATCH)时,或者当脚本位于 R block 内并且我按下 knit HTML 时,bbox 获取以下字符串缺少 BoundingBox 信息,使其无法使用:

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

我怎样才能克服这个问题并让脚本自动运行?

(脚本来自对 that question 的接受答案)

最佳答案

您在命令末尾添加的 2>&1 被发送到 ghostscript 解释器,而不是 shell。 Ghostscript interprets it a file ,因此错误。我使用procmon查看进程创建:

stderr redirection is treated as a file by ghostscript

要让 shell 解释它,您必须在命令前加上 cmd/c,就像这样

> 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"

关于windows - R 脚本自动化时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14870400/

相关文章:

mysql - 无法加载库 RMySQL

authentication - Cypress :登录身份验证重定向到另一个域:解决方法?

使用 mod_rewrite 重定向查询字符串(反之亦然)

javascript - 页面重定向脚本给了我一个重定向循环

windows - 注册表项和值名称的有效字符是什么?

r - gc() 和 rm() 有什么区别

r - 我如何 reshape 这张 table ?

C++ - 对 Windows 命令使用 system()

c# - 如何在windows universal app中创建app bar slide menu

python - 使用 Python 在 Windows 10 上列出本地运行的服务?