You can keep your working Rmd easier to navigate and less buggy by 1) packaging code into functions and 2) adding them to a companion Rfile. Load your functions with source() in the same block you load your libraries with a relative path, full path, or ideally with here().