0

I'm trying to install terra package in R Google Colab, but it is not working.

It returns:

“installation of package ‘terra’ had non-zero exit status”

I tried the solutions found in here and here, i.e., trying to install dependencies like GDAL via system("apt install libudunits2-dev libgdal-dev libgeos-dev libproj-dev" ) but it does not work.

Is it possible to run terra in Google Colab (or even raster) packages?

kolrocket
  • 9
  • 3

1 Answers1

0

I was able to install terra on Google Colab using the code below. It did take about 5-10 minutes for the library to install.

  1. In Google Colab, select "Runtime", "Change runtime type" and select "R" under "Runtime type".

  2. Install terra per the instructions on the module's GitHub page:

install.packages("Rcpp")

Sys.setenv("R_REMOTES_NO_ERRORS_FROM_WARNINGS" = "true")
remotes::install_github("rspatial/terra")

#read in packages
library(terra)
  1. Test that the module is imported and working, the code block below is from the rspatial.org site:
## terra 1.7.36
# create an empty SpatRaster
r <- rast(ncol=10, nrow=10)
# assign values to cells
values(r) <- 1:ncell(r)
s <- r + 10
s <- sqrt(s)
s <- s * r + 5
values(r) <- runif(ncell(r))
r <- round(r)
r <- r == 1
r

Output is below:

class       : SpatRaster 
dimensions  : 10, 10, 1  (nrow, ncol, nlyr)
resolution  : 36, 18  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 
source(s)   : memory
name        : lyr.1 
min value   : FALSE 
max value   :  TRUE 

Additional code block:

x <- rast(ncol=36, nrow=18, xmin=-1000, xmax=1000, ymin=-100, ymax=900)
res(x)
## [1] 55.55556 55.55556

res(x) <- 100
res(x)

Output:

55.555555555555655.5555555555556
100100
  • Mine did not work :(, I got ```Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified)```. Then ```Warning message: “dependency ‘Rcpp’ is not available”```. Then ```Warning message in install.packages("terra",repos="https://rspatial.r-universe.dev"):"installation of package "terra" had non-zero exit status". Error in library(terra): there is no package called `terra`. Traceback: 1. library(terra).``` To do that I opened a new notebook and change to R like you said. Do you have any other tips? Thank you!!! – kolrocket Jul 25 '23 at 16:32
  • Odd, it worked yesterday, had the same issue as you just now. Went back and edited the code to install `terra` from source. Seems to work now, also put it up on [GitHub](https://github.com/ad17171717/Stack-Overflow-Answers/blob/main/Stack-Overflow-Answers/How_to_install_terra_raster_packages_in_R_Google_Colab%3F.ipynb) in case you want to try it. – Adrian Dolinay Jul 25 '23 at 20:09
  • Oh, so it was not only here! Thank you very much, this new approach from source worked :). – kolrocket Jul 26 '23 at 14:50