如何修复在R中出现的“failed to load cairo DLL”错误?

17

我遇到了一个会停止代码执行的警告信息。

Saving 8.04 x 5.67 in image
Warning messages:
1: In dev(file = filename, width = dim[1], height = dim[2], ...) :
  unable to load shared object '/usr/local/Cellar/r/3.5.0_1/lib/R/library/grDevices/libs//cairo.so':

  dlopen(/usr/local/Cellar/r/3.5.0_1/lib/R/library/grDevices/libs//cairo.so, 6): image not found
2: In dev(file = filename, width = dim[1], height = dim[2], ...) :
  failed to load cairo DLL

这是生成警告的代码


library(tidyverse)
library(cowplot)

d <- sample_n(diamonds, 50)
g <- ggplot(d, aes(carat, price)) + geom_point()
gg <- list(g,g,g)

plot_grid(plotlist = gg, nrow=1) %>% 
  cowplot::ggsave(filename = paste0("~/Desktop/", paste0("testing", ".svg")), device = cairo_ps)

我该如何解决这个错误?

顺便说一句,我是个新手。

编辑: 这是我的会话信息。不确定它是否有用。我包含它以防万一。

sessionInfo()

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin17.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] cowplot_0.9.2     forcats_0.3.0     stringr_1.3.1     dplyr_0.7.5      
 [5] purrr_0.2.5       readr_1.1.1       tidyr_0.8.1       tibble_1.4.2     
 [9] ggplot2_2.2.1     tidyverse_1.2.1   doParallel_1.0.11 iterators_1.0.9  
[13] foreach_1.4.4     lmerTest_3.0-1    lme4_1.1-17       Matrix_1.2-14    

loaded via a namespace (and not attached):
 [1] tidyselect_0.2.4  reshape2_1.4.3    splines_3.5.0     haven_1.1.1      
 [5] lattice_0.20-35   argparse_1.1.1    colorspace_1.3-2  getopt_1.20.2    
 [9] yaml_2.1.19       rlang_0.2.1       nloptr_1.0.4      pillar_1.2.3     
[13] foreign_0.8-70    glue_1.2.0        readxl_1.1.0      modelr_0.1.2     
[17] bindrcpp_0.2.2    bindr_0.1.1       plyr_1.8.4        cellranger_1.1.0 
[21] munsell_0.4.3     findpython_1.0.3  gtable_0.2.0      rvest_0.3.2      
[25] codetools_0.2-15  psych_1.8.4       labeling_0.3      broom_0.4.4      
[29] proto_1.0.0       Rcpp_0.12.17      scales_0.5.0      jsonlite_1.5     
[33] mnormt_1.5-5      hms_0.4.2         stringi_1.2.2     numDeriv_2016.8-1
[37] grid_3.5.0        cli_1.0.0         tools_3.5.0       magrittr_1.5     
[41] lazyeval_0.2.1    crayon_1.3.4      pkgconfig_2.0.1   MASS_7.3-49      
[45] xml2_1.2.0        lubridate_1.7.4   rstudioapi_0.7    assertthat_0.2.0 
[49] minqa_1.2.4       httr_1.3.1        R6_2.2.2          nlme_3.1-137     
[53] compiler_3.5.0   

1
我不确定macOS...但是你的系统中是否安装了cairo? - Bruno Pinheiro
1
你有查看过这个答案吗? - SeGa
非常感谢您的回复!我已经查阅了许多在线资源,试图解决这个问题。我在电脑上安装了homebrew中的cairo。我还重新安装了我的Quartz版本。 - Nicholas Hayden
3个回答

14

Homebrew版本的R不再支持cairo (更多信息请看这里),因此如果您通过brew install r安装R,则基本R中的svg()cairo_pdf()cairo_ps()等图形设备将无法工作。您需要使用其他图形设备(例如pngjpegpdf)。如果您需要SVG输出,则ggsave()需要svglite包,并使用svglite::svglite()将您的图表保存为SVG文件。

如果确实要使用Homebrew来安装R,我建议使用brew cask install r


7

我曾经遇到同样的问题,我的电脑是MacOS。根据CRAN的说法,这个问题与XQuartz相关,因为它不再是OS X的一部分了。我安装了XQuartz后,基本上所有与Cairo有关的问题都得到了解决。


3
我遇到了同样的“无法加载cairo DLL”错误。在我的Mac OS上安装XQuartz解决了这个问题。谢谢! - tlqs
6
2022年5月,我在我的M1 Mac上使用了以下命令成功安装:brew install --cask xquartz - Peter Diakumis

6

我只能提供一个Windows机器,但是以下步骤适用于保存你的cowplot为.svg.eps格式(在安装了svglite包之后):

# install.packages(svglite)
library(tidyverse)
library(cowplot)

d <- sample_n(diamonds, 50)
g <- ggplot(d, aes(carat, price)) + geom_point()
gg <- list(g,g,g)

plot_grid(plotlist = gg, nrow=1)
ggsave(filename = "testing.svg")
ggsave(filename = "testing.eps")

当然,您可以根据需要将文件名修改为像"〜/桌面/testing.svg"这样的内容,具体取决于您要将绘图保存到哪里。请注意,默认情况下ggsave会保存最后一个活动的绘图,您指定的文件名的结尾应自动触发正确的设备。


是的!这对我起作用了!由于某些原因 - 设置 device = cairo_ps 是主要问题。如果将来有人阅读这篇文章,我猜只需通过 .svg.etc 结尾让 ggsave 的内部工作处理您的文件名。 - Nicholas Hayden

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接