rmacosrstudiogfortrandylib

Why does Rstudio library fail due to libgfortran dylib, but works in base R


The problem

Using Rstudio, I installed edgeR using BiocManager::install("edgeR") which went fine. When running library(edgeR), I get an error involving some libgfortran.5.dylib:

Loading required package: limma
Error: package or namespace load failed for ‘edgeR’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/Users/sbouhad2/Library/R/arm64/4.3/library/edgeR/libs/edgeR.so':
  dlopen(/Users/sbouhad2/Library/R/arm64/4.3/library/edgeR/libs/edgeR.so, 0x0006): Library not loaded: /opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0/libgfortran.5.dylib
  Referenced from: <7A0E0743-72B0-3B24-A4BB-8E34A69B6095> /Users/sbouhad2/Library/R/arm64/4.3/library/edgeR/libs/edgeR.so
  Reason: tried: '/opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0/libgfortran.5.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0/libgfortran.5.dylib' (no such file), '/opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0/libgfortran.5.dylib' (no such file), '/usr/local/lib/libgfortran.5.dylib' (no such file), '/usr/lib/libgfortran.5.dylib' (no such file, not in dyld cache)

When I open a terminal in the default MacOS application or in Rstudio and run R followed by library(edgeR), all goes well and I can run functions within the package. I have no idea where to look or how to solve this. I am also not familiar with the technicalities w.r.t. x86 and arm64.

Session info

For Rstudio, after running the failing library command

> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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

time zone: Europe/Amsterdam
tzcode source: internal

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

other attached packages:
[1] limma_3.55.7

loaded via a namespace (and not attached):
[1] compiler_4.3.0  tools_4.3.0     rstudioapi_0.14 Rcpp_1.0.10    
[5] grid_4.3.0      locfit_1.5-9.7  lattice_0.21-8

For base R, after running the working library command using a terminal

> sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.3.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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

time zone: Europe/Amsterdam
tzcode source: internal

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

other attached packages:
[1] edgeR_3.41.8 limma_3.55.7

loaded via a namespace (and not attached):
[1] compiler_4.3.0 Rcpp_1.0.10    grid_4.3.0     locfit_1.5-9.7 lattice_0.21-8

I can't see any difference except maybe that BLAS has a different path.


Relevant (?) background info

I installed gcc and gfortran using (arch -arm64) homebrew to compile Rcpp packages on a MacOS. After a long search I found out that I need to create ~/.R/Makevars which now comprises of:

CC=gcc-13
CXX=g++-13
FLIBS   =-L/usr/local/gfortran/lib
F77     = /usr/local/gfortran/bin/gfortran
FC      = /usr/local/gfortran/bin/gfortran

With this modification, I could build Rcpp packages. Since then, edgeR fails to load within Rstudio.


I tried dyn.load some of the paths, such as dyn.load("/opt/gfortran/lib/gcc/aarch64-apple-darwin20.0/12.2.0/libgfortran.5.dylib"). That didn't work in Rstudio nor in base R.

Also, library of the Rcpp based package works and I can execute the code in R and Rstudio.

I don't have any other idea what I could do.


Solution

  • Guided by the comments of @user2554330 and others, I installed gfortran from https://mac.r-project.org/tools/. After restarting RStudio, library(edgeR) works!