2016年2月3日 星期三

匯入 UTF-8 編碼的中文檔案並在 R-Studion Console中顯示中文字

環境: Win7, RStudio Version 0.99.491

原理:
1. 先在 Single Byte Character Set 環境中讀入 UTF-8 編碼的中文字
2. 再轉成 Double Byte Character Set 環境顯示內容

1:  setwd("d:/R-Workshop/CaseStudy")  
2:  # Change to single byte character set to read UTF-8 encoded characters  
3:  Sys.setlocale("LC_CTYPE", "us")  
4:  l10n_info()  
5:  # Read the csv file encoded with UTF-8   
6:  my.data <- read.csv(file="UV_20151116152215.csv", header = TRUE, encoding="UTF-8")  
7:  # Show content with the single byte character set  
8:  # The RStudio Console cannot display the Chinese characters correctly in   
9:  # the Single-Byte-Character-Set environment.  
10:  head(my.data)  
11:  # Change to Double-Byte-Character-Set environment  
12:  Sys.setlocale("LC_CTYPE", "cht")  
13:  # Now your console can display Chinese characters correctly.  
14:  head(my.data)  
15:  rm(my.data)