13

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.

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Have you tried to set the `envir` parameter in `knit2pdf` explicitly? – krlmlr Mar 26 '13 at 21:13
  • Yeah, I just get an unused argument error. – Gregor Thomas Mar 26 '13 at 21:19
  • Have you tried it without referencing a global variable? I get exactly the same error when I try to use `knit2pdf()`, when the one line of R code is `print("Hello World")` (i.e. when there's no explicit reference to a global variable)... I'm on a Windows XP box, using R-devel, and `knitr_1.1`, with a MikTeX distro under the hood. – Josh O'Brien Mar 26 '13 at 21:33
  • @JoshO'Brien, thanks for pointing that out! It all seems to be a problem with the `output` argument to `knit2pdf`, when that is gone everything else works as expected. – Gregor Thomas Mar 26 '13 at 21:51

1 Answers1

8

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks for reporting the issue! The real story is, `knitr` 1.1 does not have the `envir` argument; the development version of `knitr` added it, but later removed the `output` argument. Now the latest development version has both arguments, and `knit2pdf()` should work fine now. – Yihui Xie Mar 27 '13 at 00:30
  • I am using Rstudio with R markdown. Does this apply to markdown documents also? I am using source() to run a script inside a cached chunk. I want to use the objects computed in that script in later chunks, but those objects don't seem to be available to later chunks. Can you clarify what I need to do? – statistician_in_training Jun 19 '13 at 02:45
  • @statistician_in_training Objects defined in one chunk should definitely be available in later chunks, no matter how you're compiling. If you're having problems with that, create a minimal example and post it as a question. – Gregor Thomas Jun 19 '13 at 21:02
  • I have created a new [question](http://stackoverflow.com/questions/17222380/) with a minimal example, about using cached chunks with sourced scripts and accessing R objects in later chunks. – statistician_in_training Jun 20 '13 at 19:41
  • @Yihui. I tried the above code with R_3.0.1, knitr_1.5, Emacs/ESS on Windows7 but I still get message "... had status 1". When I remove output part the pdf was produced. – giordano Jan 12 '14 at 19:03
  • Thanks to posting [multiple-pdf-reports](http://stackoverflow.com/questions/15396755/using-loops-with-knitr-to-produce-multiple-pdf-reports-need-a-little-help-to?rq=1) I could solve my problem. If I change `output = 'globaltest.pdf'` to `output = 'globaltest.tex'` the pdf-file will be produced. – giordano Jan 13 '14 at 05:41
  • @giordano In your case (perhaps also in most cases), you do not need to specify the `output` argument at all. – Yihui Xie Jan 14 '14 at 03:59