5

I am getting an error when using the summary() function using Knitr in Lyx. The functions preceding it works.

<<>>=
library(faraway)
head(teengamb)
mdl <- lm(gamble ~ sex + status, data=teengamb) 
summary(mdl)
@

I am inputing this code through Insert Tex Code in Lyx. I just tested I can run summary(teengamb) but not summary(mdl). Both codes work in RStudio.

The error is "Undefined Control Sequence" with description "\end{verbatim} ..."

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
Saber CN
  • 765
  • 1
  • 6
  • 7
  • 2
    Can you be more specific about the error you get? Do you get the error if you put this in a Lyx document by itself? – A5C1D2H2I1M1N2O1R2T1 Sep 16 '12 at 16:19
  • 1
    I wasn't being specific, I'd updated my post to include more details. – Saber CN Sep 16 '12 at 16:23
  • 2
    Sorry, I'm not able to replicate your problem; I'm running Lyx 2.0.4 on Ubuntu. Have you tried marking the code as a Sweave chunk (using the dropdown styles) instead of using `Insert > Tex Code`? What version of Lyx etc and what OS are you using? – A5C1D2H2I1M1N2O1R2T1 Sep 16 '12 at 17:10
  • I am using Lyx 2.0.4 on Mac. I just tried Sweave instead and it worked, guess I am only having problem with knitr. – Saber CN Sep 16 '12 at 17:23
  • To follow up on @mrdwab's comment, his suggestion also worked for me, but I had to make sure that Rnw(knitr) was moved to the 'Selected' column under Document > Settings > Modules. –  Sep 16 '12 at 22:16
  • @SaberCN I cannot replicate the problem either with LyX 2.0.4/Ubuntu. Can you post the full error log? (e.g. on http://pastebin.com/) BTW, what is your version of knitr? `packageVersion('knitr')` – Yihui Xie Sep 17 '12 at 20:02
  • @Yihui I didn't see your comment until now. I posted the log to http://pastebin.com/t2ZL1CnA , the error starts at line 234. – Saber CN Oct 11 '12 at 07:03
  • @SaberCN Someone asked me a similar question after your post, and finally it turned out that the reason was his LaTeX does not support `'` (apostrophe) in the `verbatim` environment, which is really weird and I have no idea how that could be possible. To verify your problem, see if you can compile this minimal document: http://pastebin.com/72n9RCEm – Yihui Xie Oct 11 '12 at 18:05
  • @Yihui I saved your code to a tex file, imported it to Lyx, loaded knitr and compiled with no problem. This is the only instance I get error with knitr. For example, I don't get any error with this code, <<>>= library(faraway) mdl<-lm(total~ratio+expend, data=sat) summary(mdl) @ – Saber CN Oct 12 '12 at 03:24
  • @SaberCN How about setting `options(show.signif.stars = FALSE)`? – Yihui Xie Oct 12 '12 at 03:45
  • @Yihui Yes! It worked after I added this code at the beginning. Can you explain the issue behind this? – Saber CN Oct 12 '12 at 05:20
  • @SaberCN it is too weird; I cannot reproduce it, nor can I explain it... – Yihui Xie Oct 12 '12 at 05:29
  • @SaberCN I have found out the real reason. See my edit below. – Yihui Xie Feb 15 '13 at 05:22

1 Answers1

6

This problem has been solved in knitr after version 1.1. You do not need to change anything in LyX or R. Install knitr from CRAN:

install.packages('knitr')

Please ignore both answers below:


I have finally found out the reason for this error (this is the deepest bug I have ever seen). It is because the upquote package does not work if the T1 encoding is declared after it is loaded, e.g.

\documentclass{article}
\usepackage{upquote}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\begin{document}
\begin{verbatim}
'
\end{verbatim}
\end{document}

But if we move upquote after fontenc, it works:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{upquote}
\begin{document}
\begin{verbatim}
'
\end{verbatim}
\end{document}

Or just do not use the T1 encoding -- uncheck the checkbox before the font encoding in the preferences:

The reason that Ubuntu users were not able to reproduce the problem was because upquote.sty was from R's texmf tree instead of the one in TeXLive, and R's version of upquote works.

The other way to fix the problem is to add R's texmf tree to MikTeX under Windows.

Please ignore the answer below:


Since options(show.signif.stars = FALSE) worked, I'm posting it as one possible answer, but this is still a very weird problem to me. Setting show.signif.stars = FALSE removes the significance codes from the results below (which was from summary(mdl)):

Call:
lm(formula = gamble ~ sex + status, data = teengamb)

Residuals:
    Min      1Q  Median      3Q     Max 
-35.873 -15.755  -3.007  10.924 111.586 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  60.2233    15.1347   3.979 0.000255 ***
sex         -35.7094     9.4899  -3.763 0.000493 ***
status       -0.5855     0.2727  -2.147 0.037321 *  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

Residual standard error: 27.99 on 44 degrees of freedom
Multiple R-squared: 0.2454, Adjusted R-squared: 0.2111 
F-statistic: 7.154 on 2 and 44 DF,  p-value: 0.002042 

The error came from the line Signif. codes, and I do not understand why any of these characters could possibly cause errors in LaTeX: all of them are ASCII and should work inside the verbatim environment.

From the comments above, neither @mrdwab nor me could reproduce the problem. I guess there must be something weird about the OP's LaTeX installation.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419