Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obscure error with indexing st_geometry on tibble #391

Closed
timelyportfolio opened this issue Jun 19, 2017 · 5 comments
Closed

obscure error with indexing st_geometry on tibble #391

timelyportfolio opened this issue Jun 19, 2017 · 5 comments

Comments

@timelyportfolio
Copy link

timelyportfolio commented Jun 19, 2017

I hesitated to even file this issue as it seems to be an obscure edge case, but in the interest of robustness of sf I elected to file. Feel free to close if too obscure. I discovered researching r-spatial/mapedit#32.

generalized issue

Probably easier to understand below as a reproducible example. The error only happens with assignment using st_geometry(my_sf[####,]) on a sf tibble object with a column that is after the geometry column.

reproducible example

Everything works exactly as I would expect except the one commented case below.

library(sf)
library(tibble)

geom <- st_sfc(
  st_polygon(
    list(
      cbind(x = c(0.69, 0.8, 1.1, 1.23, 0.69), 
            y = c(0, 0.6, 0.63, 0.3, 0))
    )
  )
)

feat <- st_sf(ltr="a",geom)

# add column when as data.frame
feat$id <- 1
# demonstrate st_geometry assignment works with data.frame
# works
st_geometry(feat) <- geom
feat
# works
st_geometry(feat[1,]) <- geom
feat
# works
st_geometry(feat)[1] <- geom
feat

# do same as above but this time as tibble
feat_tbl <- st_sf(tibble(ltr="a",geom=geom))
# add a column when as tibble
feat_tbl$id <- 1
# works
st_geometry(feat_tbl) <- geom
feat_tbl
################# does not work ##########################
feat_tbl <- st_sf(tibble(ltr="a",geom=geom))
feat_tbl$id <- 1
st_geometry(feat_tbl[1,]) <- geom
feat_tbl
##########################################################
# works
feat_tbl <- st_sf(tibble(ltr="a",geom=geom))
feat_tbl$id <- 1
st_geometry(feat_tbl)[1] <- geom
feat_tbl
# works
feat_tbl <- st_sf(tibble(ltr="a",geom=geom))
feat_tbl$id <- 1
# move id column before geometry column
feat_tbl <- feat_tbl[,c("ltr","id","geom")]
st_geometry(feat_tbl[1,]) <- geom
feat_tbl
@edzer
Copy link
Member

edzer commented Jun 19, 2017

p1 is not defined, afaics.

@timelyportfolio
Copy link
Author

@edzer sorry about that. Fixed example above.

@edzer
Copy link
Member

edzer commented Jun 24, 2017

This is a nested replacement function; I'm afraid this is never going to work. A similar example from base R, which also doesn't work, is this:

df = data.frame(a = 3, id = 1)
row.names(df[1,]) = "a"
row.names(df)
# [1] "1"
row.names(df[1,])
# [1] "1"

I'll try to find out why the object returned is invalid - that shouldn't happen.

@timelyportfolio
Copy link
Author

No problem and thanks. I just thought it strange that all cases worked except the one. Thanks again!

@edzer
Copy link
Member

edzer commented Jun 24, 2017

Sure; I'll keep it open until I'm convinced it doesn't reveal a problem in sf.

@edzer edzer reopened this Jun 24, 2017
@edzer edzer closed this as completed Jul 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants