DEV Community

Cover image for Reproducible R code with PACMAN
Mauro Loprete
Mauro Loprete

Posted on

Reproducible R code with PACMAN

Maybe you ever found yourself in the situation of having to share an R script and having to give too many explanations 😂 😂!
In R, the use of libraries is very frequent and these are installed locally on each computer, so a good way to make our codes reproducible is to use Pacman.

This library that is available in CRAN has a very useful function p_load(). This function loads the package to the work area and, if it is not installed, installs it.

Well ... yes, but in this case I should warn you to install pacman to run my code

NO !

With this simple sentence, we can do that in the case that the person does not have pacman installed, install it. And for the other libraries use p_load()

An example :

if (!require("pacman")) {
  install.packages("pacman")
  library(pacman)
}
p_load(here,pracma,tidyverse,magrittr)
Enter fullscreen mode Exit fullscreen mode

In this case, we call the library pracma, tidyverse and here.

Another library that focuses on reproducibility is "here", this library uses the workspace we are working on and concatenates its arguments separated by commas.

here("ExampleHere","Folder1","Script1.R")
Enter fullscreen mode Exit fullscreen mode

And the result :

Alt Text

Share your code!

Top comments (0)