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
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
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
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
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.
I'm using a remote server without sudo permissions.
Then that may be a problem. Perhaps, for now, can you try:
install.packages('BiocManager', repos = 'http://cran.us.r-project.org')
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
...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.
Log in to answer this question.