r - 通过SQL从数据库导入数据时如何让注册商标标志在R中正确显示
问题描述
我不确定这是 R/R Studio 问题本身还是与我的计算机相关的 Java 问题。我无法将注册商标符号(带圆圈的 R)显示在通过 SQL 从数据库导入的数据中。但是,当我通过 Excel 下载此数据时(注册商标符号被正确读取),将商标符号从 Excel 文件读入 R/R Studio 没有问题。根据我的阅读,这个 unicode 字符 <U+00AE> 在 Latin-1 Supplement 中,所以我不知道这是否是我的 R Studio 设置的问题。
这是我的会话信息。
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] DBI_1.1.0 odbc_1.2.2 feasts_0.1.6 fable_0.2.1 fabletools_0.2.0 tsibble_0.9.1
[7] lubridate_1.7.4 writexl_1.2 forcats_0.4.0 stringr_1.4.0 dplyr_1.0.0 purrr_0.3.3
[13] readr_1.3.1 tidyr_1.1.0 tibble_3.0.3 ggplot2_3.3.0 tidyverse_1.3.0 readxl_1.3.1
loaded via a namespace (and not attached):
[1] tidyselect_1.1.0 haven_2.2.0 lattice_0.20-38 colorspace_1.4-1 vctrs_0.3.2
[6] generics_0.0.2 blob_1.2.1 utf8_1.1.4 rlang_0.4.7 pillar_1.4.6
[11] glue_1.4.0 withr_2.1.2 bit64_0.9-7 dbplyr_1.4.2 modelr_0.1.5
[16] distributional_0.1.0 lifecycle_0.2.0 munsell_0.5.0 anytime_0.3.7 gtable_0.3.0
[21] progressr_0.6.0 cellranger_1.1.0 rvest_0.3.5 RDCOMClient_0.93-0 fansi_0.4.0
[26] broom_0.5.4 Rcpp_1.0.3 scales_1.1.0 backports_1.1.5 jsonlite_1.6.1
[31] bit_1.1-15.2 farver_2.0.1 fs_1.5.0 hms_0.5.2 digest_0.6.25
[36] stringi_1.5.3 grid_3.6.1 cli_2.0.2 tools_3.6.1 magrittr_1.5
[41] crayon_1.3.4 pkgconfig_2.0.3 ellipsis_0.3.0 xml2_1.3.2 reprex_0.3.0
[46] assertthat_0.2.1 httr_1.4.1 rstudioapi_0.11 R6_2.4.1 nlme_3.1-140
[51] compiler_3.6.1
这是我在控制台中调用该函数时选择文件列的预览,head()
为保护知识产权而匿名。
MFMCU MFLITM IMDSC1 MFAN8 IMUOM1 UMCONVF
1 PBK0100 123456 product<U+00AE> 559286 DR 50
解决方案
在做了更多阅读之后,这是我找到的解决方案。
我意识到我在相关列中混合了编码
table(stri_enc_mark(forecast.file$IMDSC1))
ASCII UTF-8
658 184
UTF-8 项是注册商标 unicode 未被 R 识别的行。
有两种方法可以解决这个问题。
Encoding(forecast.file$IMSDSC1) <- "latin1"
forecast.file$IMDSC1 <- iconv(forecast.file$IMDSC1, from = "latin1", to = "UTF-8")
或者,每个强制字符向量编码从 R 中的“未知”到“UTF-8”
您可以写入 csv 并将其读回,将 latin-1 指定为编码类型。
library(data.table)
fwrite(forecast.file, "temp.csv")
forecast.file <- fread("temp.csv", encoding = "Latin-1")
推荐阅读
- css - 如何使我的页面内容停止使用页面正文进行填充
- database - 在 MongoDB 升级期间迁移数据库
- python - 无法安装 imblearn 以使用 SMOTE
- algorithm - 如何生成相邻数字在指定范围内的随机唯一数字?
- mysql - 内连接和全外连接查询
- ios - 重置世博会权限
- file - 什么是 .nsh 文件?找不到任何文档
- javascript - 在“let”中将字符串作为对象的属性传递给“Unexpected token }”
- javascript - react-native 设置全局状态并使用 redux 回调
- node.js - 使用 Node 和 Angular 调用 Google Calendar API 中的变量范围