Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am saving tables using write.table in R using the following code:

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
model <- z ~ x + y
results <- glm(model)
pe <- results$coefficients
vc <- vcov(results)
se <- sqrt(diag(vc))
results.table <- cbind(pe, se)
rownames(results.table) <- c("Intercept", "X-Estimate", "Y-Estimate")
write.table(results.table,file="test.csv",row.names=T,col.names=NA,sep=",")

When I then try to read in the table using pgfplot, there are quotes around all non-numeric values:

% Set up
\documentclass{article}
\usepackage{pgfplotstable,filecontents,booktabs}
\pgfplotstableread[col sep=comma]{test.csv}\tablea

%Start the document
\begin{document}
% Table a

\pgfplotstabletypeset[
string type,
columns/pe/.style={column name=PE, column type = {r}},
columns/se/.style={column name=SE, column type = {r}},
every head row/.style={column type={|l}, before row={\toprule},after row=\midrule},
every last row/.style={after row={\toprule}},
]\tablea

\end{document}

I (obviously) don't want double quotes around all of my string (column and row titles). I also am unable to adjust the styles of the columns using the above code, perhaps because of the absent first column title (which I want to leave blank). Help on either of these issues would be appreciated, this is a full working example. Thanks,

share|improve this question
add comment

2 Answers

Use the quote argument like this:

write.table(results.table,file="c:/test.csv",row.names=T,col.names=NA,sep=",", quote = FALSE)

That should get rid of the quotes. Incidentally, for future R-specific questions (which this is) you will likely get a far better response on StackOverflow rather than in this part of StackExchange.

UPDATE - I misunderstood the question. The original poster wants to keep the "" that write.table inserts into the column names but he doesn't want the quotes. If he omits the quotes, that "" doesn't get written. So writing the header and the matrix separately should work as per screenshot below:

screenshot

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)
model <- z ~ x + y
results <- glm(model)
pe <- results$coefficients
vc <- vcov(results)
se <- sqrt(diag(vc))
results.table <- cbind(pe, se)
rownames(results.table) <- c("Intercept", "X-Estimate", "Y-Estimate")
## Doing it this way leads to unwanted quotes
write.table(results.table,file="c:/original.csv",row.names=T,col.names=NA,sep=",")

## Better way - first open file for reading
my.con <- file("c:/revised.csv", "w")
## Write the header, with quotes in the first column
writeLines('"",pe,se', my.con)
## Write the rest of the table as normal, without quotes
write.table(results.table, my.con, row.names = T, col.names = F, sep = ",", quote = FALSE)
close(my.con)
share|improve this answer
1  
This doesn't work, because it leaves the first column of the header row completely empty, which trips PGFPlots. –  Jake Nov 15 '12 at 7:08
    
I see. With quote = TRUE you get "","pe","se" and with quote = FALSE you get ,pe,se which throws your PGFPlots. So if the header contains three columns, it should work? Aah, but the original poster wants to have only those two column titles. Mmmm. –  SlowLearner Nov 15 '12 at 7:23
add comment

As SlowLearner suggested, you can get rid of the quotation marks in the data table by setting quote=F in your R script. However, this leaves the column name for the first column empty, which PGFPlotstable can't handle.

I've modified the PGFPlotstable function that assigns the column names to check whether the column name would be empty, and replaces empty column names with the string empty, so you can work with it like with a "normal" column (for example, setting /columns/empty/.style={column name={}} to prevent \pgfplotstabletypeset from printing the empty string).

\documentclass{article}
\usepackage{pgfplotstable,filecontents,booktabs}

%% Redefine an internal PGFPlotstable macro to check for empty column names
\makeatletter
\long\def\pgfplotstableread@impl@collectcolnames@NEXT#1{%
%\pgfplots@message{Got column name no \thepgfplotstableread@curcol\ as '#1'}%
    \pgfutil@ifundefined{pgfplotstableread@impl@COLNAME@#1}{%
        \if\relax\detokenize{#1}\relax
            \def\pgfplotstable@loc@TMPa{empty}%
        \else
            \def\pgfplotstable@loc@TMPa{#1}%
        \fi
    }{% generate unique column names warning:
        \if\relax\detokenize{#1}\relax
            \def\pgfplotstable@loc@TMPa{empty}%
        \else
            \def\pgfplotstable@loc@TMPa{#1}%
        \fi
        \pgfplots@warning{Table '\pgfplotstableread@filename' has non-unique column name '\pgfplotstable@loc@TMPa'. Only the first occurence can be accessed via column names.}%
        \edef\pgfplotstable@loc@TMPa{\pgfplotstable@loc@TMPa--index\thepgfplotstableread@curcol}%
    }%
    \expandafter\def\csname pgfplotstableread@impl@COLNAME@#1\endcsname{foo}% remember this name.
    \expandafter\pgfplotslistpushbackglobal\expandafter{\pgfplotstable@loc@TMPa}\to\pgfplotstable@colnames@glob
    \ifpgfplots@tableread@to@listener
        % create an associative container colindex -> colname
        % for use in a listener.
        \expandafter\edef\csname pgfplotstblread@colindex@for@name#1\endcsname{\thepgfplotstableread@curcol}%
    \fi
    \pgfplotstableread@countadvance\pgfplotstableread@curcol
}
\makeatother

\pgfplotstableread[col sep=comma]{test.csv}\tablea

%Start the document
\begin{document}
% Table a

\pgfplotstabletypeset[
    columns/empty/.style={column name={}, string type},
    columns/pe/.style={column name=PE, fixed, fixed zerofill, precision=3,  dec sep align},
    columns/se/.style={column name=SE, fixed, fixed zerofill, precision=3,dec sep align},
    every head row/.style={before row={\toprule},after row=\midrule},
    every last row/.style={after row={\toprule}},
]\tablea

\end{document}
share|improve this answer
    
Thanks so much for the detailed response -- using this example, I get an error stating:! Package PGF Math Error: Could not parse input '"Intercept"' as a floating point number, sorry. The unreadable part was near '"Intercept"'.. Could you help ID the error? Thanks, –  mike Nov 18 '12 at 0:22
1  
I think you didn't use quote=F in the write.table command of your R script, as there are still quotes around the word Intercept. Could you try if that solves the problem? –  Jake Nov 21 '12 at 18:49
    
@Jake to the rescue again. That certainly helped me. This problem has been vexing me most of the day. –  ThomasH Nov 24 '12 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.