2014-01-25 66 views
1

首先輸出之間的差異,我有大小15的2個載體: lb_result:KnitR:控制檯和PDF(編碼)

cap-color    
cap-color    
odor      
odor      
odor      
odor      
gill-spacing    
gill-size    
gill-color    
stalk-surface-above-ring 
stalk-color-above-ring 
spore-print-color  
spore-print-color  
population    
population 

AND: vl_result:

buff  
pink  
creosote 
foul  
musty  
pungent 
close  
narrow 
buff  
silky  
cinnamon 
chocolate 
green  
scattered 
several 

現在,我想輸出爲:

cap-color ∈ {buff, pink} 
odor ∈ {creosote, foul, musty, pungent} 
gill-spacing = {close} 
gill-size = {narrow} 
gill-color = {buff} 
stalk-surface-above-ring = {silky} 
stalk-color-above-ring = {cinnamon} 
spore-print-color ∈ {chocolate, green} 
population ∈ {scattered, several} 

我寫了一個Rscript爲:

dt <- data.table("Name"=lb_result,"Var"=vl_result) 
    res <- dt[,paste(Var,collapse=","),by=Name] 

    for(i in 1:length(res$V1)){ 
    if (length(grep(",",res$V1[i],value=T)) == 0) { 
    res$V1[i] = paste("= ", res$V1[i]) 
    } else 
    # {res$V1[i] = paste(" \u2208 {", res$V1[i], "}")} 
    {res$V1[i] = paste(" ∈ {", res$V1[i], "}")} 
    } 


    for(i in 1:length(res$V1)){ 
    print(paste(res$Name[i],res$V1[i])) 
    } 

在康壽R,我得到的結果是:

[1] "cap-color ∈ { buff,pink }" 
[1] "odor ∈ { creosote,foul,musty,pungent }" 
[1] "gill-spacing = close" 
[1] "gill-size = narrow" 
[1] "gill-color = buff" 
[1] "stalk-surface-above-ring = silky" 
[1] "stalk-color-above-ring = cinnamon" 
[1] "spore-print-color ∈ { chocolate,green }" 
[1] "population ∈ { scattered,several }" 

但是,當我在一個文件中插入此RSCRIPT。與knitR的Rnw。

輸出的PDF文件:

它顯示的<U+2208>代替∈

回答

1

有在這個問題上編碼的至少兩個層次。你必須確保兩個都是正確的。第一個級別是你的Rnw文件的文件編碼,你需要傳遞給knit(..., encoding = "Your File Encoding")。第二個級別是LaTeX中的編碼規範,通常通過inputenc包。例如,如果編碼爲UTF-8,則爲\usepackage[utf8]{inputenc}

如果您通過例如source()將其作爲獨立文件執行,則可以有第三級文件編碼,即R腳本的編碼。如果沒有更多細節(如操作系統,文本編輯器以及您如何調用knitr),則很難診斷問題。至少你需要包括

library(knitr) 
sessionInfo()