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!

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay