如何在R Studio中正确显示中文文本?

问题描述 投票:0回答:1

我是中国人,经常与汉字打交道。我仍然找不到正确编码中文的方法。

到目前为止最好的方法是read.csv(file_name, fileEncoding = "UTF8")。但是,当我使用wd列出dir()中的所有文件时,出现乱码:

> dir()
[1] "16PF.csv"       "codebook.html"  "Table_2.xls"   
[4] "win-library"    "鏈牎鏂囩.csv" "鏈牎鏂囩.R"

这是在我设置系统区域设置之后:

> Sys.setlocale(category = "LC_ALL", locale = "chs") #cht for traditional Chinese, etc.
[1] "LC_COLLATE=Chinese_People's Republic of China.936;LC_CTYPE=Chinese_People's Republic of China.936;LC_MONETARY=Chinese_People's Republic of China.936;LC_NUMERIC=C;LC_TIME=Chinese_People's Republic of China.936"

Global Options-> Code-> Saving中,默认编码设置为UTF8。

我也希望R Studio阅读中文文本,而不必每次打开都使用命令。

r rstudio chinese-locale
1个回答
2
投票

不需要很长。带有错误输出某些字符的单个输出的闪亮应用程序将起作用。我的快速示例

library(shiny)

shinyApp(
  ui = fluidPage("昨日すき焼きを食べました"),
  server = function(input, output) {}
)



sessionInfo()
# R version 3.5.1 (2018-07-02)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows >= 8 x64 (build 9200)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
# [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.1252    
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] shiny_1.1.0
# 
# loaded via a namespace (and not attached):
#  [1] compiler_3.5.1  magrittr_1.5    R6_2.2.2        rsconnect_0.8.8 promises_1.0.1 
#  [6] later_0.7.2     htmltools_0.3.6 tools_3.5.1     yaml_2.1.19     Rcpp_0.12.17   
# [11] jsonlite_1.5    digest_0.6.15   xtable_1.8-2    httpuv_1.4.3    mime_0.5
© www.soinside.com 2019 - 2024. All rights reserved.