【问题标题】:Error using special character with reprex::reprex()使用带有 reprex::reprex() 的特殊字符时出错
【发布时间】:2021-02-20 17:58:26
【问题描述】:

我发现reprex::reprex 无法读取¦ 字符(以及其他一些特殊字符,如“é”)并引发错误。有没有办法让 reprex 使用本地编码(这里是法语语言环境)?

Reprex

reprex::reprex(x = {
    tibble::tribble(
        ~var1,          ~var2, ~var3,
        "gr¦pefruit",   "ugli fruit",    4L,
        "huckleberry",      "kumquat",    6L,
        "duri¦n", "blackcurrant",    1L,
        "d¦te",   "cantaloup¦",    1L,
        "fig", "canary m¦lon",    6L,
        "s¦l¦l berry",     "rambutan",    5L
    )
})

查看器窗格中的输出

``` r
tibble::tribble(
    ~var1,          ~var2, ~var3,
    "gr


---
title: reprex_reprex.R
author: cbo
date: '2020-11-08'

---
#> Error: <text>:3:9: INCOMPLETE_STRING inattendu(e)
#> 10: 
#> 11: ---
#>             ^

会话信息

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252   
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C                  
[5] LC_TIME=French_France.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

loaded via a namespace (and not attached):
 [1] pillar_1.4.6      compiler_4.0.2    prettyunits_1.1.1
 [4] remotes_2.1.1     tools_4.0.2       testthat_2.3.2   
 [7] digest_0.6.25     pkgbuild_1.1.0    pkgload_1.1.0    
[10] evaluate_0.14     memoise_1.1.0     lifecycle_0.2.0  
[13] tibble_3.0.3      pkgconfig_2.0.3   rlang_0.4.7      
[16] reprex_0.3.0      cli_2.0.2         rstudioapi_0.11  
[19] yaml_2.2.1        blogdown_0.20.2   xfun_0.15        
[22] stringr_1.4.0     withr_2.2.0       knitr_1.29       
[25] desc_1.2.0        fs_1.4.2          vctrs_0.3.2      
[28] devtools_2.3.0    rprojroot_1.3-2   glue_1.4.1       
[31] R6_2.4.1          processx_3.4.3    fansi_0.4.1      
[34] rmarkdown_2.3     bookdown_0.20     sessioninfo_1.1.1
[37] whisker_0.4       callr_3.4.3       clipr_0.7.0      
[40] magrittr_1.5      backports_1.1.7   ps_1.3.3         
[43] ellipsis_0.3.1    htmltools_0.5.0   usethis_1.6.1    
[46] assertthat_0.2.1  utf8_1.1.4        stringi_1.4.6    
[49] crayon_1.3.4 

更新以波尔卡斯的建议

locale = c("LC_COLLATE" = "French_France.1252")locale = c("LC_COLLATE" = "fr_LU.UTF-8") 均已尝试,错误结果如下:

代码

reprex_locale(x = {
    tibble::tribble(
        ~var1,          ~var2, ~var3,
        "gr¦pefruit",   "ugli fruit",    4L,
        "huckleberry",      "kumquat",    6L,
        "duri¦n", "blackcurrant",    1L,
        "d¦te",   "cantaloup¦",    1L,
        "fig", "canary m¦lon",    6L,
        "s¦l¦l berry",     "rambutan",    5L
    )}, language = "fr",
    locale = c("LC_COLLATE" = "fr_LU.UTF-8")
)

控制台

 Error : callr subprocess failed: <text>:28:1: ')' unexpected
27:         locale = c("LC_COLLATE" = "fr_LU.UTF-8")
28: )
    ^

使用的解决方案 - 用于接下来的解决方案

TL;DR:错误仅限于 OS Windowsreprex 0.3.0,因此安装开发版本解决了问题:

devtools::install_github("tidyverse/reprex")

它发生的原因:正如@cderv 所提到的,并在随附的博客文章中很好地解释了 R in Windows 在编写文件时倾向于使用语言环境编码作为中间步骤。因此,当您在 R-Windows 中使用 writeLines() 时,除非您指定没有中间步骤并且使用字节和 writeLines(..., usebytes = "TRUE"),否则它会弄乱特殊字符。

【问题讨论】:

    标签: r


    【解决方案1】:

    我认为这是由于 windows 和编码是如何工作的。默认情况下,windows 不是 UTF8,而且你有特殊字符,我相信这需要这种编码。

    有时这种行为很棘手,因为 R 将使用默认编码 native,这可能会产生意想不到的结果,因为它会在写入文件之前转换为本机。 看到这篇很棒的帖子:https://kevinushey.github.io/blog/2018/02/21/string-encoding-and-r/

    为了让它发挥作用,我会

    • 将代码写入 R 文件中的 reprex,以 UTF8 编码
    tibble::tribble(
      ~var1,          ~var2, ~var3,
      "gr¦pefruit",   "ugli fruit",    4L,
      "huckleberry",      "kumquat",    6L,
      "duri¦n", "blackcurrant",    1L,
      "d¦te",   "cantaloup¦",    1L,
      "fig", "canary m¦lon",    6L,
      "s¦l¦l berry",     "rambutan",    5L
    )
    
    • 然后在这个文件上使用reprex
    reprex::reprex(input = "test.R")
    

    这对我有用。

    当您直接在reprex::reprex() 中提供代码时,reprex 可能存在关于它如何在 Windows 上写入文件的问题

    【讨论】:

      【解决方案2】:

      即使使用withr 包,也很难重现您的环境。 在我的环境中它总是能正常工作。

      tidyverse 的这个解决方案可能会工作https://github.com/tidyverse/reprex/blob/master/R/reprex-locale.R

      reprex_locale <- function(...,
                                language = "en",
                                locale = NULL) {
        withr::local_envvar(c(LANGUAGE = language))
        if (!is.null(locale)) {
          # If we use withr::local_locale(), the new locale is NOT inherited by the
          # reprexing child process. Whereas it is if we use an env var approach.
          withr::local_envvar(locale)
        }
        reprex(...)
      }
      

      我了解reprex_locale 与 language = "fr" 和某些 locale 可以为您工作。

      【讨论】:

      • 谢谢你的回答。我已经用language = "fr"locale = c(LC_COLLATE = French_France.1252) 尝试了你的建议,并且只有默认版本没有成功:Error: callr subprocess failed: &lt;text&gt;:27:1: unexpected ')'。这还没有在包中导出,因此它可能仍在开发中。
      • 我认为这是一个仅限 Windows 的问题;我在那里运行你的代码时看到了它,但在 macOS 上没有。
      • 我尝试调试它,但它一直在崩溃 R。那里有一个编码错误,但我找不到它。
      • 尝试将您的语言环境更改为 fr_LU.UTF-8。我将从命令行开始并尝试使用 echo sth_french 如果它有效 - 如果它是一个 R 问题。
      • 不错的尝试,但结果保持不变。
      【解决方案3】:

      我发现此解决方法适用于 Windows 和当前 reprex CRAN 版本 (0.3.0),ANSI 字符代码为 \xa6 for ¦

      reprex::reprex(x = {
          dfr <- tibble::tribble(
              ~var1,          ~var2, ~var3,
              "gr\xa6pefruit",   "ugli fruit",    4L,
              "huckleberry",      "kumquat",    6L,
              "duri\xa6n", "blackcurrant",    1L,
              "d\xa6te",   "cantaloup\xa6",    1L,
              "fig", "canary m\xa6lon",    6L,
              "s\xa6l\xa6l berry",     "rambutan",    5L
          )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-02
        • 2014-10-15
        • 1970-01-01
        • 2019-09-06
        • 2021-04-30
        • 1970-01-01
        相关资源
        最近更新 更多