在Google Colab的R内核中无法安装exactextractr包的问题
问题场景
在Google Colab的原生R内核Notebook中,基础R代码可正常运行:
a <- 1 print(a)
但执行install.packages("exactextractr")安装包时失败,报错显示依赖包units、sf安装存在非零退出状态,最终导致exactextractr无法安装。调用library(exactextractr)时提示包不存在。
安装包的错误输出
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)also installing the dependencies ‘proxy’, ‘e1071’, ‘wk’, ‘sp’, ‘terra’, ‘classInt’, ‘s2’, ‘units’, ‘Rcpp’, ‘raster’, ‘sf’
Warning message in install.packages("exactextractr"):
“installation of package ‘units’ had non-zero exit status”
Warning message in install.packages("exactextractr"):
“installation of package ‘sf’ had non-zero exit status”
Warning message in install.packages("exactextractr"):
“installation of package ‘exactextractr’ had non-zero exit status”
调用包的错误信息
library(exactextractr)
Error in library(exactextractr): there is no package called ‘exactextractr’
Traceback:
- library(exactextractr)
问题根源
Google Colab默认的R运行环境缺少units、sf这类地理空间R包所需的系统级底层依赖库:
units包需要libudunits2-devsf包需要libgdal-dev、libgeos-dev、libproj-dev
这些库是编译对应R包的必要条件,缺失会导致编译失败,触发非零退出状态的警告。
解决方法
先安装系统级依赖,再安装R包:
- 执行以下代码安装系统依赖库:
system("sudo apt-get update && sudo apt-get install -y libudunits2-dev libgdal-dev libgeos-dev libproj-dev")
- 重新安装
exactextractr包:
install.packages("exactextractr")
- 验证安装成功:
library(exactextractr)
内容的提问来源于stack exchange,提问作者sermomon

