Home > Software design >  How to download and install package automatically if required in R?
How to download and install package automatically if required in R?

Time:07-09

How can I download and install this packages from GitHub automatically, if it is not installed in RStudio?

install.packages("remotes")
remotes::install_github("davidsjoberg/ggsankey")
library(ggsankey)

CodePudding user response:

Load ggsankey or if not installed load remotes or if that is not installed then install it and then use it to install and load ggsankey. Omit quietly=TRUE if you would prefer to see the warning messages. These instructions do not depend on RStudio and should work more generally.

Note that this installs packages but not external programs such as R itself and on Windows Rtools. The latter is needed on Windows for packages with C/C code. Also if the user is using an old version of R any package dependencies may be only available on CRAN in source form increasing the likelihood that they need Rtools on Windows. Thus the instructions should specify that the latest version of R be used and that on Windows Rtools be installed. Both these are easy to install because they have automated installers.

if (!require("ggsankey", quietly = TRUE)) {
  if (!requireNamespace("remotes", quietly = TRUE)) {
    install.packages("remotes")
  }
  remotes::install_github("davidsjoberg/ggsankey",
    dependencies = TRUE, upgrade = TRUE)
  library("ggsankey")
}

CodePudding user response:

Using p_load_gh() from pacman package

pacman::p_load_gh("davidsjoberg/ggsankey")

this will check if ggsankey is installed, if not it will install it and then load the package.

  • Related