Showing posts with label package. Show all posts
Showing posts with label package. Show all posts

Tuesday, 28 June 2016

R Install Required Packages

When using R, if there is a script with multiple packages that might not be installed, it can take some time to go through the list and check they are all installed.

Instead of using the "require(packagename)" command, I used the following script to load packages

pkgTest <- function(x)
{
  if (!require(x,character.only = TRUE))
  {
    install.packages(x,dep=TRUE)
    if(!require(x,character.only = TRUE)) stop("Package not found")
  }
}

This script tries to load the package using the "require" command, and if this is not successful then will try to install it. It then tries the "require" command once more, and if not successful will stop the code execution.

Wednesday, 22 October 2014

"yarn" in R

Whilst trying to follow the PCA tutorial from the following page http://nir-quimiometria.blogspot.de/2012/02/pca-for-nir-spectrapart-001-plotting.html I couldn't find the "yarn" dataset, and couldn't find any information about where it was from, whether it was bundled with the R source etc. Trying to evaluate yarn simply gave the following:
Error: object 'yarn' not found
After some searching I stumbled across the documentation for the pls library. It turns out the data is bundled with the pls library. Running the following allowed me to use the dataset:
install.packages("pls")
library("pls")
Now on with the rest of the tutorial!