From 1f8d002fc14eeb8d9baec7335cbddd9af86c985f Mon Sep 17 00:00:00 2001 From: Peter Desmet Date: Wed, 17 Jul 2024 12:16:07 +0200 Subject: [PATCH] Remove helper get_encoding() as it is only used in locale() --- R/get_encoding.R | 31 ------------------------------- R/locale.R | 17 +++++++++++++---- R/read_resource.R | 2 +- 3 files changed, 14 insertions(+), 36 deletions(-) delete mode 100644 R/get_encoding.R diff --git a/R/get_encoding.R b/R/get_encoding.R deleted file mode 100644 index 5f61f152..00000000 --- a/R/get_encoding.R +++ /dev/null @@ -1,31 +0,0 @@ -#' Read encoding -#' -#' Returns the encoding of a [Data -#' Resource](https://specs.frictionlessdata.io/data-resource/) (in a Data -#' Package), or sets it to UTF-8 if unknown. -#' A warning is returned if the given encoding is unknown. -#' -#' @inheritParams read_resource -#' @return Encoding of resource, or UTF-8 if unknown. -#' @family helper functions -#' @noRd -#' @examples -#' # Load the example Data Package -#' package <- example_package() -#' -#' # Get encoding of resource "observations" -#' frictionless:::get_encoding(package, resource_name = "observations") -get_encoding <- function(package, resource_name) { - # Get resource - resource <- get_resource(package, resource_name) - encoding <- resource$encoding %||% "UTF-8" # Set default to UTF-8 - if (!tolower(encoding) %in% tolower(iconvlist())) { - cli::cli_warn( - "Unknown encoding {.field {encoding}}. Reading file(s) with UTF-8 - encoding.", - class = "frictionless_warning_resource_encoding_unknown" - ) - encoding <- "UTF-8" - } - return(encoding) -} diff --git a/R/locale.R b/R/locale.R index f4f9a6dc..88bcbbc7 100644 --- a/R/locale.R +++ b/R/locale.R @@ -8,10 +8,11 @@ #' @family read functions #' @noRd locale <- function(package, resource_name) { + resource <- get_resource(package, resource_name) schema <- get_schema(package, resource_name) fields <- schema$fields - # Decimal mark + # Set decimal mark d_chars <- purrr::map_chr(fields, ~ .x$decimalChar %||% NA_character_) d_chars <- unique_sorted(d_chars) if (length(d_chars) == 0 || (length(d_chars) == 1 && d_chars[1] == ".")) { @@ -25,7 +26,7 @@ locale <- function(package, resource_name) { ) } - # Grouping mark + # Set grouping mark g_chars <- purrr::map_chr(fields, ~ .x$groupChar %||% NA_character_) g_chars <- unique_sorted(g_chars) if (length(g_chars) == 0 || (length(g_chars) == 1 && g_chars[1] == "")) { @@ -39,8 +40,16 @@ locale <- function(package, resource_name) { ) } - # Encoding - encoding <- get_encoding(package, resource_name) + # Set encoding + encoding <- resource$encoding %||% "UTF-8" # Set default to UTF-8 + if (!tolower(encoding) %in% tolower(iconvlist())) { + cli::cli_warn( + "Unknown encoding {.field {encoding}}. Reading file(s) with UTF-8 + encoding.", + class = "frictionless_warning_resource_encoding_unknown" + ) + encoding <- "UTF-8" + } readr::locale( decimal_mark = decimal_mark, diff --git a/R/read_resource.R b/R/read_resource.R index f06a7eff..3f617e9d 100644 --- a/R/read_resource.R +++ b/R/read_resource.R @@ -222,7 +222,7 @@ read_resource <- function(package, resource_name, col_select = NULL) { ) } - # Create locale with encoding, decimal_mark and grouping_mark + # Create locale with decimal_mark, grouping_mark and encoding locale <- locale(package, resource_name) # Create col_types: list(, , ...)