Using knitr to produce multiple output documents

+3 votes
130 views
asked Aug 17, 2015 in Open Science by Thomas (915 points)

I like to use knitr for literate programming of a research article because I can embed R code that produces results directly into the document. It becomes frustrating, however, when I also need to produce another output format (e.g., a presentation of those results for a conference). What is the best workflow for using knitr to produce multiple output documents without having to duplicate all of the code in each source document?



This post has been migrated from the Open Science private beta at StackExchange (A51.SE)
commented Aug 18, 2015 by Alexander Konovalov (135 points)
Computational Science has a tag for R with 28 questions at the moment - maybe try there? Also, I will not ping you with suggestion to migrate your other questions, but please have a look at them! Sorry for too many pings:)

This post has been migrated from the Open Science private beta at StackExchange (A51.SE)

2 Answers

+3 votes
answered Aug 17, 2015 by Stephen Eglen (100 points)

It may seem like it defeats the purpose of having the code and the latex in the same report, but I like the approach of putting your code in a separate R file, and then using this feature http://yihui.name/knitr/demo/externalization/

so all your code is an R script, and with named chunks you include the bits you want in your report. See example 113.

https://github.com/yihui/knitr-examples/blob/master/113-foo.R



This post has been migrated from the Open Science private beta at StackExchange (A51.SE)
commented Aug 18, 2015 by Stephen Eglen (100 points)
You could also think about putting the code in an R package, and having multiple vignettes in that package - one ofr the paper, one for a talk...

This post has been migrated from the Open Science private beta at StackExchange (A51.SE)
0 votes
answered Aug 20, 2015 by Gavin Simpson (720 points)

If you can live with writing in markdown, the rmarkdown package provides a nice interface to rendering documents via pandoc. I have been able to render to beamer slides or a PDF (LaTeX) just by using this package. You are restricted to some types of documents --- I don't think you can use some of the HTML slide deck frameworks and render to PDF as the markup is different in terms of delineating slides boundaries, but beamer slides and documents use the same standard markdown headers to delineate sections/slides (beamer slides need you to set a configuration option to choose which level of header starts a slide; I use level 2 so that level 1 introduces a section slide).

There's probably a limit to how far you can go with this however, without totally compromising either of the rendered documents.



This post has been migrated from the Open Science private beta at StackExchange (A51.SE)

Welcome to Open Science Q&A, where you can ask questions and receive answers from other members of the community.

If you participated in the Open Science beta at StackExchange, please reclaim your user account now – it's already here!

e-mail the webmaster

...