Download GPM-IMERG satellite images in R - r

I've been trying (a lot) to download NASA's GPM-IMERG satellite images with precipitation data from R. I'm doing the question in this forum and not in the GIS forum because I read that most r users are here. (FYI: Im running Windows 10 and r 3.6.3 in rstudio). So far I've tried the following:
Created an account at PMM-NASA (see here). Everything worked well.
Installed the gpm package from devtools::install_github("csaybar/gpm"), followed by
gpm_getaxel() (see here). I tried running the following code.
gpm_download(path = RutaDownloads,
user = "myuser#email.com",
password = "myuser#email.com",
dates = c('2017-01-01','2017-02-28'),
band = 3,
lonMin = 70,
lonMax = 75,
latMin = 34,
latMax = 38,
product = 'finalrun',
quiet = F,
n = 1)
However, it did not worked. The error shown in rstudio is the following.
'gdal_translate' not found Error in gdaltranslate_exist() : GDAL
should be declared as a system variable if you are using Windows
I haven't had any problems running gdal when working with multiple rasters/vectors in R (so far). Does anyone know if I have to install gdal in my PC apart from installing rgdal in r? If so, how can I do it and 'synchronize' it with R to be used with gpm package? I know there is a lot of information in google, however, I'd rather take the advice from anyone that has done this before, because in the past I did not have such a good experience working, for example, with GDAL and Python, and that is the main reason I started working my GIS codes in R.
I also tried with another alternative installing the remotes package and remotes::install_github("bastianmanz/GPM_rain") (See here). However, this uses the rhdf5 package, which is not available for my R version, and thus I installed the BiocManager and then BiocManager::install("rhdf5") (following the instructions from here). With GPM_rain there are two possible ways to download the images: (1) with wget i.e. system_download, but its not straightforward because it needs a list of the files to download, and (2) with rcurl_download using RCurl. This seemed easy to use. Nevertheless, the first problem is that I cannot specify lat/long for the image extent and, as in point 2, it did not work. I tried running the following code:
rcurl_download(product = "nrt",
nrt_type = "late",
start = 20170101,
end = 20170131,
userpwd = "myuser#email.com:myuser#email.com")
But the error says that rcurl_downloadfunction was not found. How to fix any of the above errors or have any other solutions, I would be really grateful if you could share your experience, specially for option 2, where I can set lat/long to download the data. Thanks in advance,
Jorge.
--------------------------------------------------------------------------------------------------------
EDIT:
Package gpm is "retired" according to its developer (see here).

Related

RStudio session aborts when I run gRbase::stepwise function (undirected graphs)

I'm doing an R project on RStudio (RStudio 2021.09.1+372 "Ghost Orchid" Release for Windows), (R version 4.1.2).
I'm working on Windows 10 x64.
I want to create an Undirected Graph from a dataset:
library(gRbase)
library(gRim)
library(gRain)
data(BodyFat)
BodyFat <- BodyFat[-c(31,42,48,76,86,96,159,169,175,182,206),]
BodyFat$Age <- sqrt(BodyFat$Age)
BodyFat$Weight <- sqrt(BodyFat$Weight)
names(BodyFat)[names(BodyFat) == 'BodyFat'] <- 'BodyFatPerc'
gRbodyfat <- BodyFat[,2:15]
graph.MyGraph <- cmod(~.^., data = gRbodyfat)
AIC.MyGraph <- gRbase::stepwise(graph.MyGraph)
I'm still exploring RStudio and graphical models, so I run these lines from the console, one by one.
When I run the last line of code, R session aborts and I get the following pop-up message:
I've also tried the following lines of code (I changed the last line):
library(gRbase)
library(gRim)
library(gRain)
data(BodyFat)
BodyFat <- BodyFat[-c(31,42,48,76,86,96,159,169,175,182,206),]
BodyFat$Age <- sqrt(BodyFat$Age)
BodyFat$Weight <- sqrt(BodyFat$Weight)
names(BodyFat)[names(BodyFat) == 'BodyFat'] <- 'BodyFatPerc'
gRbodyfat <- BodyFat[,2:15]
graph.MyGraph <- cmod(~.^., data = gRbodyfat)
AIC.MyGraph <- stepwise(graph.MyGraph)
but I get the same problem. I tried three or four times running those lines of code, R aborted everytime.
My gRbase, gRim, gRain, Rgraphviz and RBGL libraries are in the home folder:
C:/Users/MyUser/Documents/R/win-library/4.1
Any advice?
EDIT:
I've tried uninstalling/reinstalling R, Rtools, RStudio, reinstalling libraries, updating them, I've tried launching RStudio as Administrator, I've tried changing computer.
The last thing I tried is this:
uninstalling R, Rtools and RStudio
deleting .RData and .Renviron files in the Documents folder
deleting all libraries
downloading R (latest version) from CRAN webpage and installing it as Administrator
downloading Rtools (latest version from webpage) and installing it as Administrator
creating the .Renviron file as described here
downloading RStudio (latest version from webpage) and installing it as Administrator
installing BiocManager as described here (when asked to update Matrix library I said NO, I thought it could be the problem)
installing gRbase, gRim, gRain libraries as described here (when asked to update fansi library I said NO because it said "There are binary versions available but the source versions are later", I thought it could be the problem)
Tried again the lines of code
Still got the problem, R session aborted again.
A friend of mine doing a similar project used gRbase::stepwise function, so I inserted his code in the console and the function worked. So I think the problem might be the object of the function, graph.MyGraph. The cmod function works fine, so maybe there's a problem with gRbodyfat, which is used to build graph.MyGraph.
If I run these lines (I tried changing the dataset, I used BodyFat instead of gRbodyfat):
graph.MyGraph <- cmod(~.^., data = BodyFat)
AIC.MyGraph <- gRbase::stepwise(graph.MyGraph)
I get the following error:
Error in dim(res) <- c(NSEL, NSET) :
dims [product 210] do not match the length of object [14]
Don't know what to do.
EDIT:
I've tried debugging gRbase::stepwise function and I get this:
The problem appears to be the iModel function.
EDIT:
I've discovered that if I use graph.MyGraph <- cmod(~.^1, data = gRbodyfat) the gRbase::stepwise function works fine.

Unable to install the package rmetadata in R

My primary goal is to extract metadata of an article using the title and journal name. To do so, rmetadata package is useful according to the blog https://ropensci.org/blog/2013/03/15/r-metadata/. However, I'm unable to install the package using the install_github() function. Any help? Thanks!
The repo is now archived at https://github.com/ropensci-archive/rmetadata
It does install though, try remotes::install_github("ropensci-archive/rmetadata")
However, that package is no longer maintained. You probably want to use rcrossref, see the docs at https://docs.ropensci.org/rcrossref/. e.g.,
install.packages("rcrossref")
res = cr_works(flq = c(`query.container-title` = 'Ecology',
`query.bibliographic` = 'An assessment of statistical methods for
nonindependent data in ecological meta-analyses'))
See the Crossref API docs http://api.crossref.org/swagger-ui/index.html#/Works/get_works as well

Better reproductibility of rPackages (pin version of packages) in nix in comparison to guix

I'm actually evaluate different solution to enhance/explore reproductibility in my R/Python scientific workflow : data with reproductible analysis (plot, analysis) and paper.
There is, as you know, two big linux flavours offer some solutions : Nix and Guix
In nix, the way commonly described to develop with R is, for example, using rWrapper and rPackages :
pkgs.rWrapper.override{ packages = with pkgs.rPackages; [tidyverse rmarkdown]; };
My problem is (not so...) simple, like Python, R is well known to be a nightmare in term of reproducibility, even at middle term. For fun, you could try to run a ggplot2 code from 2 years with a recent version of R...
In order to propose a flake that produce the same result from the same data for a scientific paper, i'm interested to fix in derivation the version of R and the version of R packages used to compute analysis or plot.
{
description = "Generate R result from simulation";
inputs = {
nixpkgs.url = "nixpkgs/nixos-20.09";
utils.url = "github:numtide/flake-utils";
};
outputs = {self, nixpkgs, utils, mach-nix } : (utils.lib.eachDefaultSystem
(system :
let
pkgs = nixpkgs.legacyPackages.${system};
REnv = pkgs.rWrapper.override{ packages = with pkgs.rPackages; [tidyverse rmarkdown]; };
buildRScripts = { stdenv, fetch,... }: stdenv.mkDerivation {
name = "myscript";
src = self;
nativeBuildInputs = [ REnv ];
dontBuild = true;
buildInputs = [ pkgs.pandoc pkgs.unzip ];
installPhase=''
mkdir $out
cd $out
${REnv}/bin/Rscript -e 'rmarkdown::render("test.Rmd")
'';
in {
defaultPackage = self.packages.${system}.buildRScripts;
}
));}
For example, how could i define more precisly that i want to use, to compile my test.Rmd, only the tidyverse 1.3.1 with R 4.1.O ? Even in 5 years ?
I found that Guix show different available packages/versions of R and tidyverse :
http://data.guix.gnu.org/repository/1/branch/master/package/r-tidyverse
Version needed by tidyverse.1.3.1 are clearly presented :
http://data.guix.gnu.org/gnu/store/31rwnjq3map2njx1518vsxpwbvy7xx2j-r-tidyverse-1.3.1.drv
With rPackages in Nix i search a way to achieve something similar, ie. a way to refer explicitly to version of R or R packages into derivation, but i didn't found it.
With rPackages here nix developper already offering a great fundation, but perhaps we need more ...
How we could, collectively achieve a better reproducibility on R packages with Nix ? I'm interested by any ideas ?
Perhaps we could fetch sources of packages directly from the cran archive and compile it ? For example with tidyverse :
https://cran.r-project.org/web/packages/tidyverse/index.html,
https://cran.r-project.org/src/contrib/Archive/tidyverse/ ?
Ps : i know that Nix and Guix are each partners with https://archive.softwareheritage.org/, a great way to archive and call cran package :
https://guix.gnu.org/fr/blog/2019/connecting-reproducible-deployment-to-a-long-term-source-code-archive/
https://www.tweag.io/blog/2020-06-18-software-heritage/
Ps : answer could also be added to https://nixos.wiki/wiki/R
Update 1
After discussion with some great people on nix discord, i understand that nix doesn't need version because flake.nix + flake.lock store hash (see nix flake metadata) that link my build and download with a very specific commit on nixpkgs.
But that don't solve :
the problem of the tar.gz sources linked/needed by this packages declared at this very specific commit by RPackages ? I suppose software heritage will help on this point ?
the common problem of incompatibility between some R version, and R version of packages. For example, you write a code with R 3.0.0 and tidyverse 1.2.3, you update your R version because some other packages need an update and only works with dependency available with R 3.2.0, but ahum, tidyverse 1.2.3 don't exist for R 3.2.0 ... Fixing version and access to old tar.gz resolve part of this problem i suppose.
How we define something like this using nix ?
Update 2
It seems someone build an unofficial index to help people searching old version of package Ex with tidyverse : https://lazamar.co.uk/nix-versions/?channel=nixpkgs-unstable&package=r-tidyverse
Thanks #dram for link and discussion on this.

Geomorph not functioning after update

I recently updated my MacBook air (2017) to the Mojave OS and proceeded to update my version of R and Rstudio. Unfortunately, ever since the 'plotTangentSpace' command no longer functions appropriately even when attempting to run the example 'plethodon' code from the Geomorph vingette, I always receive the following error in the output (bolded below):
data(plethodon)
Y.gpa<-gpagen(plethodon$land) #GPA-alignment
|====================================================================================| 100%
gp <- interaction(plethodon$species, plethodon$site)
plotTangentSpace(Y.gpa$coords, groups = gp)
Error in plotTangentSpace(Y.gpa$coords, groups = gp) :
unused arguments (Y.gpa$coords, groups = gp)
I have tried uninstalling and reinstalling R for a MacOS many times including versions 4.0.2, 3.6.3, 3.6.1, and 3.2.1. I have also uninstalled and reinstalled XQuartz several times.
If anyone has encountered a similar issue or has any other ideas I can try I would really appreciate the help. Thank you!
If you execute:
?plotTangentSpace
You should see that this is a deprecated function. That means you should not be using it. It says:
Notes for geomorph 3.3.0 and subsequent versions
I suspect you have not updated your copy of the vignette. There are instructions on the help page for an alternate approach. The example in the help page for the suggested function is:
data(plethspecies)
Y.gpa <- gpagen(plethspecies$land) #GPA-alignment
### Traditional PCA
PCA <- gm.prcomp(Y.gpa$coords)
summary(PCA)
plot(PCA, main = "PCA")
For the plethodon data they suggest:
PCA.w.phylo <- gm.prcomp(Y.gpa$coords, phy = plethspecies$phy)
summary(PCA.w.phylo)
plot(PCA.w.phylo, phylo = TRUE, main = "PCA.w.phylo")
So start a new session (to unload the currently loaded geomorph namespace, and execute this at the r session command line:
install.packages("geomorph")
You should be getting version 3.3.1 of the geomorph package.
I think I found the problem! In my case, the error was due to a old version of the package RRPP, which is required by Geomorph. After updating it, Geomorph is working perfectly! Hope this can be useful for you too.
I came across the same error but after updating the RRPP and rgl packages required for geomorph, the gm.prcomp() function worked for me. I hope this helps if you haven't figured it out already.

How do I deal with an error message while installing a package?

I am brand new to this so please forgive my inexperience...I'm trying to learn.
I'm attempting to install an R package called "Doublet Finder" using the specified code given on the Github site.
When I do this, I get this error immediately:
Error in rbind(info, getNamespaceInfo(env, "S3methods")) :
number of columns of matrices must match (see arg 2)
Being new to R, I'm not sure what this error means and when I google this something similar comes up and the individual removed and re-installed ALL of their libraries...that seems crazy. Does anyone have advice on what this could be, how to fix it, or why the package won't install?
Your problem seems to be fairly similar to this one. It might be the case that the dependencies (packages that Doublet Finder relies on) are outdated. What you can try is to follow these steps to uninstall and reinstall all packages with the hope that by updating packages there isn't a version mismatch.
This code is copied from the website above:
ip <- as.data.frame(installed.packages(lib.loc = .libPaths()[1]),
stringsAsFactors = FALSE)
head(ip)
str(ip)
path.lib <- unique(ip$LibPath)
# create a vector with all the names of the packages you want to remove
pkgs.to.remove <- ip[,1]
head(pkgs.to.remove)
str(pkgs.to.remove)
sapply(pkgs.to.remove, remove.packages, lib = path.lib)
sapply(pkgs.to.remove, install.packages, lib = path.lib)

Resources