How can I tell if a certain package was already installed?

后端 未结 4 785
庸人自扰
庸人自扰 2021-02-15 15:18

When I install the yaml package, an annoying error message pops up in RStudio if it had been previously installed. How can I tell if the package was already installed so I can d

4条回答
  •  -上瘾入骨i
    2021-02-15 16:02

    This will load yaml, installing it first if its not already installed:

    if (!require(yaml)) {
      install.packages("yaml")
      library(yaml)
    }
    

    or if you want to parameterize it:

    pkg <- "yaml"
    if (!require(pkg, character.only = TRUE)) {
      install.packages(pkg)
      if (!require(pkg, character.only = TRUE)) stop("load failure: ", pkg)
    }
    

    UPDATE. Parametrization.

提交回复
热议问题