Windows 10下使用R端到端生成出版级EPS文件的实现与排错
背景
此前在配置完善的Linux机器上可顺利生成符合期刊出版要求的EPS矢量图,在全新安装的64位Windows 10(搭配R 4.02版本、RStudio)环境下,沿用旧的Postscript导出逻辑时反复报错。
排错过程与已验证无效的操作
最初的导出代码如下:
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环境变量,规避“Program Files”目录结构带来的路径转义问题
执行代码后RStudio控制台抛出如下警告:
Warning message: In dev.off() : error from postscript() in running: gsprint -ghostscript gswin64 C:\Users\yewro\AppData\Local\Temp\Rtmp0Yqsfg\Rps.17d065ef330
单独在Windows命令行执行上述调用命令时,可看到完整的报错输出:
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 1977 1 3 %oparray_pop 1840 0 3 %oparray_pop 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
为定位问题先后做了如下排查,所有操作均未解决报错:
- 确认生成的.Rps临时文件确实存在
- 调整.Rps文件的生成路径,换用Documents文件夹、RStudio当前工作目录存储文件
- 在Windows命令行测试gsprint调用,分别使用二进制绝对路径、依赖系统PATH变量两种方式
- 分别切换gswin64(GUI版)、gswin64c(控制台版)作为Ghostscript后端
- 以管理员身份在Windows命令行执行所有上述操作(等效于Linux下的sudo权限)
关键报错特征:根据Ghostscript的公开报错规则,待处理文件不存在时会抛出
undefinedfilename错误,与当前报错的invalidfileaccess+权限拒绝特征不符,说明Ghostscript可正常识别待处理的Postscript文件,问题根源不在文件路径或基础权限。
对照R官方文档中的Windows端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')
- 文档中
redpr的调用逻辑适用于网络打印场景,不符合本地生成EPS文件的需求 gsprint -query参数的原生作用是唤起打印机设置弹窗,实际命令行调用时提示必须指定-ghostscript参数,与原代码配置完全一致,无法绕过现有报错。
可行的替代导出方案
经根因定位,原流程报错的核心问题为Ghostscript版本与旧版gsprint工具的兼容性不匹配,无需继续调整printcmd参数、权限配置或路径设置,直接使用ggplot2生态内置的EPS导出逻辑即可稳定生成符合出版要求的矢量图。
需在RStudio控制台直接执行(不要在RMarkdown代码块内运行),流程如下:
library("cowplot") # 按业务逻辑生成ggplot对象,示例代码供参考 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 # 导出EPS文件 ggsave("image1.eps", units="cm", width=15, height=20, device="eps")
也可使用cowplot包自带的导出函数实现相同效果:
# 参数单位为英寸,匹配常规期刊排版尺寸 save_plot("image1.eps", image1Grid, base_height=7.9, base_width=5.9)
注:无需深究ggsave处理Postscript渲染的底层逻辑,只要在Win10上正确安装对应版本的GSView与Ghostscript,上述代码即可稳定导出符合投稿要求的EPS文件。两种导出方式默认生成的EPS会在图形左右两侧各预留约3cm的安全边距,适配多数期刊的排版规范。
内容的提问来源于stack exchange,提问作者Anthony Nash

