读取拉丁字符时的编码问题

时间:2014-10-07 14:20:08

标签: r encoding shiny

我需要在我的闪亮应用中阅读一些txt表,其中一些列是文本。当我读一张拉丁字符的表(á,â,ó,é,ô,ç......)时,它显示为 。我想我需要使用UTF-8作为编码来阅读它,我尝试了以下代码,但它对我不起作用。

test<-read.delim("test.txt",sep="\t",dec=",",header=TRUE, encoding="UTF-8") or
test<-read.table(file = "test.txt", dec = ",", fileEncoding = "utf-8")

这是一个重现我的问题的简短脚本:

x<- matrix(c('áeiou','aêiou','aeíou','aeiõu','aeiouç',1:5),ncol=2)
write.table(x,"test.txt",sep="\t")
test<-read.delim("test.txt",sep="\t",header=TRUE)

我使用的是Windows操作系统,我有这个信息:

localeToCharset()
[1] "ISO8859-1"


sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)

locale:
  [1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252   
[3] LC_MONETARY=Portuguese_Brazil.1252 LC_NUMERIC=C                      
[5] LC_TIME=Portuguese_Brazil.1252  

提前致谢!!

1 个答案:

答案 0 :(得分:1)

适合我:

> Sys.getlocale()
[1] "LC_COLLATE=English_Ireland.1252;LC_CTYPE=English_Ireland.1252;LC_MONETARY=English_Ireland.1252;LC_NUMERIC=C;LC_TIME=English_Ireland.1252"
> x<- matrix(c('áeiou','aêiou','aeíou','aeiõu','aeiouç',1:5),ncol=2)
> write.table(x,"test.txt",sep="\t")
> test<-read.delim("test.txt",sep="\t",header=TRUE)
> test
      V1 V2
1  áeiou  1
2  aêiou  2
3  aeíou  3
4  aeiõu  4
5 aeiouç  5
> sessionInfo()$platform
[1] "x86_64-w64-mingw32/x64 (64-bit)"

Sys.setlocale(category = "LC_ALL", locale = "English_Ireland.1252")

或某些类似的语言环境。