我发现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蒂布尔::特里布(
~var1, ~var2, ~var3,"gr标题: reprex_reprex.R
作者: cbo
日期:“2020-11-08”
#>错误::3:9: INCOMPLETE_STRING inattendu(e)
#> 10:
#> 11:
#> ^
**Session Info**
```javascriptR版本4.0.2 (2020-06-22)
平台:x86_64-W64-mingw32 32/x64(64位)
运行于: Windows 10 x64 (build 18363)
矩阵产品:默认
现场:
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
随附的基本包件:
1个stats图形grDevices使用数据集方法
7个基地
通过命名空间加载(而不是附加的):
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
**Update** with polkas' suggestion
Both `locale = c("LC_COLLATE" = "French_France.1252")` and `locale = c("LC_COLLATE" = "fr_LU.UTF-8")` have been tried with the following error result :
**code**
```javascriptreprex_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"))
**console**
```javascript错误: callr子进程失败::28:1:')‘意外
27: locale = c("LC_COLLATE“= "fr_LU.UTF-8")
28:)
^----
_**Solution used - for those that may come next**_
**TL;DR** : the error is restricted to `OS Windows` with `reprex 0.3.0` so installing the dev version fixed the issue :
```javascriptdevtools::install_github("tidyverse/reprex")
为什么会发生:正如@cderv所提到的,并在附带的博客文章R中解释得很好,在编写文件时,倾向于使用地区编码作为中间步骤。因此,当您在R中使用writeLines()时,除非您没有指定中间步骤,并且一直使用字节和writeLines(..., usebytes = "TRUE"),否则它会搞乱特殊的字符。
发布于 2020-11-16 14:36:40
我认为这是由于窗口和编码是如何工作的。默认情况下,窗口不是UTF8,而且您有特殊的字符,我相信这需要编码。
有时,这种行为很棘手,因为R将使用默认编码,即native,这可能会产生意想不到的结果,因为在写入文件之前,它将转换为本机。请看这篇伟大的文章:https://kevinushey.github.io/blog/2018/02/21/string-encoding-and-r/
为了让这件事成功,我会
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上编写文件的
发布于 2020-11-08 18:28:33
即使使用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(...)
}我理解带有language = "fr“的reprex_locale和特定的locale可以为您工作。
发布于 2020-11-16 13:21:06
我已经在Windowsand当前的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
)https://stackoverflow.com/questions/64741078
复制相似问题