Sf: *_join: Error in if (!setting_geom) { : missing value where TRUE/FALSE needed

Created on 26 May 2019  路  6Comments  路  Source: r-spatial/sf

Encountered an error without an informative error message. Here's my code:

#read
drvalg2015 = read_csv("data/dr.dk valget 2015/Valgkort Live Pre Valg.csv")

#geodata
drvalg2015_shp = sf::read_sf("data/dr.dk valget 2015/Testdata for Afstemningsomraader/AFSTEMNINGSOMRAADE.shp")

#flet sammen
drvalg2015_shp$Aggr = str_glue("{drvalg2015_shp$KommuneNum}-{drvalg2015_shp$AfstemKod}") %>% as.character()
drvalg2015_shp = left_join(drvalg2015_shp, drvalg2015 %>% filter(!duplicated(Aggr)), by = "Aggr")

The files can be found here: https://osf.io/7bzc9/ but you don't need them to understand the issue.

The left_join call fails with rather unhelpful:

> drvalg2015_shp = left_join(drvalg2015_shp, drvalg2015 %>% filter(!duplicated(Aggr)), by = "Aggr")
Error in if (!setting_geom) { : missing value where TRUE/FALSE needed

Tracing it down into left_join.sf() and then sf:::sf_join(), we see that the bug has a trivial cause:

Browse[2]>   attr(g[[sf_column]], "bbox") = NULL
Error in if (!setting_geom) { : missing value where TRUE/FALSE needed
Browse[2]> sf_column
[1] "geometry"
Browse[2]> g %>% names() %>% str_subset("geo")
[1] "geometry.x" "geometry.y"

Thus, the bug is caused by having a geometry column on both sides, causing the sf_column to fail to subset properly. I can think of two solutions:

  1. Check for duplicate geometry columns, and fail with informative error (easy solution).
  2. Check for duplicate geometry columns, if found, rename the one in second input data frame to "geometry.y", throw an informative warning about this, and proceed with joining (nice solution).

Both should be trivial to implement.

bug

All 6 comments

If you provide a simple download for your data (that is, without requiring me to sign up) then I can look into this.

You don't need the data to understand this issue. It's a simple case of overlapping names in the two input data frames. However, the data are already open (no sign up needed).

If you want me to solve this issue, I need you to provide me a minimum reproducible example that does not require me to sign up for a system first.

@Deleetdk, the issue seems to be related to tibbles. Try using st_read(..., quiet = TRUE, stringsAsFactors = FALSE) instead of read_sf as a workaround.

I could reproduce the issue if x is a tbl_df (to be passed to left_join.tbl_df rather than left_join.data.frame) and y must have a column named as the geometry column of x (geometry).

library(sf)
library(tidyverse)

x <- sf::read_sf(system.file("shape/nc.shp", package="sf"))
y <- st_drop_geometry(x) %>% mutate(geometry = 1)

lj <- left_join(x, y, by = c("CNTY_ID"))
#> Error in if (!setting_geom) {: missing value where TRUE/FALSE needed

sloop::s3_dispatch(left_join(x, y, by = c("CNTY_ID")))
#> => left_join.sf
#> -> left_join.tbl_df
#>    left_join.tbl
#>  * left_join.data.frame
#>    left_join.default

Created on 2019-06-04 by the reprex package (v0.2.1)

Thanks! Should be fixed now.

With the dev version, works on my original datasets too. Yay!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nowosad picture Nowosad  路  3Comments

kendonB picture kendonB  路  4Comments

gregmacfarlane picture gregmacfarlane  路  4Comments

jmsigner picture jmsigner  路  4Comments

matthewpaulking picture matthewpaulking  路  4Comments