How to update all the r packages in RStudio in Anaconda

Viewed 527

I'm using RStudio in a renv environment in Anaconda. I want to update the r packages in renv all at once. Merv's command in this post updates only one package at a time (in the following code it only updates the askpass package)

conda install -n renv -c conda-forge r-askpass

However, since I have a long list of r packages to be updated, I wonder if there is any command that would update all r packages at once. Thank you.

1 Answers

Available Conda CLI Options

The CLI only offers two options:

  • listing specific packages
    conda update pkg1 pkg2 ...
    
  • updating all packages
    conda update --all
    

So, not anything where you can specify all R packages.

Using a RegEx

One trick to get something in between would be to use the regex capability of conda list to obtain a list of matching packages, then use some UNIX commands to send these as arguments to the conda update command.

Here's what doing that for all packages that start with either r- or bioconductor- (the two most common prefixes for R packages) would look like:

# environment you wish to update
ENV_NAME=renv

# update all `r-` and `bioconductor-` packages
conda list -n ${ENV_NAME} '^(r|bioconductor)-' | \
  grep -vE '^#' | cut -f1 -d' ' | \
  xargs conda update -n ${ENV_NAME} -d

Important Note: I include the -d (dry run) flag in conda update to prevent automatic acceptance of proposed changes, which seems to be triggered by the use of xargs here. I strongly recommend using this as is first (always review the changes!), and then after reviewing, execute again without the -d.

Related