3

If you want to export the output of an R script to latex and create a pdf document you can use the package Knitr with RStudio. But I guess this is only a good practise for short results.

What if I want to create a whole book, a 1000 pages thesis?

How do yo do the opposite?

I mean, Can I include R code within my latex files and run it directly from say TexStudio+LaTex/LuaTex? (In order to get the numerical results and graphics in the pdf).

How?

NEW: Some people said one can use knitr to generate big docuemnts. Even if it's possible I think it's more comfortable and versatile to create LaTex documents from specific platform such as TexStudio.

So, what is the best practice?. Mixing both methods?: Generating parts of the document with Knitr and later mixing and modifying them with TexStudio?. How?

There are packages like lstlisting designed to show code properly but not to run it.

4

In ConTeXt, you can use the filter module for such tasks. In fact, the filter module is a generalization of ConTeXt MkII module m-r, which was designed for specifically what you are asking.

However, with the filter module it is easy to replicate the functionality for any language, so there is no MkIV version of m-r.

Here is a minimal example:

\usemodule[filter]

\defineexternalfilter
  [R]
  [
     filtercommand={R CMD BATCH -q --save --restore \externalfilterinputfile\space \externalfilteroutputfile},
     output=\externalfilterbasefile.out,
     readcommand=\typefile,
     read=no,
     cache=yes,
   ]

\starttext
\startR
  ushape <- c(rexp(500000), 12-rexp(500000))
  pdf("ushape.pdf")
  par(mfrow=c(1,2))
  hist(ushape)
  plot(density(ushape), main="Density")
  dev.off()
\stopR

\startplacefigure[location=here, title={Output from R}]
  \externalfigure[ushape.pdf][height=0.5\textheight]
\stopplacefigure

\stoptext

which gives

enter image description here

Note that the files are cahced, so subsequent runs are very quick.

  • Nice, though I'm not using ConText. – skan Oct 17 '15 at 9:35
4

But I guess this is only a good practice for short results.

I believe that this is a false premise. I never make books of 1000 pages, but I made several statistical reports, some near the 100 pages at two columns, with only a few text paragraphs in LaTeX but dozens and dozens of tables (xtables), dozens of various types of plots and dozens of statistical results, all generated by R chunks in a single file, with the same problems that in shorter documents.

With this experience, for me the rule is just the opposiste: the larger size of the document, the higher the convenience of the Sweave/knitr chunks (using Rstudio, LyX or TeXworks, that is not the question).

If the compilation times are too high in the previews, try with cache=TRUE in knitr and consider working mainly with subsets of the final document as far as possible. However, I've never needed any of these workarounds, because the compilation times were were only enough for some eye-rest.

  • 2
    These are also the points I want to make. The number of pages is irrelevant. The knitr book (Dynamic Documents with R and knitr) was written with the knitr module in LyX, and it is about 300 pages. The major challenge was not from using knitr, but writing the content (of course). I have not heard of a thesis of 1000 pages, and I'd admire the effort in such a long thesis :) – Yihui Xie Oct 17 '15 at 3:04
  • Rstudio doesn't offer GUI tools to easily create Latex tables, equations, layouts, etc... Most specialized applications (such as TexStudio) do. That's why I think it could be better to create small chunks on knitr (automatically from R results), and then mix and modify them on any of these applications, or it would be even better to be able to run small chunks of R code from these applications – skan Jun 21 '16 at 17:54
  • @skan As said, the editor is not the question. My thoughts about the editor are true only for people with my LaTeX & R skills, my needs & preferences (that is: me), but about that I hardly use LaTeX wizards, but Rstudio features helps me sometimes (BTW: Rstudio is able to run any R chunk within or outside a .Rnw file), but really any editor with syntax highlight, that can be configured to make a .Rnw > .tex > .pdf conversion, and show the result, is for me fine. – Fran Jun 22 '16 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.