在 Lyx 中使用 Knitr 的 R 函数出错 [英] Error with R function using Knitr in Lyx

查看:23
本文介绍了在 Lyx 中使用 Knitr 的 R 函数出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 summary() 函数时出现错误Lyx 的针织衫.它前面的功能有效.

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

我正在通过在 Lyx 中插入 Tex 代码输入此代码.我刚刚测试我可以运行摘要(teengamb)但不能运行摘要(mdl).两种代码都可以在 RStudio 中使用.

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.

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

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

推荐答案

这个问题已经在中解决了knitr 1.1 之后的版本.您无需更改 LyX 或 R 中的任何内容.从 CRAN 安装 knitr:

install.packages('knitr')

请忽略以下两个答案:

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

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}
egin{document}
egin{verbatim}
'
end{verbatim}
end{document}

但是如果我们将 upquote 移动到 fontenc 之后,它会起作用:

But if we move upquote after fontenc, it works:

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

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

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

Ubuntu 用户无法重现问题的原因是因为 upquote.sty 来自 R 的 texmf 树,而不是 TeXLive 中的树,以及 R 的 upquote 有效.

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.

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

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

请忽略下面的答案:

由于 options(show.signif.stars = FALSE) 有效,我将它作为一个可能的答案发布,但这对我来说仍然是一个非常奇怪的问题.设置 show.signif.stars = FALSE 会从以下结果中删除重要代码(来自 summary(mdl)):

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 

错误来自 Signif 行.代码,我不明白为什么这些字符中的任何一个都可能导致 LaTeX 错误:它们都是 ASCII 并且应该在 verbatim 环境中工作.

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.

根据上面的评论,@mrdwab 和我都无法重现该问题.我想 OP 的 LaTeX 安装一定有什么奇怪的地方.

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.

这篇关于在 Lyx 中使用 Knitr 的 R 函数出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆