diff --git a/NEWS.md b/NEWS.md index d7cfc28be..a93332aef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ * `st_write` writes non-spatial tables when given a plain `data.frame` or `tbl_df`; #1345 +* the default for `stringsAsFactors` in `st_read` and `st_sf` is FALSE for R version > 4.1.0 + # version 0.9-3 * `st_is_valid` is a generic diff --git a/R/read.R b/R/read.R index 8ed665044..e0ac2eabd 100644 --- a/R/read.R +++ b/R/read.R @@ -1,3 +1,10 @@ +sf_stringsAsFactors = function() { + if (getRversion() < "4.1.0") + default.stringsAsFactors() + else + FALSE +} + set_utf8 = function(x) { n = names(x) Encoding(n) = "UTF-8" @@ -44,9 +51,9 @@ set_utf8 = function(x) { #' of LineString and MultiLineString, or of Polygon and MultiPolygon, convert #' all to the Multi variety; defaults to \code{TRUE} #' @param stringsAsFactors logical; logical: should character vectors be -#' converted to factors? The `factory-fresh' default is \code{TRUE} for -#' \code{st_read} and \code{FALSE} for \code{read_sf}, but this can be changed -#' globally by e.g. the R command \code{options(stringsAsFactors = FALSE)}. +#' converted to factors? Default for \code{read_sf} or R version >= 4.1.0 is +#' \code{FALSE}, for \code{st_read} and R version < 4.1.0 equal to +#' \code{default.stringsAsFactors()} #' @param int64_as_string logical; if TRUE, Int64 attributes are returned as #' string; if FALSE, they are returned as double and a warning is given when #' precision is lost (i.e., values are larger than 2^53). @@ -134,7 +141,8 @@ st_read.default = function(dsn, layer, ...) { } process_cpl_read_ogr = function(x, quiet = FALSE, ..., check_ring_dir = FALSE, - stringsAsFactors = ifelse(as_tibble, FALSE, default.stringsAsFactors()), geometry_column = 1, as_tibble = FALSE) { + stringsAsFactors = ifelse(as_tibble, FALSE, sf_stringsAsFactors()), + geometry_column = 1, as_tibble = FALSE) { which.geom = which(vapply(x, function(f) inherits(f, "sfc"), TRUE)) @@ -199,8 +207,8 @@ process_cpl_read_ogr = function(x, quiet = FALSE, ..., check_ring_dir = FALSE, #' to the current working directory (see \link{getwd}). "Shapefiles" consist of several files with the same basename #' that reside in the same directory, only one of them having extension \code{.shp}. #' @export -st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet = FALSE, geometry_column = 1L, type = 0, - promote_to_multi = TRUE, stringsAsFactors = default.stringsAsFactors(), +st_read.character = function(dsn, layer, ..., query = NA, options = NULL, quiet = FALSE, geometry_column = 1L, + type = 0, promote_to_multi = TRUE, stringsAsFactors = sf_stringsAsFactors(), int64_as_string = FALSE, check_ring_dir = FALSE, fid_column_name = character(0), drivers = character(0), wkt_filter = character(0)) { diff --git a/R/sf.R b/R/sf.R index a2c1e3f34..1693c7452 100644 --- a/R/sf.R +++ b/R/sf.R @@ -193,7 +193,7 @@ list_column_to_sfc = function(x) { #' @param crs coordinate reference system: integer with the EPSG code, or character with proj4string #' @param agr character vector; see details below. #' @param row.names row.names for the created \code{sf} object -#' @param stringsAsFactors logical; logical: should character vectors be converted to factors? The `factory-fresh' default is \code{TRUE}, but this can be changed by setting \code{options(stringsAsFactors = FALSE)}. +#' @param stringsAsFactors logical; see \link{st_read} #' @param precision numeric; see \link{st_as_binary} #' @param sf_column_name character; name of the active list-column with simple feature geometries; in case #' there is more than one and \code{sf_column_name} is \code{NULL}, the first one is taken. @@ -215,7 +215,7 @@ list_column_to_sfc = function(x) { #' df <- st_sf(id = 1:nrows, geometry = geometry) #' @export st_sf = function(..., agr = NA_agr_, row.names, - stringsAsFactors = default.stringsAsFactors(), crs, precision, + stringsAsFactors = sf_stringsAsFactors(), crs, precision, sf_column_name = NULL, check_ring_dir = FALSE, sfc_last = TRUE) { x = list(...) if (length(x) == 1L && (inherits(x[[1L]], "data.frame") || (is.list(x) && !inherits(x[[1L]], "sfc")))) diff --git a/man/sf.Rd b/man/sf.Rd index c8436c940..23117cb7f 100644 --- a/man/sf.Rd +++ b/man/sf.Rd @@ -11,7 +11,7 @@ st_sf( ..., agr = NA_agr_, row.names, - stringsAsFactors = default.stringsAsFactors(), + stringsAsFactors = sf_stringsAsFactors(), crs, precision, sf_column_name = NULL, @@ -30,7 +30,7 @@ st_sf( \item{row.names}{row.names for the created \code{sf} object} -\item{stringsAsFactors}{logical; logical: should character vectors be converted to factors? The `factory-fresh' default is \code{TRUE}, but this can be changed by setting \code{options(stringsAsFactors = FALSE)}.} +\item{stringsAsFactors}{logical; see \link{st_read}} \item{crs}{coordinate reference system: integer with the EPSG code, or character with proj4string} diff --git a/man/st_read.Rd b/man/st_read.Rd index 3552224fa..38f882947 100644 --- a/man/st_read.Rd +++ b/man/st_read.Rd @@ -19,7 +19,7 @@ st_read(dsn, layer, ...) geometry_column = 1L, type = 0, promote_to_multi = TRUE, - stringsAsFactors = default.stringsAsFactors(), + stringsAsFactors = sf_stringsAsFactors(), int64_as_string = FALSE, check_ring_dir = FALSE, fid_column_name = character(0), @@ -82,9 +82,9 @@ of LineString and MultiLineString, or of Polygon and MultiPolygon, convert all to the Multi variety; defaults to \code{TRUE}} \item{stringsAsFactors}{logical; logical: should character vectors be -converted to factors? The `factory-fresh' default is \code{TRUE} for -\code{st_read} and \code{FALSE} for \code{read_sf}, but this can be changed -globally by e.g. the R command \code{options(stringsAsFactors = FALSE)}.} + converted to factors? Default for \code{read_sf} or R version >= 4.1.0 is +\code{FALSE}, for \code{st_read} and R version < 4.1.0 equal to +\code{default.stringsAsFactors()}} \item{int64_as_string}{logical; if TRUE, Int64 attributes are returned as string; if FALSE, they are returned as double and a warning is given when