If R package is already install - load it; if not installed, install it
1
0
Entering edit mode
2.4 years ago

I want to create a function to install only missing packages. If the package is already install, just load it. My R function below failed to install any of the packages (KEGGREST, EnrichmentBrowser, org.Hs.eg.db). Why?

# Install missing packages or load the package if already installed
if(!requireNamespace('BiocManager', quietly=TRUE)){
  install_version('BiocManager', version='3.14', repos="http://cran.us.r-project.org")
}
require('BiocManager')

  packages=c("KEGGREST", "EnrichmentBrowser", "org.Hs.eg.db")
  package.check <- sapply(
    packages,
    FUN = function(x) {
      if (!requireNamespace(x, quietly=TRUE)){
        BiocManager::install(x, dependencies=TRUE)
      }
      library(x, character.only=TRUE)
    }
  )

Traceback:

Error in library(x, character.only = TRUE) :
  there is no package called ‘KEGGREST’
Calls: sapply -> lapply -> FUN -> library
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted
R • 2.6k views
ADD COMMENT
0
Entering edit mode
2.4 years ago

Hi, I would do it like this, first ensuring that BiocManager is installed, and then using that as the installer. Also note the use of sapply() (not a big difference maybe) and requireNamespace():

if (!requireNamespace('BiocManager', quietly = TRUE)) {
    install.packages('BiocManager')
}
require('BiocManager')

# Install missing packages or load the package if already installed    
  packages <- c('KEGGREST', 'EnrichmentBrowser', 'org.Hs.eg.db')
  package.check <- sapply(
    packages,
    FUN = function(x) {
      if (!requireNamespace(x, quietly = TRUE)) {
        BiocManager::install(x, dependencies = TRUE)
      }
      library(x, character.only = TRUE)
    }
  )

Kind regards,

Kevin

ADD COMMENT
0
Entering edit mode

It returns a new error:

Error in contrib.url(repos, type) : trying to use CRAN without setting a mirror Calls: install.packages -> startsWith -> contrib.url Execution halted

ADD REPLY
0
Entering edit mode

From where are you running R - RStudio on Windows? You basically need to first configure your mirror for CRAN. This is a problem that is unrelated [directly] to the code that I have posted.

ADD REPLY
0
Entering edit mode

I'm using a remote server without sudo permissions.

ADD REPLY
1
Entering edit mode

Then that may be a problem. Perhaps, for now, can you try:

install.packages('BiocManager', repos = 'http://cran.us.r-project.org')
ADD REPLY
0
Entering edit mode

I specified a Bioconductor version which is compatible to the packages that I want to install. However, the code is unable to install any of the packages.

Error in library(x, character.only = TRUE) :
  there is no package called ‘KEGGREST’
Calls: sapply -> lapply -> FUN -> library
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted
ADD REPLY
0
Entering edit mode

...and if you try to install KEGGREST manually? You probably run into access issues, in which case the code that you wrote -- and that I further developed -- will neither be able to install the package.

ADD REPLY

Login before adding your answer.

Traffic: 848 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6