Rmd install.packages() best practices

When collaborating on Rmds and their knit outputs, what's everyone's best practice for including/not including install.packages() in the R markdown file?

In the past, when I was leading a team, my practice was just to use the library() call in Rmarkdown and let the errors inform the user on what packages they need to install if they need to re-knit the output.

I just joined a new team where the person I'm backfilling always used a call to install.packages() for every package in his Rmd's first chunk, with the second chunk including all the library() calls.

I don't know if there's any agreement on best practice(s) in the community regarding installing or not installing packages directly from the Rmd file...

Thank you for your thoughts!

I would say: don't ever put install.packages() in to an Rmd (or an R script for that matter).

Attach pacakges with library(), and let the user discover that they don't have a package installed that way. (And RStudio will also alert you if you are missing a required package).

That way, errors/problems during package installation are easier to see/not tangled up with running the Rmd/R script.


I agree with not putting install.packages() in your Rmd.

Most of the other folks in my group don't use R as much as I do, so I use pacman::p_load() in all my code.

This makes it easier for them if I'm using a package they don't have loaded.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.