如何在Windows 10上端到端使用R编写EPS文件?

问题描述

这是为科学论文投稿而生成准备发布的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,并为避免使用目录结构和“程序文件”来避免字符混淆的烦恼,我将这两组二进制文件的路径都添加到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 Commmand上执行此操作,则会看到更多内容(正如我期望的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. 在比代码更本地的目录(即文档和当前工作环境或RStudio)中生成.Rps文件
  3. 试图从Windows命令运行gsprint,在一个实例中指定绝对路径,然后在第二个实例中依赖PATH。
  4. 我分别使用了gswin64和gswin64c,GUI和控制台。

从阅读论坛中,我知道如果要处理的文件不存在(我认为是unfinfinnedfilename),ghostscript会引发不同错误,因此我相信它会看到 ,但是此操作会引发无效文件访问,而操作系统会引发“权限被拒绝”。因此,我也尝试过:

  1. 管理员身份在Windows命令上运行上述操作(此Windows回答sudo吗?!)。

我已经浏览了R postscript()文档,并且对示例下的条目感到有些困惑:

## 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的经验,因此立即被替代方法吸引。

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

总的来说,运气好的情况下生成EPS文件,但我感觉它的命令参数与gswin64c有关。非常感谢您的帮助。

解决方 多亏了KenS,如果没有他们的投入,我可能仍会进行故障排除。原来是Ghostscript的版本是问题所在(请参阅评论和答案)。为了生成用于发布的EPS图像,我在R中使用了另一种方法

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

library("cowplot")
#code for my ggplot2 objects here: image1Agg,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,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")

在装有最新GSView和Ghostscript的Windows 10上,以上内容生成了EPS文件。我不会假装知道ggsave如何使用后记,我只知道在我的OS上正确安装了这两个程序,它可以工作。我还发现cow_plot替代方法有效:

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

save_plot和ggsave均生成认情况下15厘米宽的EPS图像,该图像在左右边缘留有3厘米。我希望这对其他人有帮助。

解决方法

Ghostscript的最新版本(很遗憾,您已从stdout中删减了该版本,但由于是(C)2020,所以它必须是最近的版本)已更改了默认行为,因为存在安全漏洞。现在默认设置是在SAFER模式下运行(以前的默认设置是NOSAFER)。这意味着不允许PostScript程序打开/读取/写入/删除磁盘上的文件。

看起来您正在运行的文件在尝试写入文件时遇到了invalidfileaccess错误。 Ghostscript命令行上的输出文件未提及要写入'00002dc'的文件名,这是Ghostscript推断应该允许文件访问的唯一方法。基本上,如果文件不是在命令行上命名的,并且您没有告诉GS它具有读取/写入给定目录的权限,则不允许访问该文件。

您可以尝试使用-dNOSAFER,我不知道如何将其添加到gsprint中,这不是Ghostscript的一部分,但可能(读取gsprint.htm文件)-option“ -dNOSAFER”可能有效。 / p>

我不会真的建议您将其作为长期解决方案。