(I have posted this qustion to Stackoverflow, but I didn't got any answer. Sorry for the crossposting!)
I want to use parametrized reports in RStudio. But when I use params with swedish characters with umlauts (like å, ä ö) something goes wrong with the encoding. I'm running Windows 10 on my computor.
Edit: This code work when I knit the chunk but not when I run it.
---
title: "test_yaml_encoding"
output: html_document
params:
swe_chars_param: "Swedish municipalities: Åre, Östersund, Älmhult, Mölndal"
sv_umlauts: "åäöÅÄÖ"
---
{r}
ffix_swedish_chars <- function(txt) {
# convert the string to a character string of hexvalues that can be
# used as input to gsub
hex_str <-
paste(as.character(charToRaw(txt)), collapse = "")
# Replace the hexcode that represent the wrong encoded chars with the righ hexcodes
hex_str <- gsub("c383c2a5", "e5", hex_str) # å
hex_str <- gsub("c383c2a4", "e4", hex_str) # ä
hex_str <- gsub("c383c2b6", "f6", hex_str) # ö
hex_str <- gsub("c383e280a6", "c5", hex_str) # Å
hex_str <- gsub("c383e2809e", "c4", hex_str) # Ä
hex_str <- gsub("c383e28093", "d6", hex_str) # Ö
# Split the hexcode string to a vector of chars where every element is 2 chars long
hex_vec <-
substring(hex_str, seq(1, nchar(hex_str) - 1, 2), seq(2, nchar(hex_str), 2))
# Transform the character vector of hexcodes to integers and then to a
# vector of type raw. Then transfer the raw vector to a character vector.
txt_correct <-
rawToChar(as.raw(strtoi(hex_vec, base = 16L)))
Encoding(txt_correct) <- "UTF-8"
return(txt_correct)
}
swe_chars_param <- unname(ffix_swedish_chars(txt = params$swe_chars_param))
sv_umlauts <- unname(ffix_swedish_chars(txt = params$sv_umlauts))
# Check if the result realy is identical to a string defined in the chunk
identical(swe_chars_param, "Swedish municipalities: Åre, Östersund, Älmhult, Mölndal")
# [1] TRUE