R for data science

Create a custom R working environment for reproducible research

  • Start an RStudio job session and choose an appropriate R version with MKL support, such as 4.4.0-MKL. MKL (Math Kernel Library) is used for compiling R packages with optimized performance.

  • Mount the /public/utils/renv and your project directories (such as /myproject) to the RStudio job session.

  • Create an R project in the /myproject directory.

  • Install the renv package and initialize the project:

install.packages("renv")
  • Create a .Renviron file in the project directory and add the following code:
# point the renv cache to the shared directory
RENV_PATHS_CACHE=/work/renv
  • Restart the R session and run the following code:
# Check if the renv cache is set correctly
renv::paths$cache()

Optional: Using Posit Package Manager for Bioconductor and CRAN repositories

you can add the following code to the .Rprofile:

# Configure BioCManager to use Posit Package Manager:
options(BioC_mirror = "https://packagemanager.posit.co/bioconductor")
options(BIOCONDUCTOR_CONFIG_FILE = "https://packagemanager.posit.co/bioconductor/config.yaml")

# Configure a CRAN snapshot compatible with Bioconductor 3.19:
options(repos = c(CRAN = "https://packagemanager.posit.co/cran/__linux__/jammy/latest"))

Using Posit Package Manager (formerly RStudio Package Manager) for configuring Bioconductor and CRAN repositories offers several advantages:

  • Reproducibility: Ensures that the same package versions are used across different environments and over time, which is crucial for reproducible research.
  • Reliability: Provides a stable and reliable source for package installations, reducing the risk of downtime or unavailability.
  • Performance: Often faster and more optimized package downloads compared to the default repositories.
  • Compatibility: Ensures compatibility between CRAN and Bioconductor packages, reducing the likelihood of dependency issues.
  • Security: Packages are scanned for security vulnerabilities, providing a safer environment for package installations.

Initialize the renv project

  • Run the following code to initialize the renv project:
renv::init()

If you have an existing project, renv will automatically detect the existing packages and create a lockfile.