knit2pdf可以使用全球环境吗? [英] Can knit2pdf use the global environment?

查看:55
本文介绍了knit2pdf可以使用全球环境吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案中,@ Yihui表示knitr使用全球环境.这让我感到困惑-我的经验是事实并非如此.不过,我从来没有真正使用knit,我通常直接使用PDF.

在一个小实验中,似乎knit确实使用了全局环境(或您使用envir参数指定的任何环境),但是knit2pdf并未使用.

最小示例:global_test.Rnw文件

\documentclass{article}
\begin{document}
<<>>=
    print(x)
@
\end{document}

R脚本:

x <- "Hello World"
knit(input="global_test.Rnw")
# Works as expected, could now call tools::texi2pdf to generate pdf.

knit2pdf(input="global_test.Rnw")
# Doesn't

后者会生成不会显示的PDF文件并给出警告:

running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf
"global.pdf"  -I "C:/PROGRA~1/R/R-215~1.3/share/texmf/tex/latex" -I
"C:/PROGRA~1/R/R-215~1.3/share/texmf/bibtex/bst"' had status 1 

我尝试将环境传递给knit2pdf(envir = globalenv()),希望将其传递给...,我只是得到了一个未使用的参数错误.

通常,我知道引用全局环境的形式很差,但是有没有办法用knit2pdf做到这一点,或者显式地传递环境,或者我更好地使用brewsprintf作为在 @Ramnath的答案中针对上述同一问题?

在我的用例中,我不认为tools::texi2pdf有用,因为我需要使用knit2pdf可以轻松处理的XeLaTeX进行编译.

解决方案

问题中的示例问题似乎与环境无关.如果将output参数保留为knit2pdf,则所有内容将正确编译且没有警告.

作为参考,我在Windows 7上的R 2.15.3上使用了knitr 1.1.我将让Yihui知道,因为它似乎是knit2pdf中的错误(它调用了tools::texi2pdf,该错误不接受输出文件路径).

更新:该问题已在knitr的开发版本中得到解决,可用这里.

还值得注意的是, RStudio 中的Compile PDF按钮不使用您当前的环境,因此如果您想访问全局变量并且您正在使用RStudio,显式调用适当的knit函数,而不是依赖快捷方式.实际上,它不直接使用knit2pdf,而是调用rmarkdown::render.

In this answer, @Yihui said that knitr uses the global environment. This confused me--my experience had been that it doesn't. I never really use knit though, I usually go straight to PDF.

In a little experiment, it seems that knit does use the global environment (or whatever environment you specify using the envir argument), but that knit2pdf doesn't.

Minimal example: global_test.Rnw file

\documentclass{article}
\begin{document}
<<>>=
    print(x)
@
\end{document}

R Script:

x <- "Hello World"
knit(input="global_test.Rnw")
# Works as expected, could now call tools::texi2pdf to generate pdf.

knit2pdf(input="global_test.Rnw")
# Doesn't

The latter generates PDF file that won't display and gives a warning:

running command '"C:\PROGRA~2\MIKTEX~1.9\miktex\bin\texi2dvi.exe" --quiet --pdf
"global.pdf"  -I "C:/PROGRA~1/R/R-215~1.3/share/texmf/tex/latex" -I
"C:/PROGRA~1/R/R-215~1.3/share/texmf/bibtex/bst"' had status 1 

I tried passing an environment to knit2pdf (envir = globalenv()) hoping it would be ... passed on, I just get an unused argument error.

Generally, I know that referencing the global environment is poor form, but is there a way to do it with knit2pdf, or to pass an environment explicitly, or am I better off using brew and sprintf as in @Ramnath's answer to the same question above?

In my use case, I don't think tools::texi2pdf is useful because I need to compile with XeLaTeX, which knit2pdf handles effortlessly.

解决方案

The problems with the examples in the question seem to have nothing to do with environments. Everything will compile correctly and without warnings if the output argument is left off of knit2pdf.

For reference, I was using knitr 1.1 on R 2.15.3 on Windows 7. I'll let Yihui know as it appears to be a bug in knit2pdf (which calls tools::texi2pdf, which doesn't accept an output file path).

UPDATE: The problem has been fixed in the development version of knitr, available here.

It's also worth noting that the Compile PDF button in RStudio does not use your current environment, so if you want to have access to global variables and you are using RStudio, make an explicit call to the appropriate knit function rather than relying on the shortcut. In fact, it doesn't use knit2pdf directly, rather it calls rmarkdown::render.

这篇关于knit2pdf可以使用全球环境吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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