AlejandroDGR
AlejandroDGR

Reputation: 188

Using R library sf in Google Colab

I'm trying to plot a choropleth map in Google Colab using R.

After searching the web for hints, I have found that using the library sf is a must. However, I find it difficult to install and call it on Colab.

If I write:

install.packages("sf")

After some minutes, the following error arises:

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

also installing the dependencies ‘proxy’, ‘e1071’, ‘wk’, ‘classInt’, ‘Rcpp’, ‘s2’, ‘units’


Warning message in install.packages("sf"):
“installation of package ‘units’ had non-zero exit status”
Warning message in install.packages("sf"):
“installation of package ‘sf’ had non-zero exit status”

Nonetheless, I have found a solution for this in GitHub (sf installation on google colab throws errors #1572):

system('sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable')
system('sudo apt-get update')
system('sudo apt-get install libudunits2-dev libgdal-dev libgeos-dev libproj-dev')
install.packages('sf')

No error this time.

Nevertheless, when calling the library (library(sf)), a new error arises:

Error: package or namespace load failed for ‘sf’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/usr/local/lib/R/site-library/sf/libs/sf.so':
  /usr/lib/libgdal.so.26: undefined symbol: GEOSMakeValid_r
Traceback:

1. library(sf)
2. tryCatch({
 .     attr(package, "LibPath") <- which.lib.loc
 .     ns <- loadNamespace(package, lib.loc)
 .     env <- attachNamespace(ns, pos = pos, deps, exclude, include.only)
 . }, error = function(e) {
 .     P <- if (!is.null(cc <- conditionCall(e))) 
 .         paste(" in", deparse(cc)[1L])
 .     else ""
 .     msg <- gettextf("package or namespace load failed for %s%s:\n %s", 
 .         sQuote(package), P, conditionMessage(e))
 .     if (logical.return && !quietly) 
 .         message(paste("Error:", msg), domain = NA)
 .     else stop(msg, call. = FALSE, domain = NA)
 . })
3. tryCatchList(expr, classes, parentenv, handlers)
4. tryCatchOne(expr, names, parentenv, handlers[[1L]])
5. value[[3L]](cond)
6. stop(msg, call. = FALSE, domain = NA)

Any idea on how to fix it?

Thanks!

Upvotes: 0

Views: 1229

Answers (1)

AlejandroDGR
AlejandroDGR

Reputation: 188

Thanks to a GitHub user, I have found the solution (Calling sf on google colab throws errors)

You should install sf without adding ubuntugis repository in a fresh collab notebook :

system("apt-get -y update")
system("apt-get install -y libudunits2-dev libgdal-dev libgeos-dev libproj-dev")
install.packages("sf")
library("sf")

No error arises then:

Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

also installing the dependencies ‘proxy’, ‘e1071’, ‘wk’, ‘classInt’, ‘Rcpp’, ‘s2’, ‘units’


Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0; sf_use_s2() is TRUE

Upvotes: 2

Related Questions