(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.
Example:
title: "test_yaml_encoding"
output: html_document
params:
swe_chars_param: "åäöÅÄÖ"
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
print(params$swe_chars_param)
```
[1] "åäöÅÄÖ"
It seems to be a known issue:
but I have not mangaged to find a solution.
One solution is to fix the encoding with a function. I have tried two different solutions.
First try:
ffix_swedish_chars <- function(txt) {
txt <- gsub("Ã¥", "å", txt)
txt <- gsub("ä", "ä", txt)
txt <- gsub("ö", "ö", txt)
txt <- gsub("Ã…", "Å", txt)
txt <- gsub("Ä", "Ä", txt)
txt <- gsub("Ö", "Ö", txt)
}
print(ffix_swedish_chars(params$swe_chars_param))
Result:
[1] "åäöÅÄÖ"
It worked, but only for lower case letters.
Then I tried to use brute force and tried to loop through all possible encodings to UTF-8
library(utf8)
library(purrr)
library(readr)
koder <- iconvlist()
ftest_kodning <- function(str, kod) {
iconv(str, from = kod, to = "UTF-8")
}
ftest_kodning_safe <- possibly(ftest_kodning, NA)
for (i in 1:length(koder)) {
print(paste(koder[i], ftest_kodning_safe(params$swe_chars_param, koder[i])))
}
I couldn't find any combination of encoding that worked.
Now I'm stuck. Does anyone have a solution?
Edit:
I don't know if this is of any help but this is the raw codes for the characters:
print(charToRaw(params$swe_chars_param))
[1] c3 83 c2 a5 c3 83 c2 a4 c3 83 c2 b6 c3 83 e2 80 a6 c3 83 e2 80 9e c3 83 e2 80 93