如何使用 R on Windows 10 从端到端编写 EPS 文件?

How to write EPS files using R on Windows 10 from end-to-end?

这是为科学手稿提交生成可发布的 EPS 文件的可怕时刻。数据,绘图的线条图,在 R 中(RStudio 与 R 4.02 版相结合)。我以前遇到过这个挑战,但是在一台人口众多的 Linux 机器上。今天,我使用的是四个月前才安装的 x64 位 Windows 10。

密码是:

require(graphics)
options(printcmd = 'gsprint -ghostscript gswin64')
postscript(file = tempfile("Rps."), print.it=TRUE, family="sans")
plot_grid(image1Agg, image1Bgg, image1Cgg, labels = c('A','B','C'), label_size = 12, label_fontfamily = "sans")
dev.off()

我为 Win10 安装了最新版本的 gsview 和 ghostscript,为了避免与目录结构和“Program Files”混淆字符的烦恼,我将两组二进制文件的路径添加到 PATH。

当我在 RStudio 的控制台上执行代码时,我得到...

Warning message:
In dev.off() : error from postscript() in running:
    gsprint -ghostscript gswin64 C:\Users\yewro\AppData\Local\Temp\Rtmp0Yqsfg\Rps.17d065ef330

如果我在 Windows 命令上执行此命令,我会看到更多(正如我在 R postscript() 文档中所期望的那样):

Copyright (C) 2003-2006, Ghostgum Software Pty Ltd.  All Rights Reserved.
2006-02-24 gsprint 1.9
GPL Ghostscript 9.53.1 (2020-09-14)
Copyright (C) 2020 Artifex Software, Inc.  All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Loading NimbusSans-Regular font from %rom%Resource/Font/NimbusSans-Regular... 4216964 2747487 1743856 440491 2 done.
Loading NimbusSans-Bold font from %rom%Resource/Font/NimbusSans-Bold... 4283076 2921313 1764056 451768 2 done.
Loading NimbusSans-Italic font from %rom%Resource/Font/NimbusSans-Italic... 4470388 3128353 1764056 459255 2 done.
Loading NimbusSans-BoldItalic font from %rom%Resource/Font/NimbusSans-BoldItalic... 4677900 3341192 1784256 470184 2 done.
Loading StandardSymbolsPS font from %rom%Resource/Font/StandardSymbolsPS... 4771268 3431892 1784256 477717 2 done.
GPL Ghostscript 9.53.1: **** Could not open the file 000002dc .
Error: /invalidfileaccess in --showpage--
Operand stack:
   1   true
Execution stack:
   %interp_exit   .runexec2   --nostringval--   showpage   --nostringval--   2   %stopped_push   --nostringval--   showpage   showpage   false   1   %stopped_push   1990   1   3   %oparray_pop   1989   1   3   %oparray_pop   1988   1   3   %oparray_pop   showpage   1977   1   3   %oparray_pop   1833   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   showpage   --nostringval--   2   %stopped_push   --nostringval--   showpage   1840   0   3   %oparray_pop   showpage   showpage
Dictionary stack:
   --dict:733/1123(ro)(G)--   --dict:0/20(G)--   --dict:105/200(L)--
Current allocation mode is local
Last OS error: Permission denied
Current file position is 5271
GPL Ghostscript 9.53.1: Unrecoverable error, exit code 1

我尝试过的各种场景:

  1. 检查发现生成的.rps文件确实存在
  2. 在代码更本地的目录中生成 .Rps 文件,即文档和当前工作环境或 RStudio。
  3. 尝试从 Windows 命令 运行 gsprint,在一个实例中指定绝对路径,在第二个实例中依赖 PATH。
  4. gswin64和gswin64c我都用过,分别是GUI和console,没什么区别。

通过阅读论坛,我知道如果要处理的文件不存在(undefinnedfilename,我认为!),ghostscript 会抛出一个 different 错误,所以我相信它看到 某些东西 ,但是这个正在抛出 invalidfileaccess 以及 OS 抛出“权限被拒绝”。因此我也尝试了:

  1. 运行 作为管理员在 Windows 命令上的上述努力(这是对 sudo 的 Windows 回答?!)。

我浏览了 R postscript() 文档,但我对 Examples:

下的条目感到有些困惑
## On Windows:
options(printcmd = 'redpr -P"\printhost\lw"')
postscript(file = tempfile("Rps."), print.it = TRUE)
# produce the desired graph(s)
dev.off()              # send plot file to the printer
## alternative using GSView 4.x :
options(printcmd = '/GhostGum/gsview/gsprint -query')

注意:我还没有尝试过:options(printcmd = 'redpr -P"\printhost\lw"'),首先,该行包含无效的 R 语法,其次,我有更多使用 GSView 的经验,所以我立即被吸引到替代方法。

有趣的是,gsprint -query 在查看 Ghostscript 指令时指的是“显示打印机设置对话框”。在命令行上调用它,它告诉我使用-ghostscript,这是我要求它做的..

总而言之,生成 EPS 文件的运气为零,但我感觉它是与 gswin64c 相关的命令参数。非常感谢任何帮助。

解决方案 感谢 KenS,如果不是他们的意见,我可能仍在进行故障排除。原来 Ghostscript 的版本是问题所在(见评论和回答)。为了生成用于发布的 EPS 图像,我在 R 中使用了另一种方法:

在 RStudio 控制台上(不在 RMarkdown 文档中):

library("cowplot")
#code for my ggplot2 objects here: image1Agg, image1Bgg, and image1Cgg
#for example

image1Cgg <- ggplot(combinedFrequenciesDF_ByClass, aes(x=reorder(displayDescription, -Frequency), y=Frequency)) + geom_bar(stat="identity") + theme_classic() + theme(text = element_text(size = 10), axis.text.x = element_text(angle = 45, hjust=1),  axis.text = element_text(color="black")) + xlab("First Prescription (Drug Class)") + ylab("Patients") + scale_y_continuous(expand = expansion(mult = c(0,.1)))

image1Grid <- plot_grid(image1Agg, image1Bgg, image1Cgg, labels= c("(a)","(b)","(c)"), label_size = 10, ncol = 1, vjust = 1, hjust=0.03)
image1Grid #in the RStudio console so it appears in the RStudio Plots window
ggsave("image1.eps", units="cm", width=15, height=20, device="eps")

在 Windows10 安装了最新的 GSView 和 Ghostscript,上面生成了一个 EPS 文件。我不会假装知道 ggsave 如何与 postscripts 一起工作,我只知道在我的 OS 上正确安装了这两个程序,它可以工作。我还发现 cow_plot 替代方案有效:

save_plot("image1.eps", image1Grid, base_height=7.9, base_width=5.9) #this is in inches

save_plot和ggsave都默认生成了15cm宽的EPS图片,左右留3cm。我希望这对其他人有帮助。

最新版本的 Ghostscript(不幸的是,您已经从 stdout 中删除了版本,但它是 (C) 2020,所以它 必须 是相当新的)已经改变了默认行为,因为安全漏洞。现在在 SAFER 模式下默认为 运行(之前默认为 NOSAFER)。这意味着不允许 PostScript 程序 open/read/write/delete 磁盘上的文件。

看起来您正在 运行ning 的文件在尝试写入文件时遇到无效文件访问错误。它试图写入“00002dc”的文件名并未作为 Ghostscript 命令行上的输出文件提及,这是 Ghostscript 推断应允许文件访问的唯一方式。基本上,如果文件没有在命令行上命名,并且您没有告诉 GS 它有权 read/write 到给定目录,那么它就不允许访问该文件。

您可以尝试使用 -dNOSAFER,我真的不知道如何将它添加到 gsprint,它不是 Ghostscript 的一部分,但可能(读取 gsprint.htm 文件)-选项“-dNOSAFER”可能有用。

虽然我真的不推荐将其作为 long-term 解决方案。