Hon*_*ong 6 encoding r package roxygen
我正在构建自己的包,但我一直遇到编码问题,因为我的包中的函数具有非英语(非 ASCII)字符。
从本质上讲,韩文字符是我包中许多功能的一部分。示例函数:
library(rvest)
sampleprob <- function(url) {
# sample url: "http://dart.fss.or.kr/dsaf001/main.do?rcpNo=20200330003851"
result <- grepl("?????? ??", html_text(read_html(url)))
return(result)
}
Run Code Online (Sandbox Code Playgroud)
但是,在安装软件包时,我遇到了编码问题。
我创建了一个示例包 ( https://github.com/hyk0127/KorEncod/ ),只有一个函数(如上所示),并将其上传到我的 github 页面上作为一个可重现的示例。我运行以下代码进行安装:
library(devtools)
install_github("hyk0127/KorEncod")
Run Code Online (Sandbox Code Playgroud)
以下是我看到的错误消息
Error : (converted from warning) unable to re-encode 'hello.R' line 7
ERROR: unable to collate and parse R files for package 'KorEncod'
* removing 'C:/Users/myname/Documents/R/win-library/3.6/KorEncod'
* restoring previous 'C:/Users/myname/Documents/R/win-library/3.6/KorEncod'
Error: Failed to install 'KorEncod' from GitHub:
(converted from warning) installation of package ‘C:/Users/myname/AppData/Local/Temp/RtmpmS5ZOe/file48c02d205c44/KorEncod_0.1.0.tar.gz’ had non-zero exit status
Run Code Online (Sandbox Code Playgroud)
错误信息 aboutline 7
是指函数中的韩文字符。
可以使用 tar.gz 文件在本地安装该包,但是该功能无法按预期运行,因为韩语字符在损坏的编码中被识别。
这不可能是第一次有人尝试构建具有非英语(或非 ASCII)字符的包,但我找不到解决方案。任何帮助将不胜感激。
我认为相关的一些信息:
目前该DESCRIPTION
文件指定“编码:UTF-8”。
我曾经sys.setlocale
将语言环境设置为韩语并返回无济于事。我也指定@encoding UTF-8
了该函数也无济于事。
我目前使用的是管理语言设置为英语的 Windows。我尝试使用不同的笔记本电脑,并将 Windows 和管理语言设置为韩语,但出现了同样的问题。
关键技巧是用 unicode 代码(编码)替换非 ASCII 字符\\uxxxx
。
这些可以通过函数生成stringi::stri_escape_unicode()
。
请注意,由于需要完全删除代码中的韩文字符才能通过 R CMD 检查,因此有必要通过在{stringi}
命令行上执行手动复制和重新编码并在所有内容上粘贴回操作您的 R 脚本包含在包中。
我不知道这个问题有可用的自动化解决方案。
\n在所提供示例的特定用例中,unicode 将如下所示:
\nsampleprob <- function(url) {\n # stringi::stri_escape_unicode("\xec\x97\xb0\xea\xb2\xb0\xec\x9e\xac\xeb\xac\xb4\xec\xa0\x9c\xed\x91\x9c \xec\xa3\xbc\xec\x84\x9d") to get the \\uxxxx codes\n result <- grepl("\\uc5f0\\uacb0\\uc7ac\\ubb34\\uc81c\\ud45c \\uc8fc\\uc11d", \n rvest::html_text(xml2::read_html(url)))\n return(result)\n}\nsampleprob("http://dart.fss.or.kr/dsaf001/main.do?rcpNo=20200330003851")\n[1] TRUE\n
Run Code Online (Sandbox Code Playgroud)\n这会很麻烦,但它似乎是使代码平台中立的唯一方法(这是关键的 CRAN 要求,因此需要接受 R CMD 检查)。
\n