OToPS/RStudio setup
RStudio Setup
We suggest R Studio as the platform for working with R (rather than Base R, or JASP until it develops a robust way of exporting code for reproducibility).
Steps:
- Download R
- Download R Studio (personal installation)
- (Optional) Download R tools
Next we can get a list of packages that are often used in our workflows.
A few approaches are commonly used to replicate (or “clone”) an R package environment across different machines. Below are two popular strategies: one using base R commands, and another using renv for stricter reproducibility.
1. Using Base R Commands
Exporting the Package List On the source machine, get a list of all user-installed packages (excluding base and recommended packages) and save it to a file:
r Copy code
- List of all installed packages
installed <- installed.packages()
- We usually want to exclude base and recommended packages
user_installed <- installed[is.na(installed[, "Priority"]), "Package"]
- Save package list to an RDS file
saveRDS(user_installed, "user_installed_pkgs.rds") Copy user_installed_pkgs.rds to the target machine.
Installing on the New Machine On the target machine, read the package list and install: r Copy code
- Load the list of packages
pkgs_to_install <- readRDS("user_installed_pkgs.rds")
- Install all packages in that list
install.packages(pkgs_to_install) This approach is straightforward and very flexible. You can edit the list or exclude certain packages if needed before installing. However, you may lose fine-grained control over package versions or dependencies if CRAN has been updated in the meantime.
2. Using the renv Package for Reproducibility If your goal is to exactly replicate package versions (including dependencies), renv is often considered the most robust approach. It takes a “snapshot” of your project’s environment (with package versions) so you can restore it on another machine. This is especially important for reproducible research and collaboration.
On the source machine (in your R project folder), initialize renv:
r Copy code install.packages("renv") library(renv)
renv::init() This will create a renv.lock file that records all of the package names and exact versions used in the project. Copy the entire project folder (including the renv folder and renv.lock file) to the target machine.
On the target machine, open the R project folder and run:
r Copy code renv::restore() This will install all packages (and their dependencies) in the same versions that were recorded in renv.lock. Because renv locks both package names and exact versions, this is the preferred way to ensure strict reproducibility—much more so than simply exporting and installing whatever is current on CRAN.
Which Method Should You Use? Simple duplication of current packages: Use the base R approach (export a list, then install on the new machine). Exact duplication of package versions: Use renv for a fully reproducible environment—especially critical for research, production code, or collaboration. In most modern workflows, renv is recommended because it solves version mismatch headaches and ensures your code runs exactly the same way on different systems or at different points in time.