Unable to load tidycensus and map view packages on Macbook

811 views Asked by At

I installed the packages tidycensus and mapview in RStudio but I get the following error when I try load either one:

library(tidycensus)

Error: package or namespace load failed for ‘tidycensus’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so': dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so, 6): Library not loaded: /usr/lib/libpq.5.dylib Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so Reason: image not found

library(mapview)

Error: package or namespace load failed for ‘mapview’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so': dlopen(/Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so, 6): Library not loaded: /usr/lib/libpq.5.dylib Referenced from: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so Reason: image not found

I've tried this solution but it is not solving my problem.

1

There are 1 answers

1
Cris Silva On

This issue indeed happened to me today when upgraded to Big Sur. After researching, finally I found a solution here:

copy libpq.5.dylib to /usr/lib/libpq.5.dylib

Looks like macOS is keeping libraries on /usr/lib closed. Basically is needed to point back /usr/lib/libpq.5.dylib against another libpq, like the one you get by installing it with Homebrew.

This is what worked for me, first uninstall sf and rgdal packages in R and use HomeBrew to install libpq if needed:

$ brew install libpq

Check if library is pointing itself:

$ otool -l /usr/local/Cellar/libpq/13.0/lib/libpq.5.dylib | fgrep -A2 LC_ID_DYLIB

If not, point it against itself:

$ sudo install_name_tool -id /usr/local/Cellar/libpq/13.0/lib/libpq.5.dylib /usr/local/Cellar/libpq/13.0/lib/libpq.5.dylib

And link the new path of libpq to each R package binary that references to it:

$ sudo install_name_tool -change /usr/lib/libpq.5.dylib [new_path] [path_to_binary]

In this case, for sf and rgdal:

$ sudo install_name_tool -change /usr/lib/libpq.5.dylib /usr/local/Cellar/libpq/13.1/lib/libpq.5.dylib /Library/Frameworks/R.framework/Versions/4.0/Resources/library/sf/libs/sf.so

$ sudo install_name_tool -change /usr/lib/libpq.5.dylib /usr/local/Cellar/libpq/13.1/lib/libpq.5.dylib /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/libs/rgdal.so