Loading packages in R and RStudio gives image not found error - r

When trying to load packages in RStudio or R, I get "image not found" errors, e.g.:
> install.packages("Rcpp")
Installing package into ‘/Users/blah/Library/R/4.0/library’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/bin/macosx/contrib/4.0/Rcpp_1.0.6.tgz'
Content type 'application/x-gzip' length 3203922 bytes (3.1 MB)
==================================================
downloaded 3.1 MB
The downloaded binary packages are in
/var/folders/q5/9z9mngy52lx6gbjtc_tg5w2wwn7x6k/T//RtmpZwoJ1y/downloaded_packages
> library(Rcpp)
Error: package or namespace load failed for ‘Rcpp’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/blah/Library/R/4.0/library/Rcpp/libs/Rcpp.so':
dlopen(/Users/blah/Library/R/4.0/library/Rcpp/libs/Rcpp.so, 6): Library not loaded: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libR.dylib
Referenced from: /Users/blah/Library/R/4.0/library/Rcpp/libs/Rcpp.so
Reason: image not found
> detach("package:Rcpp", unload = TRUE)
Error in detach("package:Rcpp", unload = TRUE) : invalid 'name' argument
The same thing happens for many other packages in the User Library e.g. sp, kernlab, gplots, gtools (but not all of them e.g. crayon loads ok). Packages in the System library load fine.
Similar things happen in R, but not with the same set of packages e.g. Rcpp works here but kernlab still does not:
> library(Rcpp)
> detach("package:Rcpp", unload = TRUE)
> library(kernlab)
Error: package or namespace load failed for ‘kernlab’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/blah/Library/R/4.0/library/kernlab/libs/kernlab.so':
dlopen(/Users/blah/Library/R/4.0/library/kernlab/libs/kernlab.so, 6): Library not loaded: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
Referenced from: /Users/blah/Library/R/4.0/library/kernlab/libs/kernlab.so
Reason: image not found
> detach("package:kernlab", unload = TRUE)
Error in detach("package:kernlab", unload = TRUE) :
invalid 'name' argument
I'm running on MacOS 11.2.3 and recently updated XCode (12.4) and CommandLineTools - this seemed to break a few things so I suspect there may be a connection. R is version 4.0.4 and RStudio is version 1.4.1106.
How do I go about trying to fix this?

I fixed this by using brew to uninstall R and Rstudio and reinstalling - but it only worked if I installed R from the cask, although the versions of R and RStudio remained the same.
The main difference I can see is that my new Makeconf file has a -mmacosx-version-min=10.13 flag set for CC, CXX, CXX11 etc (compiler is clang).

Related

R Fails to Library Packages

I am having trouble on getting R to library packages to use tools such as ggplot. I have reinstalled R to try and remedy the issue to no avail. Currently I can install packages normally install.packages(ggplot2) I am later given the potentially key message
Installing package into ‘/Users/jet/Library/R/arm64/4.1/library’
(as ‘lib’ is unspecified)
Installation returns to normal and I select my CRAN mirror and am left with
The downloaded binary packages are in
/var/folders/1w/nbtkj1314rnbdv3djd0vm7zw0000gn/T//RtmpOkNNTZ/downloaded_packages
Later when I try and library the packageslibrary(ggplot) I receive the following error.
Error: package or namespace load failed for ‘ggplot2’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/jet/Library/R/arm64/4.1/library/colorspace/libs/colorspace.so':
dlopen(/Users/jet/Library/R/arm64/4.1/library/colorspace/libs/colorspace.so, 0x0006): Library not loaded: /opt/homebrew/opt/r/lib/R/lib/libR.dylib
Referenced from: /Users/jet/Library/R/arm64/4.1/library/colorspace/libs/colorspace.so
Reason: tried: '/opt/homebrew/opt/r/lib/R/lib/libR.dylib' (no such file), '/usr/local/lib/libR.dylib' (no such file), '/usr/lib/libR.dylib' (no such file)
What should I do to resolve this issue?

Failing to load rgdal package in R

I'm having trouble loading rgdal in RStudio. I tried the usual command install.packages and when prompted with
"Do you want to install from sources the package which needs compilation (Yes/no/cancel)"
I've tried both yes and no. However I continue to get a cryptic error message when I try to load the library for use. It is:
Error: package or namespace load failed for ‘rgdal’ in dyn.load(file, DLLpath = >DLLpath, ...):
unable to load shared object >'/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/libs/rgdal.so':
dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/libs/rgdal.so, 6): Symbol not found: _sqlite3_column_table_name
Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/libs/rgdal.so (which was built for Mac OS X 10.13)
Expected in: /usr/lib/libsqlite3.dylib
in /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/libs/rgdal.so

R: cannot install packages after downgrading from 3.5 to 3.4. Error in dyn.load(file, DLLpath = DLLpath, ...)

I had installed R 3.5.1 as detailed in here, but I had some problem with some packages (namely rJava and XML), so I have decided to uninstall R 3.5.1 and downgrade to R 3.4.4 from the Ubuntu 18.04 repositories.
I did that successfully, but now, when I try to reinstall any CRAN package inside R with, for example:
install.packages("ggplot2")
library(ggplot2)
I get the following errors:
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so':
/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so: undefined symbol: DATAPTR
ERROR: lazy loading failed for package ‘ggplot2’
* removing ‘/usr/local/lib/R/site-library/ggplot2’
* restoring previous ‘/usr/local/lib/R/site-library/ggplot2’
The downloaded source packages are in
‘/tmp/RtmpFKBMdd/downloaded_packages’
Warning message:
In install.packages("ggplot2") :
installation of package ‘ggplot2’ had non-zero exit status
>
>
>
>
> library(ggplot2)
Error: package or namespace load failed for ‘ggplot2’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so':
/usr/local/lib/R/site-library/Rcpp/libs/Rcpp.so: undefined symbol: DATAPTR
In addition: Warning message:
package ‘ggplot2’ was built under R version 3.5.1
Any clue how to solve this? Thanks
OK this script to remove all user installed packages works like a charm if anyone is interested. It was taken from here.
# Shamlessly stolen from:
# https://www.r-bloggers.com/how-to-remove-all-user-installed-packages-in-r/
# create a list of all installed packages
ip <- as.data.frame(installed.packages())
head(ip)
# if you use MRO, make sure that no packages in this library will be removed
ip <- subset(ip, !grepl("MRO", ip$LibPath))
# we don't want to remove base or recommended packages either\
ip <- ip[!(ip[,"Priority"] %in% c("base", "recommended")),]
# determine the library where the packages are installed
path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
pkgs.to.remove <- ip[,1]
head(pkgs.to.remove)
# remove the packages
sapply(pkgs.to.remove, remove.packages, lib = path.lib)

Incompatible library version RSiena

I'm trying to load the RSiena package into R, but keep getting this error message:
Error: package or namespace load failed for ‘RSiena’:
.onLoad failed in loadNamespace() for 'tcltk', details:
call: dyn.load(file, DLLpath = DLLpath, ...)
error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/tcltk/libs/tcltk.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/tcltk/libs/tcltk.so, 10): Library not loaded: /opt/X11/lib/libfontconfig.1.dylib
Referenced from: /usr/local/lib/libtk8.6.dylib
Reason: Incompatible library version: libtk8.6.dylib requires version 11.0.0 or later, but libfontconfig.1.dylib provides version 10.0.0
I tried installing three ways:
install.packages("RSiena") , install.packages("RSiena", repos="http://R-Forge.R-project.org") , and installing straight from tgz file (from https://cran.r-project.org/web/packages/RSiena/index.html).
The first two installation methods do not give any error and say The downloaded binary packages are in /var/folders/mk/__r_sbzn5mx88x04mrlycsg00000gq/T//RtmpbxlMY8/downloaded_packages.The third option gives this message Error in install.packages : type == "both" cannot be used with 'repos = NULL'
None allow me to load the package. I'm working with R version 3.4.3 (2017-11-30) in RStudio Version 1.1.423 on Mac OS X El Capitan (version 10.11.6).
I've tried uninstalling and reinstalling. I'm not sure how else to troubleshoot this issue.
The most recent version (RSiena v.1.2.26) of rsiena addresses tcltk issues, and is now available on Github here: https://github.com/snlab-nl/rsiena.
SOLUTION. I installed XQuartz from xquartz.macosforge.org and now the package loads successfully.

When loading a package, R is unable to load libRlapack.dylib

I recently updated my R (on Mac) from Version 3.0.2 to 3.1.1. I tried to load a package by using library(WRScpp)
But I got an error as shown below:
Error in dyn.load(file, DLLpath = DLLpath, ...) :
unable to load shared object '/Library/Frameworks/R.framework/Versions/3.1/Resources/library/WRScpp/libs/WRScpp.so':
dlopen(/Library/Frameworks/R.framework/Versions/3.1/Resources/library/WRScpp/libs/WRScpp.so, 6): Library not loaded: /Library/Frameworks/R.framework/Versions/3.0/Resources/lib/libRlapack.dylib
Referenced from: /Library/Frameworks/R.framework/Versions/3.1/Resources/library/WRScpp/libs/WRScpp.so
Reason: image not found
Error: package or namespace load failed for ‘WRScpp’
It seems that R tried to load libRlapack.dylib by looking for it in the wrong directory (it looked for it in the Version 3.0 directory). I wonder what can be done to direct R to search the correct directory?

Resources