I’m encountering an issue when using the terra package in R in visual code within a Conda environment. When I run R from the interactive terminal (e.g., directly launching R or using the R extension in VSCode), I get the following error when assigning a CRS to a raster:
GDAL Error
r <- rast(nrows=10, ncols=10, xmin=0, xmax=10, ymin=0, ymax=10, crs="EPSG:4326")
Error: [rast] empty srs
In addition: Warning message:
In new_CppObject_xp(fields$.module, fields$.pointer, ...) :
GDAL Error 1: PROJ: proj_create_from_database: Cannot find proj.db
However, when I run commands via a bash script or directly in the bash terminal, I dont have problems and I can verify that the proj.db
file exists and that the PROJ_LIB
environment variable is correctly set.
Here’s what I’ve tried so far:
Finding proj.db
with find $CONDA_PREFIX -name proj.db
and setting Sys.setenv(PROJ_LIB=...)
in R.
Confirming that file.exists(file.path(Sys.getenv("PROJ_LIB"), "proj.db"))
returns TRUE
both in bash and inside R.
Restarting the R session multiple times.
Reinstalling the proj
and gdal
packages via Conda.
Despite this, the error persists only when running R interactively or inside VSCode, but not when running commands from bash or shell scripts.
Does anyone know why the interactive environment and bash might behave differently with respect to environment variables and locating proj.db
? How can I ensure that R correctly recognizes proj.db
in all contexts?
Thanks in advance for any help!