How to fix "failed to load cairo DLL" in R?
Asked Answered
N

3

20

I am running into this warning message that will stop execution.

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

This is the code that generates that warning for me

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)

How do I go about fixing this error?

P.s. I am a novice.

EDIT: This is my session information. I am not sure if this would be helpful. I have included it just in case.

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   
Neolatin answered 8/6, 2018 at 19:42 Comment(3)
I'm not sure about macOS... but do you have cairo installed in your system?Rosenblatt
Have you checked this answer?Swanson
Thank you for your responses! I have looked through a multitude of online resources trying to debug this issue. I do have cairo installed on my computer - through homebrew. I also have reinstalled my versions of Quartz.Neolatin
D
6

I only have a Windows machine to offer, but the following worked for both saving a .svg and .eps of your cowplot (after installing the package 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")

You can of course modify the filename to something like "~/Desktop/testing.svg", depending on where you want to safe your plot. Note that ggsave per default saves the last active plot, and the ending of the filename you specify should automatically trigger the right device.

Disapprove answered 12/6, 2018 at 15:12 Comment(1)
Yes! This worked for me! I was for some reason - setting device = cairo_ps was the main problem. If anyone in the future is reading this, I guess just let the internal workings of ggsave handle your filename by the ending .svg or .etc.Neolatin
M
13

The Homebrew version of R no longer supports cairo (more info here), so graphical devices like svg(), cairo_pdf(), and cairo_ps() in base R won't work if you install R via brew install r. You have to use other graphical devices (e.g., png, jpeg, pdf). If you need SVG output, ggsave() requires the svglite package and uses svglite::svglite() to save your plot to an SVG file.

If you do want to use Homebrew to install R, I recommend brew install --cask r.

Mainsail answered 19/6, 2018 at 5:2 Comment(1)
This error also happens with the official R directly from CRAN.Rudelson
C
9

I had same problem and I use MacOS. I found it is XQuartz related "since it is no longer part of OS X", according to CRAN. I installed XQuartz and basically all Cairo related problems are addressed.

Canasta answered 26/9, 2021 at 23:26 Comment(3)
Had the same "failed to load cairo DLL" error. Installing XQuartz (on Mac OS) solved the issue for me. Thanks!Gladiolus
This worked for me on my M1 Mac in May 2022: brew install --cask xquartzAbrasion
Can confirm, you can just install this on macOS (I have an M2 processor), especially if don't have or want brew. Site: xquartz.org.Southwesterly
D
6

I only have a Windows machine to offer, but the following worked for both saving a .svg and .eps of your cowplot (after installing the package 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")

You can of course modify the filename to something like "~/Desktop/testing.svg", depending on where you want to safe your plot. Note that ggsave per default saves the last active plot, and the ending of the filename you specify should automatically trigger the right device.

Disapprove answered 12/6, 2018 at 15:12 Comment(1)
Yes! This worked for me! I was for some reason - setting device = cairo_ps was the main problem. If anyone in the future is reading this, I guess just let the internal workings of ggsave handle your filename by the ending .svg or .etc.Neolatin

© 2022 - 2024 — McMap. All rights reserved.