将下面的文本写入缓冲区,并将其另存为.r
脚本:
letters_fa <- c('الف','ب','پ','ت','ث','ج','چ','ح','خ','ر','ز','د')
然后尝试使用以下代码行来source()
它:
script <- "path/to/script.R"
file(script,
encoding = "UTF-8") %>%
readLines() # works fine
file(script,
encoding = "UTF-8") %>%
source() # works fine
source(script) # the Farsi letters in the environment are misrepresented
source(script,
encoding = "UTF-8") # gives error
最后一行抛出错误。我尝试对其进行调试,但我认为source
函数中存在以下代码行中的错误:
...
loc <- utils::localeToCharset()[1L]
...
在.Internal(parse(
线路上出现错误。
...
exprs <- if (!from_file) {
if (length(lines))
.Internal(parse(stdin(), n = -1, lines, "?",
srcfile, encoding))
else expression()
}
else .Internal(parse(file, n = -1, NULL, "?", srcfile,
encoding))
...
确切的错误是:
Error in source(script, encoding = "UTF-8") :
script.R:2:17: unexpected INCOMPLETE_STRING
1: #' @export
2: letters_fa <- c('
^
发布于 2018-08-23 22:15:47
此问题的解决方案是将OS区域设置更改为本机区域设置(例如,本例中为波斯语),或者使用R内置函数Sys.setlocale(locale="Persian")
来更改R会话本机区域设置。
发布于 2018-08-08 18:48:39
在不指定编码的情况下使用source
,然后使用Encoding
修改矢量的编码
source(script)
letters_fa
# [1] "الÙ\u0081" "ب" "Ù¾" "ت" "Ø«"
# [6] "ج" "Ú†" "Ø" "Ø®" "ر"
# [11] "ز" "د"
Encoding(letters_fa) <- "UTF-8"
letters_fa
# [1] "الف" "ب" "پ" "ت" "ث" "ج" "چ" "ح" "خ" "ر" "ز" "د"
https://stackoverflow.com/questions/51753429
复制相似问题