The workflow that works for me (as usual - your mileage may vary) is the following:
- start the rmd document with an
init chunk, which runs very silently
{r init, echo = F, eval = T, message = F, warning = F}
This chunk loads all the data - be it from csv, database or by sourcing other R files.
It does so quietly - so the output does not find its way into the final document - and therefore it has messages and warnings turned off.
In addition I am often forced to wrap its content in capture.output( { ... }, file = '/dev/null') to stop any output bleeding into my final document.
As this chunk has warnings and errors supressed I found it good practice to limit it to loading data & making sure to close all database connections. Full stop.
- continue with other chunks, that do the real work using the data.frames loaded earlier.
It is normal that something breaks in the "other" chunks from time to time. That is life. But I found it advantageous to make sure the data is loaded in isolation, and that I am not left with any open database connections when stuff breaks.