Installing R packages in a Rstudio image with singularity

Hi, I am new to Nextflow and I am slowly building a simple pipeline. I am on an HPC environment. I want to add one last process which invokes an R script that uses a couple of channels.
I realised that in order to use R, it is a better to use containers via singularity. Despite using singularity/apptainer, I cannot install the R package I require: magick. As I said I am on an HPC and I don’t have root privileges.
This is my nextflow.config file

apptainer {
 enabled = true
 autoMounts = true
}


process {
withName: MakePDF {
    container = "rstudio_latest.sif"
    containerOptions = "--no-home"
}

This the error message I am getting - notice that I am installing the R package “magick” within my R script that’s invoked.

Command exit status:
  1

Command output:
  (empty)

Command error:
  Installing package into ‘/usr/local/lib/R/site-library’
  (as ‘lib’ is unspecified)
  Warning in install.packages("magick", repos = "http://cran.us.r-project.org") :
    'lib = "/usr/local/lib/R/site-library"' is not writable
  Error in install.packages("magick", repos = "http://cran.us.r-project.org") :
    unable to install packages
  Execution halted

I feel a bit stuck as I don’t know much about containers or singularity and I don’t understand the documentation just yet.

Someone suggested to download an image with the R package pre-installed but also that such things do not exist with only one package?

Can anyone help me please?

Thanks

Seqera containers is a wonderful resource for building public images.

Many R packages are available on the conda-forge channel, and so you can build a custom image using either docker or singularity ( select from the drop-down ).

All R packages on the conda-forge channel are prefixed with r- so you would search for r-base, r-essentials, r-magick for example and add these packages with the interface above.

Alternatively, there is also a command-line interface called wave-cli. Here you can write for example a conda environment.yml:

channels:
  - conda-forge
dependencies:
  - r-base
  - r-essentials
  - r-magick

and you can then supply this to the wave-cli.

wave-cli --conda-file environment.yml --freeze --await --singularity

The --freeze is important to make it available on the community repository

You can then pull the container image locally using

apptainer pull <image_name.sif> oras://url/you/recevied/from/wave-cli