terra not reading .nc files (GDAL error 4) - r

I'm trying to open a .nc file with terra, but I'm getting the following error:
'' not recognised as a supported file format. (GDAL error 4)
Reproducible example here (16mb nc file):
if (!file.exists("HadISST_ice.nc")) {
download.file("https://www.metoffice.gov.uk/hadobs/hadisst/data/HadISST_ice.nc.gz","HadISST_ice.nc.gz")
R.utils:::gunzip("HadISST_ice.nc.gz")
}
library(terra)
hadISST <- rast('HadISST_ice.nc')
The file opens with raster::brick, but not with terra:rast, which if I understand correctly from #robert-hijmnan answer is because raster uses ncdf4 while terra uses GDAL.
In GDAL the .nc file opens with no issues via terminal:
% gdalinfo HadISST_ice.nc
Driver: netCDF/Network Common Data Format
Files: HadISST_ice.nc
Size is 512, 512
But I can't read the file with terra without throwing a GDAL error 4. Session info below:
print(sessionInfo())
R version 4.2.0 (2022-04-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.4
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] terra_1.5-34
loaded via a namespace (and not attached):
[1] compiler_4.2.0 tools_4.2.0 Rcpp_1.0.8.3 codetools_0.2-18
and GDAL:
% gdal-config --version
GDAL 3.5.0, released 2022/05/10

edit: the problem seems to be related to be a MacOS specific issue with terra not being installed with netCDF drivers:
R terra gdal version incorrect, cannot read .nc (GDAL error 4)
installing the development version of terra solves the problem:
install.packages('terra', repos='https://rspatial.r-universe.dev’)

Related

Error: package or namespace load failed occurrs in RStudio but not in R console

While working in a conda environment, I have been observing an unexpected behavior with R>=4.1.3. Namely, in terminal I can launch R and load GDAL-based libraries
R version 4.1.3 (2022-03-10) -- "One Push-Up"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
... STANDARD R MESSAGE ...
> library(sf)
Linking to GEOS 3.11.1, GDAL 3.6.2, PROJ 9.1.0; sf_use_s2() is TRUE
but in RStudio I get the error below
R version 4.1.3 (2022-03-10) -- "One Push-Up"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
... STANDARD R MESSAGE ...
> library(sf)
Error: package or namespace load failed for ‘sf’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/ivan/miniconda3/envs/r41_dev/lib/R/library/sf/libs/sf.so':
/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.30' not found (required by /home/ivan/miniconda3/envs/r41_dev/lib/R/library/sf/libs/../../../../libgdal.so.32)
Even though the behavior in terminal suggested that the package libstdc++ was installed, I have reinstalled it from the Gnome repo. The package is definitely installed on my machine.
apt list --installed | grep libstdc++
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
libstdc++-8-dev/focal,now 8.4.0-3ubuntu2 amd64 [installed,automatic]
libstdc++-9-dev/focal-updates,focal-security,focal-security,now 9.4.0-1ubuntu1~20.04.1 amd64 [installed,automatic]
libstdc++6/focal,now 11.1.0-1ubuntu1~20.04 amd64 [installed]
libstdc++6/focal,now 11.1.0-1ubuntu1~20.04 i386 [installed,automatic]
The Rstudio version is
RStudio 2022.07.2+576 "Spotted Wakerobin" Release (e7373ef832b49b2a9b88162cfe7eac5f22c40b34, 2022-09-06) for Ubuntu Bionic
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36
And here the session info
> sessionInfo()
R version 4.1.3 (2022-03-10)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS
Matrix products: default
BLAS/LAPACK: /home/ivan/miniconda3/envs/r41_dev/lib/libopenblasp-r0.3.21.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=de_DE.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=de_DE.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=de_DE.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.1.3 magrittr_2.0.3 class_7.3-21 DBI_1.1.3 tools_4.1.3 units_0.8-1
[7] proxy_0.4-27 Rcpp_1.0.10 KernSmooth_2.23-20 grid_4.1.3 e1071_1.7-13 classInt_0.4-8
I have been using conda environments with RStudio for a while and I have come across multiple challenges but I don't seem capable to come around this one.
Can anyone help me understand how come RStudio cannot access this package while R can access it from terminal?
As as side note, the problem does not occur with R=4.0.5 and, to the best of my knowledge, did not occur with R=4.1 either. I certainly have update apt multiple times, but I cannot recall any major update to my system.
EDIT TO ADDRESS THE POINTS RAISED BY #merv IN COMMENTS
I have created several environments with different R versions in the attempt to frame and solve this problem, without success.
I've now followed these steps:
check conda version with conda -V, returns conda 22.11.1
create a fresh environment with conda create -n r41_test -c conda-forge r-base=4.1.3 r-sf -y
activate the environment with conda activate r41_test
1 save the conda list output to file and share it through this link
launch R from terminal
load the sf library with library(sf), the library loads without issues
close R with q() and type n to avoid workspace image to be saved
launch RStudio by typing rstudio (from the activated environment)
load library sf with library(sf) from the R console in RStudio, the library fails loading with the same error reported above.
While running this test, I have noticed something I had not noticed before: when the conda environment at step 2, the following message was printed, but the environment was successfully create. I could also activate it, launch R from terminal, and load the library despite the SafetyError.
Downloading and Extracting Packages
Preparing transaction: done
Verifying transaction: |
SafetyError: The package for r-base located at /home/ivan/miniconda3/pkgs/r-base-4.1.3-h2f963a2_5
appears to be corrupted. The path 'lib/R/doc/html/packages.html'
has an incorrect size.
reported size: 3061 bytes
actual size: 55120 bytes
done
Previous my post, I have tried this procedure also on another laptop running Ubuntu 20.04. I barely use that machine so it may serve as an 'independent control'. I am not sure whether the SafetyError was thrown also there, but RStudio failed to load the package also on that machine.

gdal-config not found while installing packages, Mac M1 Ventura

I am trying to install some packages to work with spatial data such as sf, stars, rgdal, on a Mac Book Air M1 with Ventura.
To do so, I need to have gdal installed to my compute, which I have. My problem is that when I try to install a library (see example with sf here below), the gdal-config file is not found.
But I have it on my computer, so my question is :
Is there a way to indicate the path to R to find that file ?
I tried to to uninstall and reinstall gdal with homebrew, I have proj that is also a dependency rightly installed.
I need to install from sf from sources as it is needed to install the stars package.
(base)#MacBook-Air ~ % echo $PATH
/opt/homebrew/bin:/opt/anaconda3/bin:/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
(base)#MacBook-Air ~ % whereis gdal-config
gdal-config: /opt/homebrew/bin/gdal-config /opt/homebrew/share/man/man1/gdal-config.1
(base)#MacBook-Air ~ % gdal-config
Usage: gdal-config [OPTIONS]
Options:
[--prefix[=DIR]]
[--libs]
[--dep-libs]
[--cflags]
[--datadir]
[--version]
[--ogr-enabled]
[--gnm-enabled]
[--formats]
> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 13.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.0.3 tools_4.0.3
(base)#MacBook-Air ~ % proj
DeprecationWarning: PROJ_LIB environment variable is deprecated, and will be removed in a future release. You are encouraged to set PROJ_DATA instead.
Rel. 9.1.1, December 1st, 2022
usage: proj [-bdeEfiIlmorsStTvVwW [args]] [+opt[=arg] ...] [file ...]
P.S: I know there are a lots of related topics (Error: gdal-config not found while installing R dependent packages whereas gdal is installed), but all of them are on Linux, and the solutions are not working for me on Mac machine.
I solved it by reinstalling the latest version of R and R Studio.

Change of location of R libraries throws error

I have an issue with the library path of R. Maybe the answer is out there already, but I was not able to find it. I am using R together with RStudio Version 1.1.456. All my additional libraries used to be in a directory dir1 <- \\SHARE\TOOLS_OLD$\R-3.5.1 which I append with .libPaths(dir1). In this setup, everything works as expected.
The problem occured after I copied all the libraries in directory \\SHARE\TOOLS_OLD$\R-3.5.1 to a new location \\NAS\TOOLSNEW$\R-3.5.1. I did not update the R version and I want to keep all the package versions the same such that I have exactly the same setup as before. That is why I did not reinstall the packages in the new location.
When I open a new R session and run the three lines of code
dir2 <- \\NAS\TOOLSNEW$\R-3.5.1
.libPaths(dir2)
.libPaths()
I get the correct output of .libPaths()
[1] "\\\\NAS/TOOLSNEW$/R-3.5.1" "C:/Apps/R-3.5.1/library"
but R also throws an error:
Error: invalid version specification ‘NA’
In addition: Warning message:
In utils:::packageDescription(packageName, fields = "Version") :
no package 'knitr' was found
Apparently some packages remember the original location where they were installed. Is there a way to copy packages to a new location without reinstalling them? If that is not possible, is there an easy (i.e. non-manual) way to reinstall my package tree without upgrading any of the packages (including the dependencies) i.e. defining the version of each package?
Thanks for your help!
PS: Here is my session info, just in case.
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=German_Switzerland.1252 LC_CTYPE=German_Switzerland.1252
LC_MONETARY=German_Switzerland.1252 LC_NUMERIC=C
[5] LC_TIME=German_Switzerland.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 yaml_2.2.0
The problem can have many causes, e.g. there might be some dependencies which are lost by copying the package folder or that the registry entries don't match anymore, but I'm guessing here. When you copy packages from older versions you have to run update.packages (but this might update any package to the currently available version, which you don't want). Check out the R-FAQ and this Stackoverflow question.
You can install packages of a certain version with the devtools package.
install.packages("devtools")
require(devtools)
install_version(somePackage, version = neededVersion)
How I did it was I created a data.frame of all the required packages with the corresponding version number, like
> requiredPkgs
Package Version
condformat "condformat" "0.9.0"
DT "DT" "0.17"
formattable "formattable" "0.2.1"
ggplot2 "ggplot2" "3.3.3"
ggthemes "ggthemes" "4.2.4"
htmlTable "htmlTable" "2.1.0"
and then installed them in a loop
for(iPkg in requiredPkgs) {
install_version(iPkg[1], version = iPkg[2]
}

NetLogo 5.3.1 and R 3.3.1 on Mac 10.10.6 headless exception

I'm trying to run NetLogo 5.3.1 on R 3.3.1 through rStudio 0.99.903 on my mac 10.10.6. I've also installed Java 1.8.0_77-b03
> system("java -version")
java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
> .jinit()
> .jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
[1] "1.8.0_77-b03"
>
> Sys.getenv('JAVA_HOME')
[1] ""
> Sys.getenv('LD_LIBRARY_PATH')
[1] ":#JAVA_LD#"
I'm also running RNetLogo_1.0-2 y rJava_0.9-9.
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RNetLogo_1.0-2 igraph_1.0.1 rJava_0.9-9
loaded via a namespace (and not attached):
[1] magrittr_1.5 rsconnect_0.5 tools_3.3.1
The error I get when I trie to lunch netlogo is as follows.
> nl.path <- ("/Applications/NetLogo 5.3.1/Java")
> ### Start NetLogo
> NLStart(nl.path)
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204)
at java.awt.Window.<init>(Window.java:536)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at javax.swing.SwingUtilities$SharedOwnerFrame.<init>(SwingUtilities.java:1758)
at javax.swing.SwingUtilities.getSharedOwnerFrame(SwingUtilities.java:1833)
at javax.swing.JOptionPane.getRootFrame(JOptionPane.java:1696)
at javax.swing.JOptionPane.showOptionDialog(JOptionPane.java:863)
at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:666)
at javax.swing.JOptionPane.showMessageDialog(JOptionPane.java:637)
at nlcon.NLink.<init>(NLink.java:109)
I've tried several solutions that I've found here at StackExchange or here and there.
http://conjugateprior.org/2014/12/r-java8-osx/
http://charlotte-ngs.github.io/2016/01/MacOsXrJavaProblem.html
https://github.com/snowflakedb/dplyr-snowflakedb/wiki/Configuring-R-rJava-RJDBC-on-Mac-OS-X
http://stackoverflow.com/questions/35179151/cannot-load-r-xlsx-package-on-mac-os-10-11
http://stackoverflow.com/questions/14915898/rnetlogo-function-nlstart-fails-to-launch-gui
None of them have worked on my computer ...
does anyone have any idea what should I have to do to make this work?
Thanks!
PS1/ I have the same exact results running R from the mac shell.
PS2/ I've installed java 1.6 and after test the issue and see that doesn't make any change I deleted it.
As someone commented (I really don't know why they didn't post the answer) the key to the problem was I was using the wrong package to start NetLogo.
First of all one have to be sure that R has the correct Java Home set. So run this on mac shell.
R CMD javareconf
Then, on R shell / console we have to install the rJava in source way to compile with our Java location and version.
install.packages("rJava", type="source", repos="http://cran.us.r-project.org”)
Then we have to install JGR
install.packages("JGR")
and run and open the JGR console.
require(JGR)
### Start JGR
JGR()
Form there on... we run the rest of the script / commands on the JGR console.
### call netlogo. Set the path where NetLogo is installed
nl.path <- ("/Applications/NetLogo 5.3.1/") # Mac path
### Start NetLogo
# The /Java directory is where the NetLogo java app lives. Don't set
# completely before to make the models path work
NLStart(file.path(nl.path, "Java"))
It should work.
PS/ As you can read here, this functions isn't supported on NetLogo for mac and linux

Can't use a installed package in R

I have installed a package in R (ggmap), but when I try to use it R gives an error:
> library()
... (all the rest)
gdata Various R programming tools for data
manipulation
ggmap A package for spatial visualization with Google
Maps and OpenStreetMap
ggplot2 An implementation of the Grammar of Graphics
...
> library("ggmap")
Error in FUN(structure(c("write_png", "read_png"), .Names = c("write_png", :
no such symbol write_png in package/Library/Frameworks/R.framework/Versions/3.0/Resources/library/png/libs/png.so
Error: package or namespace load failed for 'ggmap'
Any suggestion? Thanks!
PD:
sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
EDIT, solution:
Delete ~/Applications/R.app and Library/Frameworks/R.framework and install R again.
Could in be that you have another R installation for an earlier version 3.0 vs the 3.0.1 showing in your sessionInfo()?
Error in FUN(structure(c("write_png", "read_png"), .Names =
c("write_png", : no such symbol write_png in
package/Library/Frameworks/R.framework/Versions/3.0/Resources/library/png/libs/png.so
Error: package or namespace load failed for 'ggmap'
Go to the RStudio-->Options-->Global-->'General'-->R Version
Change the R Version to the latest one which you want to use.

Resources