From 363c34b84e769a0000a06ec5b100e252212289cc Mon Sep 17 00:00:00 2001 From: Kainhofer Reinhold <reinhold@kainhofer.com> Date: Wed, 21 Aug 2019 12:18:29 +0200 Subject: [PATCH] Modify mortalityTables.load to force UTF-8 for all input files -) sys.source on Windows uses the default locale and NOT UTF-8, so all UTF8-strings will be broken. Instead explicitly use the same code (readLines with subsequent parse and eval calls) as sys.source, but add the UTF-8 encoding to readLines. --- R/mortalityTables.load.R | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/R/mortalityTables.load.R b/R/mortalityTables.load.R index 9cd5e1f..ce44f7b 100644 --- a/R/mortalityTables.load.R +++ b/R/mortalityTables.load.R @@ -31,8 +31,18 @@ mortalityTables.load = function(dataset, package = c("^MortalityTables", "^Pensi if (any(sapply(package, grepl, p))) { # package matches the pattern given as argument filename = system.file("extdata", paste(prefix, "_", sname, ".R", sep = ""), package = p); if (filename != "") { - require(p, character.only = TRUE) # Make sure the providing package is loaded, in case it provides helper functions - sys.source(filename, envir = globalenv()) + # Make sure the providing package is loaded, in case it provides helper functions + require(p, character.only = TRUE) + + # Taken from the definition of sys.source and adjusted to include the + # encoding (required for Windows, otherwise UTF8-strings will be broken!) + lines = readLines(filename, encoding = "UTF-8", warn = FALSE) + srcfile = srcfilecopy(filename, lines, file.mtime(filename), isFile = TRUE) + exprs = parse(text = lines, srcfile = srcfile, keep.source = TRUE) + for (i in seq_along(exprs)) + eval(exprs[i], envir = globalenv()) + + # sys.source(filename, envir = globalenv()) loaded = TRUE } } -- GitLab