Just wondering, how do people incorporate knitr into their current workflow? I have a long, pre-existing .R file that reads in data, does the analysis, and at the end of the file I produce, say, a ggplot chart and a table summarizing my findings. I'd like to incorporate these into a document, either markdown or LaTeX. Should I rename my initial .R file to be a .Rmd file and put the knitr commands near the end? Should I create a new .Rmd file that creates the report, and somehow incorporate the analysis from the .R file? In other words, what are best practices for adding knitr output to my existing workflow?
You should use external sourcing to keep your scripts as your repository of code and your knitr document to call on it. You would benefit from this approach.
For example, mark your existing code in your long .R script like this:
## @knitr Q5
all your data prep code
plotQ5 <- ggplot(your plot code
plotQ5
Then in your knitr document, with the suffix .Rnw, have something like this code chunk
<<Q05, results='hide', echo = FALSE, include=FALSE>>=
@
<<Q05plot, fig.width=width, fig.height=height, fig.align="center", warning = FALSE, echo=FALSE>>=
plotQ05
@
Now, when you compile PDF for Latex knitr will source your code and plot from the script and output the PDF file with the plot in it.
Additionally, learn to cache large objects in knitr so that it does not need to read them in again if they have not changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With