5
votes

I am trying to use knitr and stargazer to export a regression table from R to Latex. Unfortunately I get the following error message "Missing $ inserted" I've inserted a begin-math/end-math symbol since I think you left one out. Proceed, with fingers crossed.
Do I miss anything in my code or might this be an error from the stargazer package?
Any hints on how one can use knitr and stargazer together?

\documentclass{article}   
\begin{document}   
%Load dataset and run regression    

 << lm, echo=FALSE >>=  
load('dataset.RData')  
library(stargazer)  
lm1 <- lm(y~ x ,data=dataset)  
@


<< table_texstyle, echo=FALSE, comment=NA, results='asis' >>=  
stargazer(lm1)  
@  
\end{document}
2
might want to try xtable if you can't get that working.scottyaz

2 Answers

8
votes

stargazer v.2.0 is now out on CRAN, and should be able to deal with variable names that include dollar signs and underscore without any trouble. Try installing the new version, and running your code again - you should not have any problems.

5
votes

It works well for me under Ubuntu 12.04, TeXLive 2012 and R 2.15.2.

\documentclass{article}   
\begin{document}   
<<lm, echo=FALSE >>=  
df <- data.frame(x=1:10, y=rnorm(10))
library(stargazer)  
lm1 <- lm(y ~ x ,data=df)  
@

<<table_texstyle, echo=FALSE, results='asis' >>=  
stargazer(lm1)  
@  
\end{document}

You did not tell us what is dataset (we have no idea of your dataset.RData), so we cannot really know what could be the reason of the failure. At least you need to paste the tex output like

% Table created by StarGazer v.1.0 by Marek Hlavac, Harvard University. E-mail: mhlavac at alumni.princeton.edu
% Date and time: Wed, Nov 14, 2012 - 06:20:12 PM
\begin{table}[htb] \centering 

....

\end{table}

so we know if the problem came from LaTeX.