代码之家  ›  专栏  ›  技术社区  ›  TarJae

来源R文件到Rmarkdown不适用于德语元音变音符

  •  1
  • TarJae  · 技术社区  · 2 年前

    为了精简我的Rmarkdown代码,我想在R文件中写一些代码,然后源代码到Rmarkdown:

    my_script_01.R

    library(tidyverse)
    df1 <- tribble(
      ~id, ~col1,
      1, "Äb",
      2, "Ab",
      3, "ÖB",
      4, "OB"
    )
    df1
    df2 <- tibble(id = c(1:4),
                  tester = c("umlaut", "no_umlaut", "umlaut", "no_umlaut"))
    df3 <- left_join(df1, df2, by="id")
    

    原则上,它运行良好:

    1. 只有当我用德语元音变音符(¥,等)来源代码时,源代码不能正常工作,它会给出奇怪的字母(蓝色矩形,代码1)。

    2. 如果我把代码放在r标记块中,那么一切都很完美(红圈,代码2)

    Rmd代码1:

    ---
    title: "test"
    author: "Me"
    date: '2022-06-25'
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(tidyverse)
    source("my_script_01.R")
    ```
    
    
    ```{r}
    
    df3 %>% 
      count(col1) %>%
      ggplot(aes(x=col1, y=n)) +
      geom_col()+
      theme_minimal(base_size = 26) 
    ```
    

    enter image description here

    Rmd代码2:

    ---
    title: "test"
    author: "Me"
    date: '2022-06-25'
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(tidyverse)
    source("my_script_01.R")
    ```
    
    
    ```{r}
    df1 <- tribble(
      ~id, ~col1,
      1, "Äb",
      2, "Ab",
      3, "ÖB",
      4, "OB"
    )
    df2 <- tibble(id = c(1:4),
                  tester = c("umlaut", "no_umlaut", "umlaut", "no_umlaut"))
    df3 <- left_join(df1, df2, by="id")
    
    df3 %>% 
      count(col1) %>%
      ggplot(aes(x=col1, y=n)) +
      geom_col()+
      theme_minimal(base_size = 26) 
    ```
    

    enter image description here

    我的默认文本编码是UTF-8。 我想知道为什么会出现这种行为,以及是否有可能从德语元音变音符中找到来源。

    例如,我环顾四周 here here 但无法找到解决方案。

    1 回复  |  直到 2 年前
        1
  •  2
  •   Robert Hacken    2 年前

    指定源文件的编码应该有助于:

    source("my_script_01.R", encoding="UTF-8")