1

I am using the sj packages within R Markdown. However, I am encountering encoding problems.

My problem: If you run the following lines, you'll get one cross table with encoding problems and a second cross table, which solves this issue.

library(sjPlot)
library(sjmisc)
library(sjlabelled)

Var1 <- factor(sample(1:6, 200, replace = T))
Var1 <- factor(Var1,
           levels = c("1","2","3","4","5", "6"),
           labels = c("Zu Hause", 
                      "Am Im Büro", 
                      "In der Schule bzw. an der Universität",
                      "An einem öffentlichen Ort",
                      "Unterwegs",
                      "Außer Haus"))
sjt.xtab(Var1, Var1)
sjt.xtab(Var1, Var1, encoding = "UTF-8")

Pic1: without encoding=UTF8 enter image description here

Pic2: with encoding=UTF8 [enter image description here][

However, if you try to run this in a Rmd-file, the solution with encoding = "UTF-8" doesn't work anymore.

```{r echo = FALSE}
library(sjPlot)
library(sjmisc)
library(sjlabelled)

Var1 <- factor(sample(1:6, 200, replace = T))
Var1 <- factor(Var1,
           levels = c("1","2","3","4","5", "6"),
           labels = c("Zu Hause", 
                      "Am Im Büro", 
                      "In der Schule bzw. an der Universität",
                      "An einem öffentlichen Ort",
                      "Unterwegs",
                      "Außer Haus"))
sjt.xtab(Var1, Var1)
sjt.xtab(Var1, Var1, encoding = "UTF-8")
```

The output looks like Pic1, but should look like Pic2

Or

      `r sjt.xtab(Var1, Var1, 
       title = "<br/><h1> Ort /h1> <br/>Beschreibung: Bla Bla.",
       wrap.labels = 5555,
       encoding = "UTF-8",
       no.output=TRUE)$knitr`

The output looks like Pic1, but should look like Pic2

A rmarkdown::render("File_in.Rmd", output_file = "File_out.html", encoding = "UTF-8") doesn't help as well.

I think, the problem always occur, when the labels of the second variable (gives the rows) in sjt.xtab() got umlauts (ä,ü,ö,ß).

It would be great to hear your solutions!
Julian

Julian B.
  • 43
  • 5
  • This is working, but it's not very nice.... ``Var1 <- factor(sample(1:6, 200, replace = T)) Var1 <- factor(Var1, levels = c("1","2","3","4","5", "6"), labels = c("Zu Hause", "Am Im Büro", "In der Schule bzw. an der Universität", "An einem öffentlichen", "Unterwegs", "AußerHaus")) sjt.xtab(Var1, Var1) sjt.xtab(Var1, Var1, encoding = "UTF-8")`` – Julian B. Apr 18 '18 at 11:03
  • Maybe I could automatically convert characters like Umlauts in knitr-files, so you don't need to care about encoding. I had this issue before with another locale, but I don't know how to fix this with character encoding like UTF-8 in knitr. – Daniel Apr 21 '18 at 15:08
  • @Daniel: Do you think the problem roots in the packages by itself and not in the way I use it? – Julian B. Apr 23 '18 at 07:39
  • I think so, but I'm, not sure where the encoding-issue exactly arises, e.g. if it's a knitr-problem or not. – Daniel Apr 23 '18 at 17:05

0 Answers0