From 80edf9535087f9aa6c47bd214794e8d074df88f7 Mon Sep 17 00:00:00 2001 From: edzer Date: Tue, 23 Apr 2024 16:16:23 +0200 Subject: [PATCH] fixes #2378 --- NEWS.md | 2 ++ R/sf.R | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index a71c43d62..4fc800da9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # version 1.0-17 +* `st_sf()` no longer strips `tbl` or `tbl_df` class labels; #2378 + * `st_layers()` returns an object of class `c("sf_layers", "data.frame")`, with a dedicated `print` method. * when `dim` is not `XYZM`, `sf_as_sf.data.frame()` interprets a length 4 `coords` argument to specify the corners of a rectangular polygon; #2357 diff --git a/R/sf.R b/R/sf.R index 846575b32..70f235198 100644 --- a/R/sf.R +++ b/R/sf.R @@ -273,7 +273,7 @@ st_sf = function(..., agr = NA_agr_, row.names, if (missing(row.names)) row.names = seq_along(x[[sf_column]]) - df = if (inherits(x, "tbl_df")) # no worries: + df = if (inherits(x, c("tbl_df", "tbl"))) # no worries: x else if (length(x) == 1) # ONLY one sfc data.frame(row.names = row.names) @@ -281,6 +281,8 @@ st_sf = function(..., agr = NA_agr_, row.names, x else if (sfc_last && inherits(x, "data.frame")) x[-all_sfc_columns] + else if (inherits(x[[1]], c("tbl_df", "tbl"))) + x[[1]] else cbind(data.frame(row.names = row.names), as.data.frame(x[-all_sfc_columns], @@ -429,7 +431,7 @@ print.sf = function(x, ..., n = getOption("sf_max_print", default = 10)) { app = paste0(app, "\n", "Active geometry column: ", attr(x, "sf_column")) print(st_geometry(x), n = 0, what = "Simple feature collection with", append = app) if (n > 0) { - if (inherits(x, "tbl_df")) + if (inherits(x, c("tbl_df", "tbl"))) NextMethod() else { y <- x