Liz
Liz

Reputation: 53

R Biogeo pkg: Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'

I am usually pretty good at figuring out error messages as long as it doesn't require much looking at the underlying code, but I am very new to working with spatial data, and can't figure this one out.

I am using the Biogeo package with RStudio v. 0.99.903, R version 3.2.4 (2016-03-10), Windows 10 (EDIT: upgrade to newest version of R did not get rid of the error). I am currently working through the tutorial alongside trying to apply it to my own data. The function code and relevant data are on github at: https://github.com/cran/biogeo (the tutorial narrative is in the docs folder - I am at the beginning, trying to make the map on page 2).

I am getting the following error when using the pointsworld function:

Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'

Related questions I found on Stack Overflow (one unanswered, one I don't really understand):

(1) R: Error in as.double(y) : cannot coerce type 'S4' to vector of type 'double'-- raster data (2) Self-authored package: load plot method for spatialPolygonsDataFrame

This is my relevant code (from the tutorial):

library(biogeo)
data(dat) # Access the species dataset
data(world) # Access the country boundaries
a <- pointsworld(world, dat) # basic plotting

I also tried the function with a subset of my own data, and received the same error.

These are all of the packages I currently have open:

search()
[1] ".GlobalEnv"        "package:dplyr"     "package:maptools"      "package:biogeo"    "package:raster"
[6] "package:sp"        "tools:rstudio"     "package:stats"     "package:graphics"  "package:grDevices"
[11] "package:utils"     "package:datasets"  "package:methods"   "Autoloads"         "package:base"     

typeof() returns "S4" for "world" and "list" for dat

class() returns "SpatialPolygonsDataFrame" attr(,"package") "sp" for "world" and "data.frame" for "dat"

I hope I have not overlooked something simple - I have typed and retyped the code, tried it with different data sets, tried it with the suggested (in the tutorial) map/boundaries file from ...www.thematicmapping.org/downloads/world_borders.php (take off the dots at the start, I don't have enough reputation pts to post the link), and have tried directly copy and pasting directly from the tutorial the different examples of using this function.

Hopefully it is not just a result of me not drinking enough coffee yet - I've spent much time trying to figure this out, and am hoping somebody here can help.

Thanks! Liz

Upvotes: 2

Views: 2296

Answers (1)

Liz
Liz

Reputation: 53

line numbers refer to code at https://github.com/cran/biogeo/blob/master/R/pointsworld.R

In lines 37-40 I specified that the functions should be from the sp package (e.g., sp::spatialPoints). Contrary to my previous comment, nothing had to be changed in previous lines. There appears to be something wrong with how the sp functions are being imported (??). I am marking this as answered, even though importing the sp functions is the real issue, but I'm not sure how to fix that at the moment (just need to learn though!).

Upvotes: 3

Related Questions