0

When I run two different scripts from examples (which seem to work where I took them from) I get an error in both cases. Something seems to be wrong in my general set up or something. But I have no idea where it's going wrong. The error I get is the following:

    Error in if (nchar(projargs) == 0) projargs <- as.character(NA) : 
    missing value where TRUE/FALSE needed

What does the error mean and how do I solve it? As far as I understand something seems to be wrong in the projection part, but I believe this should work by just running the code, since it should be working (they are both stand alone examples...)

The codes I run where I get the errors are:

library(sp)
set.seed(357)
pts <- data.frame(x = rnorm(100), y = rnorm(100), var1 = runif(100), var2 = sample(letters, 100, replace = TRUE))
coordinates(pts) <- ~ x + y ## This is where I receive the error
class(pts)
plot(pts)
axis(1); axis(2)

ply <- matrix(c(-1,-1, 1,-1, 1,1, -1,1, -1,-1), ncol = 2, byrow = TRUE)
ply <- SpatialPolygons(list(Polygons(list(Polygon(ply)), ID = 1)))
ply <- SpatialPolygonsDataFrame(Sr = ply, data = data.frame(polyvar = 357))
plot(ply, add = TRUE, border = "red")

(Taken from how to overlay a polygon over SpatialPointsDataFrame and preserving the SPDF data?)

and

r1 = cbind(c(180114, 180553, 181127, 181477, 181294, 181007, 180409, 
180162, 180114), c(332349, 332057, 332342, 333250, 333558, 333676, 
332618, 332413, 332349))
r2 = cbind(c(180042, 180545, 180553, 180314, 179955, 179142, 179437, 
179524, 179979, 180042), c(332373, 332026, 331426, 330889, 330683, 
331133, 331623, 332152, 332357, 332373))
r3 = cbind(c(179110, 179907, 180433, 180712, 180752, 180329, 179875, 
179668, 179572, 179269, 178879, 178600, 178544, 179046, 179110),
c(331086, 330620, 330494, 330265, 330075, 330233, 330336, 330004, 
329783, 329665, 329720, 329933, 330478, 331062, 331086))
r4 = cbind(c(180304, 180403,179632,179420,180304),
c(332791, 333204, 333635, 333058, 332791))

sr1=Polygons(list(Polygon(r1)),"r1")
sr2=Polygons(list(Polygon(r2)),"r2")
sr3=Polygons(list(Polygon(r3)),"r3")
sr4=Polygons(list(Polygon(r4)),"r4")
sr=SpatialPolygons(list(sr1,sr2,sr3,sr4)) # This is where I receive the error
srdf=SpatialPolygonsDataFrame(sr, data.frame(cbind(1:4,5:2), row.names=c("r1","r2","r3","r4")))

taken from the 'over' functions in the sp package help page.

ArnJac
  • 375
  • 1
  • 3
  • 18
  • Please provide some clarification, where are you receiving the error? In either of these examples there is no checking of the proj4string so, a "projargs" makes no sense. What is your actual code? I have a feeling that you are passing an incorrect argument in your code that is making it fail but not the examples. – Jeffrey Evans Jun 14 '16 at 14:41
  • I edited the code with comments where I received the error. Also I found out that the same code is working on another computer. Maybe system related? Ubuntu it doesn't work, OS it works. – ArnJac Jun 14 '16 at 14:48
  • 1
    If you are receiving an error with simply coercing an object using coordinates then there is something wrong with your install, especially considering that you have differences across OS. I would scrap it and reinstall base R and any desired packages. What happens if you call the specific environment using: sp::coordinates(pts) <- ~x + y – Jeffrey Evans Jun 14 '16 at 14:57
  • Reinstalling r base completely did the trick. Thanks! – ArnJac Jun 14 '16 at 15:24

1 Answers1

3

Updating R and sp to the released versions will resolve this.

It was caused by a change in R's behavior on what nchar(NA) returns: see the help file of ?nchar, argument keepNA.

Midavalo
  • 29,696
  • 10
  • 48
  • 104
Edzer Pebesma
  • 2,069
  • 15
  • 16