2

I am writing a table in R which contains Greek characters and am not able to export the table in CSV or txt file (I want to call the table later in an Latex file).

    #example table:
parm1 <- 2
parm2 <- 0.3
rownames_tab <- c(  paste('\u2126', "_a", sep="")  , paste('\u025B',"_a", sep="") )
tab1 <- as.data.frame( matrix(ncol=1, nrow=length(rownames_tab ) ) )
row.names(tab1) <- rownames_tab
tab1[paste('\u2126', "_a", sep=""),] <- paste("Some explanation of the variable: ", parm1, sep="")
tab1[paste('\u025B', "_a", sep=""),] <- paste("Some explanation of the second variable: ", paste('\u025B', "_a", sep=""), " = " ,parm2, sep="" )  

How to save the table in a csv or txt file which contains the greek characters (encoded as utf-8)?

write.csv(tab1, file="test1.csv", fileEncoding = "UTF-8")
write.table(tab1, file="test1.txt",  fileEncoding = "UTF-8")

This does not seem to work: if you open these files they do not read the Greek characters.

Thanks a lot in advance for your help,

Best,

Maurane

Maurane R
  • 21
  • 2
  • Which OS are you using? Your example worked for me sing Linux with a UTF-8 locale. – Ralf Stubner Apr 23 '18 at 15:13
  • Possible duplicate of https://stackoverflow.com/questions/10675360/utf-8-file-output-in-r. Also discussion in https://stackoverflow.com/questions/28483441/how-does-r-handle-unicode-utf-8 may help – camille Apr 23 '18 at 15:25
  • @camille Thanks for the links. I went through these discussion but could not find an optimal solution for a table. However, the function write.table(tab$V1, "test.txt", useBytes=T) works but only if I put a column, not for the entire table. – Maurane R Apr 24 '18 at 08:56
  • @RalfStubner Thanks for your rapid feed-back. I am using Windows, with what seems to be a "German_Switzerland.1252" locale – Maurane R Apr 24 '18 at 08:56

1 Answers1

0

I found a solution to my problem, or a way to go around this problem. Since I want to use the table on latex, I directly write my table in R with a latex syntax:

rownames_tab <- c(  "$\\omega_{a}$" ,  "$\\epsilon_{a}$" )
tab1 <- as.data.frame( matrix(ncol=1, nrow=length(rownames_tab ) ) )
row.names(tab1) <- rownames_tab

tab1[ "$\\omega_{a}$",] <- paste("Some explanation of the variable: ", parm1, sep="")
tab1[ "$\\epsilon_{a}$",] <- paste("Some explanation of the second variable: ",  "$\\epsilon_{a}$", " = " ,parm2, sep="" ) 

and save it in a way that it can be read by latex:

write.table(tab1 , "myparams.txt", quote=FALSE, eol="\\\\\n", sep=" & ", col.names = F)

And in latex I write:

\begin{table*}  \caption{my parameters} 
    \begin{tabular}{ll}
        \hline
    Param. & Description \\
    \input{myparams.txt}
    \hline
    \end{tabular}
    \label{tab:params}
\end{table*} 

This correctly shows the Greek symbols in my latex table.

Maurane R
  • 21
  • 2