简体   繁体   English

KnitR:控制台和PDF(编码)之间的输出差异

[英]KnitR: output discrepancy between console and PDF (Encoding)

First, i have 2 vectors of size 15: lb_result: 首先,我有2个大小为15的向量:lb_result:

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: AND:vl_result:

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

Now, i want an output as: 现在,我希望输出为:

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}

I wrote a Rscript as: 我写的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]))
  }

In Consol R, i got the result: 在Consol 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 }"

But, when i insert this Rscript in a file . 但是,当我将此Rscript插入文件中时。 Rnw with knitR. 与knitR的Rnw。

Output in PDF file: PDF文件输出:

It shows <U+2208> instead of ∈ 它显示<U+2208>而不是∈

There are at least two levels of encodings in this problem. 此问题中至少有两个级别的编码。 You have to make sure both are correct. 您必须确保两者均正确。 The first level is the file encoding of your Rnw file, which you need to pass to knit(..., encoding = "Your File Encoding") . 第一层是Rnw文件的文件编码,您需要将其传递给knit(..., encoding = "Your File Encoding") The second level is the encoding specification in LaTeX, normally via the inputenc package. 第二层是LaTeX中的编码规范,通常通过inputenc包进行。 For example, \\usepackage[utf8]{inputenc} if the encoding is UTF-8. 例如,如果编码为UTF-8, \\usepackage[utf8]{inputenc}

There can be a third level of file encoding, which is the encoding of your R script, if you executed it as a standalone file via, for example, source() . 如果您通过例如source()其作为独立文件执行,则可能存在第三级文件编码,即R脚本的编码。 It is difficult to diagnose the problem without more details such as your OS, text editor, and how you called knitr . 如果没有更多详细信息,例如操作系统,文本编辑器以及如何调用knitr ,就很难诊断出问题。 At least you need to include 至少你需要包括

library(knitr)
sessionInfo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM