From 2df4b5cd859d0e5f791284474fd3ec834e46682f Mon Sep 17 00:00:00 2001 From: Edzer Pebesma Date: Fri, 8 Jul 2022 19:09:42 +0200 Subject: [PATCH] fix redist revdep problem; #1958 * redist problem see https://github.com/alarm-redist/redist/issues/148 --- R/tidyverse.R | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/R/tidyverse.R b/R/tidyverse.R index bda1ad8f9..cfd3de952 100644 --- a/R/tidyverse.R +++ b/R/tidyverse.R @@ -6,13 +6,13 @@ #' @name tidyverse dplyr_col_modify.sf <- function(data, cols){ x <- NextMethod() - dplyr::dplyr_reconstruct(x, data) + dplyr_reconstruct.sf(x, data) } #' @name tidyverse dplyr_row_slice.sf <- function(data, i, ...){ x <- NextMethod() - dplyr::dplyr_reconstruct(x, data) + dplyr_reconstruct.sf(x, data) } # This is currently only used in `bind_rows()` and `bind_cols()` @@ -20,22 +20,28 @@ dplyr_row_slice.sf <- function(data, i, ...){ #' @name tidyverse dplyr_reconstruct.sf = function(data, template) { sfc_name = attr(template, "sf_column") - if (inherits(template, "tbl_df")) - data = dplyr::as_tibble(data) + class(data) = setdiff(class(template), "sf") - # Return a bare data frame is the geometry column is no longer there + # Return a bare data frame is the geometry column is no longer there or invalid: if (!sfc_name %in% names(data) || !inherits(data[[sfc_name]], "sfc")) - return(data) - - prec = st_precision(template) - crs = st_crs(template) - - st_as_sf( - data, - sf_column_name = sfc_name, - crs = crs, - precision = prec - ) + data + else { + prec = st_precision(template) + crs = st_crs(template) + agr = if (!is.null(agr <- attr(data, "agr")) && is.factor(agr)) { + att = names(data)[!sapply(data, inherits, what = "sfc")] # non-sfc columns + setNames(agr[att], att) # NA's new columns + } else + NA_agr_ + + st_as_sf( + data, + sf_column_name = sfc_name, + crs = crs, + precision = prec, + agr = agr + ) + } } group_split.sf <- function(.tbl, ..., .keep = TRUE) {