[R] How to nicely display R code with the LaTeX package 'listings'?

Yihui Xie xie at yihui.name
Thu Nov 1 22:31:04 CET 2012


Hi Marius,

I'm not sure if this helps:

1. knitr::spin() can convert a roxygen script to a report, although it
has no support to any roxygen @tags, e.g.
https://github.com/yihui/knitr/blob/master/inst/examples/knitr-spin.R
In your case, you can spin('code.R', format = 'Rnw')

2. knitr does have concordance support (opts_knit$set(concordance =
TRUE)) like Sweave, although not as accurate as it; RStudio makes use
of the concordance information generated by knitr to support
synchronization; I have not tried patchDVI yet, but I guess it should
work the same for knitr since knitr writes the same concordance file
as Sweave.

3. I do not think it matters if your collaborators do not use Sweave
or knitr, because you can always \input{} your work into their LaTeX
documents. I mean you can separate the R part from the rest of of the
LaTeX document and compile your child document separately; that is
where knitr may be helpful.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Thu, Nov 1, 2012 at 2:57 PM, Marius Hofert
<marius.hofert at math.ethz.ch> wrote:
> Dear Yihui, Dear Frank,
>
> Thanks for helping.
>
> I am aware of Sweave and knitr and this is an incredible development. Still,
> this "machinery" is too heavy for my needs. I typically use pure R scripts for
> my work (well documented with Roxygen). I can then easily debug and
> are not disturbed by additional text already. Also, I can easily run a pure R
> script on a computer cluster (not sure about an .Rnw -- okay, one might use a
> makefile that first tangles it and then sends the script to the cluster, but that
> already adds another layer of complexity). Another thing is syncronization
> (forward/backward search from .pdf). We recently managed to use Duncan's "patchDVI" to
> get backward sync to the .Rnw file instead of the .tex file. This was even in a
> multi-file document (several .Rnw loaded from a master file) where it is
> extremely useful. But that was not so easy to set up (and I am not sure if knitr
> does/can incorporate this). The biggest problem,
> however, is portability. None of my co-authors (except for Martin) uses such
> tools and is therefore able to compile the .Rnw document (they mostly use .tex
> and can compile those documents). So for putting in some lines of code in a
> paper, this is quite restrictive (that's why I was hoping for a latex-related
> solution -- still asking on r-help since I'd expect R users to have encountered
> this issue before).
>
> Cheers,
>
> Marius
>




More information about the R-help mailing list