Check If R Package Is Installed Then Load Library

Check if R package is installed then load library

Use library(x,character.only=TRUE). Also you don't need the last line as suppressPackageStartupMessages(library(x,character.only=TRUE)) already loads the package.

EDIT: @LarsKotthoff is right, you already load the package inside of the if brackets. There you already use option character.only=TRUE so everything is good if you just remove last to lines of your function body.

R check list of packages, then load or install

One way you could achieve this is by checking if it is installed using the installed.packages() function. Then, you can also check which packages are loaded by using the .packages() function.

When you implement this, it'd look like this:

# Declare packages
packages <- c("dplyr","ggplot2","magrittr")

# Loop through each package
for (package in packages) {

# Install package
# Note: `installed.packages()` returns a vector of all the installed packages
if (!package in installed.packages()) {

# Install it
install.packages(
package,
dependencies = TRUE
)

}

# Load package
# Note: `.packages()` returns a vector of all the loaded packages
if (!package in .packages()) {

# Load it
library(
package,
character.only = TRUE
)

}

}

Check for installed packages before running install.packages()

try: require("xtable") or "xtable" %in% rownames(installed.packages())

Elegant way to check for missing packages and install them?

Yes. If you have your list of packages, compare it to the output from installed.packages()[,"Package"] and install the missing packages. Something like this:

list.of.packages <- c("ggplot2", "Rcpp")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

Otherwise:

If you put your code in a package and make them dependencies, then they will automatically be installed when you install your package.

How to find out which package version is loaded in R?

You can use sessionInfo() to accomplish that.

> sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] graphics grDevices utils datasets stats grid methods base

other attached packages:
[1] ggplot2_0.9.0 reshape2_1.2.1 plyr_1.7.1

loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 MASS_7.3-18 memoise_0.1 munsell_0.3
[7] proto_0.3-9.2 RColorBrewer_1.0-5 scales_0.2.0 stringr_0.6
>

However, as per comments and the answer below, there are better options

> packageVersion("snow")

[1] ‘0.3.9’

Or:

"Rmpi" %in% loadedNamespaces()

Loop and If statement to check and load packages

There are were at least 3 errors in that for loop. First, a missing argument to the first require,

... then failing to set the character.only argument for requireto TRUE so that i can get evaluated rather than taken as a package name, (see ?require Arguments section)

... and finally, failing to join the desired character values in the print calls with paste. (See ?print.default's Argument section. The print function only prints its first argument. Its second argument is the number of digits, as stated by the console error message.)

pkg <- c("stringr", "openxlsx")

for (i in pkg){
print(i)
if(require(i, character.only=TRUE)){
print(paste(i, "is loaded correctly"))
} else{
print(paste("trying to install", i))
install.packages(i)
if(require(i, character.only=TRUE)){
print(paste(i, "installed and loaded"))
} else{
stop(paste("could not install", i))
}
}
}

I did get : "Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, :
there is no package called ‘openxlsx’" after the first run of that loop which I do not understand (since the message [1] "openxlsx installed and loaded" was printed, and the package did get installed and could be loaded. I'm guessing it had something to do with these activities being done within a function and there was some mismatch of environments??? When I removed pkg:openxlsx and re-ran my code I do not get the warning message.

How to make if statement to check whether a R package is installed or not in R

You are looking for installed.packages() That will list all installed packages. Another option is require(thepackage) which will either load the package or return FALSE if the 'thepackage' is not available.



Related Topics



Leave a reply



Submit