From 4cc3774ffb47603375a9b119127faa698f3ad75f Mon Sep 17 00:00:00 2001 From: Thierry Onkelinx Date: Tue, 22 Oct 2019 15:52:33 +0200 Subject: [PATCH] use iconv() to make sure that we use UTF-8 for all characters and factors --- R/meta.R | 4 +++- tests/testthat/test_b_special.R | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/R/meta.R b/R/meta.R index 9766c1d..69c3888 100644 --- a/R/meta.R +++ b/R/meta.R @@ -38,6 +38,7 @@ meta <- function(x, ...) { #' @importFrom assertthat assert_that is.string noNA meta.character <- function(x, na = "NA", ...) { assert_that(is.string(na), noNA(na), no_whitespace(na)) + x <- iconv(x, to = "UTF-8") if (na %in% x) { stop("one of the strings matches the NA string ('", na, "') Please use a different NA string or consider using a factor.", call. = FALSE) @@ -79,6 +80,7 @@ meta.numeric <- function(x, ...) { #' @importFrom assertthat assert_that is.flag noNA meta.factor <- function(x, optimize = TRUE, na = "NA", index, ...) { assert_that(is.flag(optimize), noNA(optimize)) + levels(x) <- iconv(levels(x), to = "UTF-8") if (missing(index) || is.null(index)) { index <- seq_along(levels(x)) names(index) <- levels(x) @@ -109,7 +111,7 @@ Please use a different NA string or use optimize = TRUE", call. = FALSE) } m <- list(class = "factor", na_string = na, optimize = optimize, - labels = enc2utf8(names(index)), index = unname(index), + labels = names(index), index = unname(index), ordered = is.ordered(x)) class(m) <- "meta_detail" attr(z, "meta") <- m diff --git a/tests/testthat/test_b_special.R b/tests/testthat/test_b_special.R index 2fd5967..d2552d2 100644 --- a/tests/testthat/test_b_special.R +++ b/tests/testthat/test_b_special.R @@ -58,14 +58,14 @@ expect_equal( expect_equal( names(suppressWarnings(write_vc(ds, "test_data_hash", root)))[1], - "2d5325687dcd6a2229b5851bca1c84df6b39da62" + "be6352bd3b0d1b3cd81739a5190c24a277ea16d5" ) expect_silent({ output_test_data_hash <- read_vc("test_data_hash", root) }) expect_equal( names(attr(output_test_data_hash, "source")[1]), - "2d5325687dcd6a2229b5851bca1c84df6b39da62" + "be6352bd3b0d1b3cd81739a5190c24a277ea16d5" ) attr(output_test_data_hash, "source") <- NULL expect_equal(