gpt4 book ai didi

R source()编码错误?

转载 作者:行者123 更新时间:2023-12-01 13:56:00 25 4
gpt4 key购买 nike

我发现了关于 R 中字符常量编码的非常奇怪的错误。

主.R:

options(encoding = "UTF-8")
print(Sys.getlocale())
print(getOption("encoding"))

print("first run")
source("internal.R")
print("")

print("second run")
source("internal.R", encoding = "UTF-8")
print("")

内部.R

print(Sys.getlocale())
print(getOption("encoding"))
char_constant="Тут не просто живут баги, тут у них гнездо"
print(Encoding(char_constant))

现在让我们看看输出,按下 R 中的源按钮

[1] "ru_RU.UTF-8/ru_RU.UTF-8/ru_RU.UTF-8/C/ru_RU.UTF-8/ru_RU.UTF-8"
[1] "UTF-8"
[1] "first run"
[1] "ru_RU.UTF-8/ru_RU.UTF-8/ru_RU.UTF-8/C/ru_RU.UTF-8/ru_RU.UTF-8"
[1] "UTF-8"
[1] "unknown"
[1] ""
[1] "second run"
[1] "ru_RU.UTF-8/ru_RU.UTF-8/ru_RU.UTF-8/C/ru_RU.UTF-8/ru_RU.UTF-8"
[1] "UTF-8"
[1] "UTF-8"
[1] ""

注意编码的不同。第一次是“未知”,第二次是“UTF-8”。有明显的小错误源代码忽略默认编码参数。

真正的错误是在 data.table 中混合不同的编码会导致很多问题,并且当您只执行一个字符串时 R-studio 使“UTF-8”成为常量,而当您获取整个文件时使“未知”成为常量。

有人知道发生了什么以及如何解决吗?

R version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin14.5.0 (64-bit)
Running under: OS X 10.12.4 (unknown)

locale:
[1] ru_RU.UTF-8/ru_RU.UTF-8/ru_RU.UTF-8/C/ru_RU.UTF-8/ru_RU.UTF-8

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

loaded via a namespace (and not attached):
[1] tools_3.3.0

最佳答案

On Windows, R's source function does not work with files that include characters that aren't part of the current system encoding. You may have trouble with RStudio's Run All and Source on Save commands, as they rely on source.

看看:https://support.rstudio.com/hc/en-us/articles/200532197-Character-Encoding

关于R source()编码错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43789184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com