clairsang
clairsang

Reputation: 125

KnitR: output discrepancy between console and PDF (Encoding)

First, i have 2 vectors of size 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:

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:

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:

[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 . Rnw with knitR.

Output in PDF file:

It shows <U+2208> instead of ∈

Upvotes: 1

Views: 128

Answers (1)

Yihui Xie
Yihui Xie

Reputation: 30114

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"). The second level is the encoding specification in LaTeX, normally via the inputenc package. For example, \usepackage[utf8]{inputenc} if the encoding is UTF-8.

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(). It is difficult to diagnose the problem without more details such as your OS, text editor, and how you called knitr. At least you need to include

library(knitr)
sessionInfo()

Upvotes: 1

Related Questions