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!

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more