Using R package BerkeleyEarth - r

I'm working for the first time with the R package BerkeleyEarth, and attempting to use its convenience functions to access the BEST data. I think maybe it's just a problem with their servers (a matter I've separately addressed to the package's maintainer) but I wanted to know if it's instead something silly I'm doing.
To reproduce my fault
library(BerkeleyEarth)
downloadBerkeley()
which provides the following error message
trying URL 'http://download.berkeleyearth.org/downloads/TAVG/LATEST%20-%20Non-seasonal%20_%20Quality%20Controlled.zip'
Error in download.file(urls$Url[thisUrl], destfile = file.path(destDir, :
cannot open URL 'http://download.berkeleyearth.org/downloads/TAVG/LATEST%20-%20Non-seasonal%20_%20Quality%20Controlled.zip'
In addition: Warning message:
In download.file(urls$Url[thisUrl], destfile = file.path(destDir, :
InternetOpenUrl failed: 'A connection with the server could not be established'
Has anyone had a better experience using this package?

The error message is pointing to a different URL than one should get judging what URLs are listed at http://berkeleyearth.org/data/ that point to the zip formatted files. There are another set of .nc files that appear to be more recent. I would replace the entries in the BerkeleyUrls dataframe with the ones that match your analysis strategy:
This is the current URL that should be in position 1,1:
http://berkeleyearth.lbl.gov/downloads/TAVG/LATEST%20-%20Non-seasonal%20_%20Quality%20Controlled.zip
And this is the one that is in the package dataframe:
> BerkeleyUrls[1,1]
[1] "http://download.berkeleyearth.org/downloads/TAVG/LATEST%20-%20Non-seasonal%20_%20Quality%20Controlled.zip"
I suppose you could try:
BerkeleyUrls[, 1] <- sub( "download\\.berkeleyearth\\.org", "berkeleyearth.lbl.gov", BerkeleyUrls[, 1])

Related

About my GIS question in R (from readShapePoly to sf::st_read)

I am needed to use sf package command, because readShapePoly commnad will be erased. That's neer future I know... So I want to change my code from route
thata readShapePloy to route sf::st_read. But I cannot write correct code. So I want to correct code and I am very happy, if u show correct sf package command. My now command below, thx...(I am sorry to my poor English skill,Plz over come it...)
In R, I wrote code again and again for ex, on sf::st_read command. But that show error again and again...code below nd error message below too
usa_state <- readShapePoly("usa_state.shp", IDvar = "STATE_CODE")
That is ok, but I know to change that code neer future, cuz this command is erased in neer future. So Plz show me command thata route of sf package.I tried below code but I know this is not understandable in R.
usa_state = sf::st_read("usa_state.shp", layer = "STATE_CODE")
bad code...Plz shw me correct coding!Error occured now am I...
Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
SQL execution failed, cannot open layer.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 1: SQL Expression Parsing Error: syntax error, unexpected
identifier, expecting SELECT or '('. Occurred around : "STATE_CODE"
You're almost there with usa_state = sf::st_read("usa_state.shp", layer = "STATE_CODE").
I'm guessing that STATE_CODE is a field in the usa_state.shp shapefile. You don't need to supply any field names to the st_read() function. Just use:
library(sf)
usa_state = st_read("usa_state.shp")
You'll need to make sure that the usa_state.shp file (and its associated files) are in your current working directory, or you'll need to use the full path:
usa_state = st_read("/path/to/usa_state.shp")
The sf package is well worth getting to know. It's made all of my spatial work in R much easier.

Problems parsing StreamR JSON data

I am attempting to use the streamR in R to download and analyze Twitter, under the pretense that this library can overcome the limitations from the twitteR package.
When downloading data everything seems to work fabulously, using the filterStream function (just to clarify, the function captures Twitter data, just running it will provide the json file -saved in the working directory- that needs to be used in further steps):
filterStream( file.name="tweets_test.json",
track="NFL", tweets=20, oauth=credential, timeout=10)
Capturing tweets...
Connection to Twitter stream was closed after 10 seconds with up to 21 tweets downloaded.
However, when moving on to parse the json file, I keep getting all sorts of errors:
readTweets("tweets_test.json", verbose = TRUE)
0 tweets have been parsed.
list()
Warning message:
In readLines(tweets) : incomplete final line found on 'tweets_test.json'
Or with this function from the same package:
tweet_df <- parseTweets(tweets='tweets_test.json')
Error in `$<-.data.frame`(`*tmp*`, "country_code", value = NA) :
replacement has 1 row, data has 0
In addition: Warning message:
In stream_in_int(path.expand(path)) : Parsing error on line 0
I have tried reading the json file with jsonlite and rjson with the same results.
Originally, it seemed that the error came from special characters ({, then \) within the json file that I tried to clean up following the suggestion from this post, however, not much came out of it.
I found out about the streamR package from this post, which shows the process as very straight forward and simple (which it is, except for the parsing part!).
If any of you have experience with this library and/or these parsing issues, I'd really appreciate your input. I have been searching non stop but haven't been able to locate a solution.
Thanks!

Difficulty opening a package data file of unknown type

I am trying to load the state map from the maps package into an R object. I am hoping it is a SpatialPolygonsDataFrame or something I can turn into one after I have inspected it. However I am failing at the first step – getting it into an R object. I do not know the file type.
I first tried to assign the map() output to an R object directly:
st_m <- maps::map(database = "state")
draws the map, but str(st_m) appears to do nothing, unless it is redrawing the same map.
Then I tried loading it as a dataset: st_m <- data("stateMapEnv", package="maps") but this just returns a string:
> str(stateMapEnv)
chr "R_MAP_DATA_DIR"
I opened the maps directory win-library/3.4/maps/mapdata/ and found what I think is the map file, “state.L”.
I tried reading it with scan and got an error message I do not understand:
scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
scan() expected 'a real', got '#'
I then opened the file with Notepad++. It appears to be a binary or compressed file.
So I thought it might be an R data file with an unusual extension. But my attempt to load it returned a “bad magic number” error:
st_m <- load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L") :
bad restore file magic number (file may be corrupted) -- no data loaded
Observing that these responses have progressed from the unhelpful through the incomprehensible to the occult, I thought it best to seek assistance from the wizards of stackoverflow.
This should be able to export the 'state' or any other maps dataset for you:
library(ggplot2)
state_dataset <- map_data("state")

R package 'biomod2' cannot find file when executing plot function

I am attempting to use the plot() function in the biomod2 package, following a vignette here (http://finzi.psych.upenn.edu/usr/share/doc/library/biomod2/doc/Simple_species_modelling.pdf). Below is the error I am getting:
getwd()
# [1] "/home/gjanzen/Documents/Hufford/Drought/Data/Layers"
plot(myBiomodData)
Error in getExportedValue(pkg, name) : cannot open file
'~/R/x86_64-pc-linux-gnu-library/3.3/viridisLite/data/Rdata.rdb': No
such file or directory In addition: Warning message: In
getExportedValue(pkg, name) : restarting interrupted promise
evaluation
I have confirmed that the Rdata.rdb exists, in the following directory:
f <- file.choose()
f
# [1] "/home/gjanzen/R/x86_64-pc-linux-gnu-library/3.3/viridisLite/data/Rdata.rdb"
So, to me, it looks like the plot() function is looking in the wrong place. How can I change where this function looks for Rdata.rdb? Can I alter the path somehow? Or would changing my working directory fix this?
PS - This is my first post on Stack Overflow, so please forgive any mistakes in etiquette, and/or feel free to point them out to me so that I do not repeat them.
I think that the first thing to try is to reinstall the package viridisLite that seems to be the one that is causing troubles.
install.packages('viridisLite', dep = TRUE)
If this not solves the issue you should try to open a new plotting device threw x11() to check if the issue is not coming from the R (resp. RStudio) plotting device itself.

In R, after installed a package,' pixmap', Error in file(file, open = "rb") : cannot open the connection

I'm learning R programming, using the book, "The Art of R Programming".
In chapter 3.2.3 Extended Example: Image Manipulation. The author Matloff tries to use a Mount Rushmore gray-scale image to illustrate that the image is stored in matrix. He used a library called pixmap. And I downloaded the package, installed it.
> library(pixmap)
> mtrush1 <- read.pnm("mtrush1.pgm")
> mtrush1
Pixmap image
Type : pixmapGrey
Size : 194x259
Resolution : 1x1
Bounding box : 0 0 259 194
> plot(mtrush1)
This is what the book has written, and I tried to run this, but got the error message,
> library(pixmap)
> mtrush1 <- read.pnm("mtrush1.pgm")
Error in file(file, open = "rb") : cannot open the connection
In addition: Warning message:
In file(file, open = "rb") :
cannot open file 'mtrush1.pgm': No such file or directory
starting httpd help server ... done
What does this mean? cannot open the connection? And also the mtrush1.pgm does not exist? How should I fix it here? Any help? Much appreciated.
Summary:
Add the argument cellres=1 to your function call and you should be fine.
Answer:
The second error you saw--Warning message: In rep(cellres, length = 2) : 'x' is NULL so the result will be NULL--is because you haven't set the cellres argument and, as a result, "cellres" assumes its default value (i.e. 'NULL'--hence the warning). For what you're working on, setting the cellres argument to 1 will do the trick (though you can pass in a two-element vector with unequal values and see just how it affects your figure by plotting the resulting object).
Note: Though it's a little late to be answering, I figure that since I had the same problem earlier today (and since Google was no help) a response was probably warranted.
This means that the file mtrush1.pgm is not in current directory. You should either setwd to the directory that contains this file, or specify the complete path in read.pnm.
For the file mtrush1.pgm, you can download it from http://heather.cs.ucdavis.edu/~matloff/
The file mtrush1.pgm and the R scripts from the book "The Art Of R Programming" can be found at this GitHub site.

Resources