3 like 0 dislike
926 views
in Open Science by (1.2k 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)
by (155 points)
0 0
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 like 0 dislike
by (130 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)
by (130 points)
0 0
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 like 0 dislike
by (820 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)

Ask Open Science used to be called Open Science Q&A but we changed the name when we registered the domain ask-open-science.org. Everything else stays the same: We are still hosted by Bielefeld University.

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

E-mail the webmaster

Legal notice

Privacy statement

Categories

...