• R语言
  • windows下 LyX+knitr 编译不成功

我是完全按照老大的视频(http://www.screenr.com/SvL8)来设置的,可还是不行!以下是log ,谁帮我诊断一下!

This is XeTeX, Version 3.1415926-2.3-0.9997.5 (MiKTeX 2.9) (preloaded format=xelatex 2013.1.31) 1 FEB 2013 11:44

entering extended mode

**LyX-knitr.tex

("C:\Documents and Settings\wgh2wxy\Local Settings\Temp\lyx_tmpdir.Hp3572\lyx_t

mpbuf2\LyX-knitr.tex"

LaTeX2e <2011/06/27>

....

[s:11]Runaway argument?

{璋㈢泭杈墋

! Paragraph ended before \author was complete.

<to be read again>

\par

l.70

I suspect you've forgotten a `}', causing me to apply this

control sequence to too much text. How can we recover?

My plan is to forget the whole thing and hope for the best.

)[s:11]

[s:11]Runaway argument?

{2012骞

! File ended while scanning use of \date.

<inserted text>

\par

<*> LyX-knitr.tex

I suspect you have forgotten a `}', causing me

to read past where you wanted me to stop.

I'll try to recover; but if the error is serious,

you'd better type E' orX' now and fix your file.

! Emergency stop.

<*> LyX-knitr.tex

*** (job aborted, no legal \end found)[s:11]

Here is how much of TeX's memory you used:

15078 strings out of 429156

317527 string characters out of 3171140

310215 words of memory out of 3000000

18175 multiletter control sequences out of 15000+200000

6678 words of font info for 21 fonts, out of 3000000 for 9000

786 hyphenation exceptions out of 8191

57i,0n,35p,878b,150s stack positions out of 5000i,500n,10000p,200000b,50000s

No pages of output.

似乎问题就出在里面我做了图标的地方

我自己发现了三个问题

(1)\author{璋㈢泭杈墋 少了"}" 与log提示吻合

(2)\date{2012骞 少了"}" 也与log提示吻合

(3)是不是也少了\end{document}

该怎么解决?

多半是编码问题。

1. 更新R包,参见 https://github.com/yihui/knitr/blob/master/FAQ.md

2. 更新LyX到最新版本,然后用这个文件替代安装目录下的同名文件: https://raw.github.com/yihui/lyx/master/scripts/lyxknitr.R 你没说装在哪儿,只能连蒙带猜在C盘某个目录下了。等LyX 2.0.6出来就不存在这一步了,参见 http://www.lyx.org/trac/ticket/8504

回复 第4楼 的 谢益辉:

修复了Windows下使用Rstudio通过Rsweave编译Rnw文件导出pdf文件过程中,插入的R代码中不能使用中文的问题。同时出现一个问题,无法在生成图形的标题中使用中文。代码如下:

<br />
\documentclass[UTF8,10pt]{ctexart}<br />
\usepackage[a4paper,%%textwidth=129mm,textheight=185mm, %%193-8<br />
text={160mm,260mm},centering]{geometry}<br />
\pagestyle{empty}<br />
\begin{document}</p>
<p>\title{使用ggplot2测试中文支持情况}<br />
\abstract{<br />
abc 测试中文,测试中文的目的在于是否能够在Rstudio的knirt中正常编译含有中文的字符,不会出现乱码的情况,事实证明,需要在代码开始设置UTF-8字符编码格式才可以正常使用中文。}<br />
<<setup, include=FALSE>>=<br />
options(device = function(file, width = 2, height = 2, ...) {<br />
  cairo_pdf(tempfile(), width = width, height = height, ...)<br />
})<br />
pdf.options(encoding="GB2312")<br />
@<br />
<<cyrillic, dev='cairo_pdf'>>=<br />
library(ggplot2)<br />
ti <- "中文"<br />
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy))<br />
p + geom_point() + opts(title="测试")<br />
@<br />
\textbf{ggplot}这里主要测试中文是否能够正常支持的功能。<br />
<<fig.width=5, fig.height=3, message=FALSE, tidy=FALSE, fig.align="center">>=<br />
y <- c("PRC","USA","UK","FR")<br />
x <- c("中国","美国","英国","法国")</p>
<p>y<br />
x<br />
@</p>
<p>\end{document}</p>
<p>


问题就出在了在ggplot中使用opts的title参数设置中文,会在生成的pdf文件中显示错误,且无法在所生成的图形标题中正常显示中文,具体结果运行代码可知。</p>

OS:win

R :R i386 2.15.2

Rstudio:0.97.310

LyX:2.0.4

Knitr:1.0.5

这是个一言难尽的问题。搜Encoding: http://yihui.name/knitr/demo/graphics/

了解,那我就还是用E文吧。上面的代码我重排了一下,使用了pdf.options()函数,并设置encoding为gb2132。效果相对开始时有了一定的变化,标题乱码从..........变为了口口了,[s:19]

至于utf8,试了一下不是太好。

pdf()设备的字体和编码设置会让人崩溃的,遇到多字节的情况,建议用我上面链接里说的cairo_pdf()试试,它对多字节字符相对友好一些。

回复 第8楼 的 谢益辉:

我尝试一下,有结果再回复。[s:13]

===================

已经有结果了,基本功能已经可以正常使用。效果请在Rstudio中使用Knitr键入如下代码到Rnw文件中,并生成pdf。pdf中文、R code中文、code output中文、ggplot2 & plot绘图使用中文字符均已初步解决。

<br />
\documentclass[UTF8,10pt]{ctexart}<br />
\usepackage[a4paper,%%textwidth=129mm,textheight=185mm, %%193-8<br />
text={160mm,260mm},centering]{geometry}<br />
\pagestyle{empty}<br />
\begin{document}<br />
\title{使用ggplot2测试中文支持情况}<br />
\maketitle<br />
\abstract{<br />
abc 测试中文,测试中文的目的在于是否能够在Rstudio的knirt中正常编译含有中文的字符,不会出现乱码的情况,事实证明,需要在代码开始设置UTF-8字符编码格式才可以正常使用中文。}</p>
<p><<setup, include=FALSE>>=<br />
options(device = function(file, width = 2, height = 2, ...) {<br />
  cairo_pdf(tempfile(), width = width, height = height, ...)<br />
})<br />
pdf.options(family = "GB1")<br />
@</p>
<p>\textbf{plot}这里主要测试plot是否支持中文功能。<br />
<<fig.width=5, fig.height=3, message=FALSE, tidy=FALSE, fig.align="center">>=<br />
library(ggplot2)<br />
ti <- "中文"<br />
plot(c(1:10), main = "这是中文的标题", xlab = "X轴也是中文的",ylab="Y轴呢?")<br />
@</p>
<p>\textbf{ggplot}这里主要测试ggplot的中文是否能够正常支持。<br />
<<fig.width=5, fig.height=3, message=FALSE, tidy=TRUE, fig.align="center">>=<br />
y <- c("PRC","USA","UK","FR")<br />
x <- c("中国","美国","英国","法国")<br />
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy))<br />
p + geom_point() + opts(title="ggplot的中文标题是这样子",plot.title=theme_text(face="bold",size=10,hjust=0.5,vjust=2,colour="black")) + xlab("测试X轴") + ylab("测试Y轴")<br />
#dev.off()<br />
#dev.off()<br />
@<br />
\textbf{}下面我们测试是否能够在代码执行中正常输出中文;输出结果如下<br />
<<>>=<br />
y<br />
x<br />
@<br />
\textbf{ggplot}我这里中文功能的支持主要使用的是pdf.options(family = "GB1")参数;如果你对ggplot或plot所绘制的图形有细节调整需求,比如:调整标题字体等,可使用各自的绘图参数调整即可,就像文中ggplot代码中的opts参数所列;<br />
\end{document}<br />
</p>

每个人的系统不同,可能会出现不同的字体簇设置。我这里主要参考了一下一些说明:

http://yihui.name/knitr/demo/graphics/

https://github.com/yihui/knitr/issues/172

https://github.com/yihui/knitr/issues/436

以及

http://yihui.name/cn/2010/02/misc-issues-in-latex-lyx-r-sweave-pgfsweave/

OS:win

R version:2.15.0

R Studio:0.97.310

最后说明一下,R调用LyX编译不能显示中文的确不是Knitr的Bug。[s:17]

很好的总结,谢谢!不过又要逼着我更新旧日志文章……

8 个月 后

回复 第9楼 的 zggjtsgzczh:

你好,我复制了你的代码,执行后我这里报错

19 <<fig.width=5, fig.height=3, message=FALSE, tidy=FALSE, fig.align="center">>=

20 library(ggplot2)

21 ti <- "中文"

22 plot(c(1:10), main = "这是中文的标题", xlab = "X轴也是中文的",ylab="Y轴呢?")

23 @

Quitting from lines 19-22 (11.Rnw)

Error in grepl("\n", lines, fixed = TRUE) :

invalid multibyte string at '<83>"'

请问是什么原因,谢谢 ,是RSTUDIO配置问题还是其他原因?

回复 第12楼 的 god123:

升级R和RStudio至最新版。可解决中文问题。不过我这边倒是出现一个哭笑不得的问题,plot图ggplot图与文字和代码分离为两个文件。待我晚上看看如何解决。[s:12]

回复 第13楼 的 zggjtsgzczh:

你好,我用的是r 3.0.1 RStudio-0.97.551.exe

从lyx导出rnw后,有了\usepackage{fontspec} 然后报错

不知怎么设置还是缺少什么

Error: running 'texi2dvi' on 'ASDF.tex' failed

LaTeX errors:

!

! The fontspec package requires either XeTeX or LuaTeX to function.

! The fontspec package requires either XeTeX or LuaTeX to function.

!

!

! You must change your typesetting engine to, e.g., "xelatex" or "lualatex"

! You must change your typesetting engine to, e.g., "xelatex" or "lualatex"

! instead of plain "latex" or "pdflatex".

! instead of plain "latex" or "pdflatex".

!

!

! See the fontspec documentation for further information.

! See the fontspec documentation for further information.

!

!

! For immediate help type H <return>

! For immediate help type H <return>

|'''''''''''''''''''''''''''''''''''''''''''''''

| This is a fatal error: LaTeX will abort

|...............................................

In addition: Warning message:

running command '"D:\PROGRA~1\CTEX\MiKTeX\miktex\bin\texi2dvi.exe" --quiet --pdf "ASDF.tex" -I "D:/setup/R/share/texmf/tex/latex" -I "D:/setup/R/share/texmf/bibtex/bst"' had status 1

回复 第13楼 的 zggjtsgzczh:

能不能留个联系方式,我的邮箱是freelife213@126.com

回复 第14楼 的 god123:

当RStudio的options设置中的Sweave卡片的"weave Rnw files using"选项为Sweave,"Typeset LaTex into PDF using"选项为pdfLaTex,以下代码有效:

我在R多个版本上测试均可以正常显示中文,以下为我在yihui站点参考的一段代码进行了相应编辑,可以正常显示中文、ggplot和plot图表。

<br />
\documentclass[UTF8,10pt]{ctexart}<br />
\usepackage[a4paper,%%textwidth=129mm,textheight=185mm, %%193-8<br />
text={160mm,260mm},centering]{geometry}<br />
\pagestyle{empty}<br />
\begin{document}<br />
\SweaveOpts{concordance=TRUE}<br />
\title{使用ggplot2测试中文支持情况}<br />
\maketitle<br />
\abstract{<br />
abc 测试中文,测试中文的目的在于是否能够在Rstudio的knirt中正常编译含有中文的字符,不会出现乱码的情况,事实证明,需要在代码开始设置UTF-8字符编码格式才可以正常使用中文。}</p>
<p><<setup, include=FALSE>>=<br />
options(device = function(file, width = 2, height = 2, ...) {<br />
  cairo_pdf(tempfile(), width = width, height = height, ...)<br />
})<br />
pdf.options(family = "GB1")<br />
@</p>
<p>\textbf{plot}这里主要测试plot是否支持中文功能。<br />
\begin{figure}<br />
\begin{center}<br />
<<rnorm,fig=TRUE>>=<br />
library(ggplot2)<br />
ti <- "中文"<br />
plot(c(1:10), main = "这是中文的标题", xlab = "X轴也是中文的",ylab="Y轴呢?")<br />
@<br />
\end{center}<br />
\end{figure}</p>
<p>\textbf{ggplot}这里主要测试ggplot的中文是否能够正常支持。<br />
\begin{figure}<br />
\begin{center}</p>
<p><<rnorm,fig=TRUE>>=<br />
y <- c("PRC","USA","UK","FR")<br />
x <- c("中国","美国","英国","法国")<br />
p <- ggplot(data=mpg,mapping=aes(x=cty,y=hwy))<br />
p + geom_point() + opts(title="ggplot的中文标题是这样子",plot.title=theme_text(face="bold",size=10,hjust=0.5,vjust=2,colour="black")) + xlab("测试X轴") + ylab("测试Y轴")<br />
#dev.off()<br />
#dev.off()<br />
@</p>
<p>\textbf{}下面我们测试是否能够在代码执行中正常输出中文;输出结果如下<br />
<<>>=<br />
y<br />
x<br />
@</p>
<p>\textbf{ggplot}我这里中文功能的支持主要使用的是pdf.options(family = "GB1")参数;如果你对ggplot或plot所绘制的图形有细节调整需求,比如:调整标题字体等,可使用各自的绘图参数调整即可,就像文中ggplot代码中的opts参数所列;</p>
<p>\end{center}<br />
\end{figure}</p>
<p>\end{document}</p>
<p>
</p>

引用代码出处:http://yihui.name/cn/2010/02/misc-issues-in-latex-lyx-r-sweave-pgfsweave/ (注:源代码中如果直接复制可能需要对所复制的代码稍加修正,就是要手动删除每行开始的那个空格,也就是要每行顶格写,否则编译错误)

我的平台:

OS:winxp

R:3.0.0

RStudio:0.97.551

CTEX:CTeX_2.9.2.164_Full

ps:不过,还是没有解决为什么ggplot所绘制的图表会单独作为一个test-rnorm.pdf存在,而不是显示到与test.Rnw同名的test.pdf文件中。————此问题已解决,加一个rnorm,fig=TRUE以及用{center}{figure}两个begin/end参数把绘图代码括起来就可以了。——最近升级过一次RStudio,然后没改Sweave选项。就发生本不该发生的悲剧了——不是说RStudio现在默认knitr了么,这个圈绕的。[s:12]

如果"weave Rnw files using"选项为knitr,"Typeset LaTex into PDF using"选项为pdfLaTex。请使用9#代码,依旧有效。

回复 第16楼 的 zggjtsgzczh:

<br />
<<setup, cache=FALSE, include=FALSE>>=<br />
opts_knit$set(upload.fun = imgur_upload, self.contained = FALSE, root.dir = '~/R/project')<br />
@<br />


如上行代码所示的upload.fun、 self.contained、root.dir 这些选项在opts_knit$get()里是找不到,那怎么知道对应的选项有哪些(不就是opts_knit$get()吗)?upload.fun应该是从外部添加的吧?如何添加,是不是和下面提到的base包的options有关。这到底是个什么机制呢?</p>

<br />
<<setup, include=FALSE, cache=FALSE>>=<br />
library(knitr)<br />
options(replace.assign=TRUE,width=50) # which package does options blong to<br />
opts_chunk$set(fig.path='figure/graphics-', cache.path='cache/graphics-', fig.align='center', dev='tikz', fig.width=5, fig.height=5, fig.show='hold', cache=TRUE, par=TRUE)<br />
knit_hooks$set(par=function(before, options, envir){<br />
if (before && options$fig.show!='none') par(mar=c(4,4,.1,.1),cex.lab=.95,cex.axis=.9,mgp=c(2,.7,0),tcl=-.3)<br />
}, crop=hook_pdfcrop)<br />
@<br />
 


如上段代码里,options()是base里的一个函数,主要是设置全局变量的。其中,replace.assign这个选项在names(options())里也是看不到的。</p>

ps:\SweaveOpts{concordance=TRUE},这句话这里还能用吗

回复 第17楼 的 daigazi:

blackbox,一般开发者会设计一个功能函数框架与参数框架,然后开发每个函数使其能够得以使用。但是随着开发的进程发展,一些函数会被隐蔽,另一些函数被开启。至于原因多种多样,比如:更好地新函数替代旧函数,处在内部测试阶段的函数及参数等等。大多数情况下都是善意的,也有一些为了理性的商用目的而屏蔽了,比如一些统计软件的免费版商业版或单机版服务器版。这样就比较容易理解CRAN的审核政策了。黑魔法? :)

有兴趣看源代或从通用函数的使用习惯进行猜测。具体本例,还是由拥有最高解释权的开发者解答,@TA.:)

ps:至于那个关于sweave的问题,请搜索本帖"悲剧"关键词[s:19]

回复 第18楼 的 zggjtsgzczh:这样子,那就不折腾了。

回复 第17楼 的 daigazi:好问题。如果你不明白这些选项是干什么的,我的建议是全部去掉。

replace.assign是formatR中的选项:https://github.com/yihui/formatR/wiki

opts_knit中的选项参见文档:http://yihui.name/knitr/options

这些事情确实有点乱,作为一个年少无知的开发者,总是有后悔的时候:https://github.com/yihui/knitr/issues/597 两年后再去想两年前的事情,肯定有很多可以改进的地方。几年前想的只是“勇敢的少年,快去创造奇迹”,有些事情之所以去做,完全只是因为觉得有办法做到,没有全局考虑。