在Lyx中使用Knitr的R函数出错

时间:2021-01-16 06:13:21

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

我在使用Lyx中的Knitr时使用summary()函数时出错。它之前的功能有效。

<<>>=
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.

我通过Lyx中的Insert Tex Code输入此代码。我刚刚测试过我可以运行摘要(teengamb)而不是摘要(mdl)。这两个代码都在RStudio中工作。

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

错误是“未定义的控制序列”,描述为“\ end {verbatim} ...”

1 个解决方案

#1


6  

This problem has been solved in knitr after version 1.1. You do not need to change anything in LyX or R. For now, you can install the development version from:

这个问题已经在1.1版之后的knitr中解决了。您无需在LyX或R中更改任何内容。现在,您可以从以下位置安装开发版本:

install.packages('knitr', repos = 'http://www.rforge.net/')

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.

我终于找到了这个错误的原因(这是我见过的最深的错误)。这是因为如果在加载之后声明T1编码,则upquote包不起作用,例如,

\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:

但是如果我们在fontenc之后向上移动,它可以工作:

\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:

或者只是不使用T1编码 - 取消选中首选项中字体编码前的复选框:

在Lyx中使用Knitr的R函数出错

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.

Ubuntu用户无法重现问题的原因是因为upquote.sty来自R的texmf树而不是TeXLive中的那个,并且R的upquote版本可以工作。

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

解决问题的另一种方法是在Windows下将R的texmf树添加到MikTeX。

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)):

由于选项(show.signif.stars = FALSE)有效,我将其作为一个可能的答案发布,但这对我来说仍然是一个非常奇怪的问题。设置show.signif.stars = FALSE从下面的结果中删除重要性代码(来自摘要(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.

该错误来自Signif系列。代码,我不明白为什么这些字符中的任何一个都可能导致LaTeX中的错误:所有这些都是ASCII并且应该在逐字环境中工作。

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.

从上面的评论来看,@ mrdwab和我都不能重现这个问题。我想OP的LaTeX安装一定有些奇怪。

#1


6  

This problem has been solved in knitr after version 1.1. You do not need to change anything in LyX or R. For now, you can install the development version from:

这个问题已经在1.1版之后的knitr中解决了。您无需在LyX或R中更改任何内容。现在,您可以从以下位置安装开发版本:

install.packages('knitr', repos = 'http://www.rforge.net/')

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.

我终于找到了这个错误的原因(这是我见过的最深的错误)。这是因为如果在加载之后声明T1编码,则upquote包不起作用,例如,

\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:

但是如果我们在fontenc之后向上移动,它可以工作:

\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:

或者只是不使用T1编码 - 取消选中首选项中字体编码前的复选框:

在Lyx中使用Knitr的R函数出错

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.

Ubuntu用户无法重现问题的原因是因为upquote.sty来自R的texmf树而不是TeXLive中的那个,并且R的upquote版本可以工作。

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

解决问题的另一种方法是在Windows下将R的texmf树添加到MikTeX。

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)):

由于选项(show.signif.stars = FALSE)有效,我将其作为一个可能的答案发布,但这对我来说仍然是一个非常奇怪的问题。设置show.signif.stars = FALSE从下面的结果中删除重要性代码(来自摘要(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.

该错误来自Signif系列。代码,我不明白为什么这些字符中的任何一个都可能导致LaTeX中的错误:所有这些都是ASCII并且应该在逐字环境中工作。

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.

从上面的评论来看,@ mrdwab和我都不能重现这个问题。我想OP的LaTeX安装一定有些奇怪。