From RStudio, is it possible to knit only part of an R Markdown document?

You can achieve what you want in many ways:

  • use the R notebooks, instead than the R Markdown document, as suggested above by @mara:
  • use @DavoWW knitr::knit_exit() trick: knit_exit() doesn't simply abort knitting. It just stops it at the point where you placed it. So you get an HTML file as an output, but of course without the parts you didn't knit (which is probably not good for your use case).

  • concerning caching options, invert your point of view :grinning: instead than going chunk by chunk, just set the default (e.g., cache all chunks) with opts_chunk$set(...) at the beginning of your doc, and then deactivate caching only in the chunk you're currently editing, for example

  • move the analysis-heavy part of the code outside the R Markdown document completely to an R script (it also makes debugging easier). The script must save the results of the analysis to some file, e.g., .csv, .rds, .rda or a feather file. Then in the R Markdown you can add a chunk with an if statement, which checks if the file exists (in which case it loads it) otherwise it sources the analysis script. This way, knitting takes way less time.

  • use drake! It requires changing your mindset quite a bit, so it definitely isn't an easy step. The reward is that it accelerates quite a lot the process of developing and reproducing time-consuming analyses.

6 Likes

Thanks for the pointers! I've been using R notebooks. I like the notebook environment for exploratory analysis, although my focus is not really on knitting/generating html reports.

move the analysis-heavy part of the code outside the R Markdown document completely to an R script (it also makes debugging easier).

Indeed, I recently found many of my Rmd and notebooks files should probably be just R scripts, because they take time to run and I never look at their html or nb.html files. The reason I started them as Rmd/notebook files is that I like the separation of code chunks and the inline output printing. They're more like an enhanced console with inline output and code storage...

My workflow is mostly: exploratory analysis -> find the best approach to analyze data -> write some functions to reproducibly produce results -> document the function calls. The notebook environment is great for exploring different approaches and keeping track of what I tried in the past few hours. But after I find a solution, I usually output the results as csv, pdf, etc. A perfectly knitted HTML file is usually not part of the motivation. About one out of 5-10 notebooks are knitted, and the rest are just Rmd files storing the code I tried. Maybe this reflects that I need to learn better habits of data analysis (recently I start thinking about how to optimize my workflow).

drake looks great! I always wanted my workflow to be more like a pipeline, where changing analysis parameters or inputs automatically starts new analysis. Right now it seems to be beyond my scope a little bit, but I hope to use it in the future.

1 Like

Add the code
eval=FALSE in the chunk option, will skip this code in knitting

like:
{r eval=FALSE }
cat("GOOD LUCK")

1 Like