Skip to content
Snippets Groups Projects
Commit 3bc2196f authored by B.Anderson's avatar B.Anderson
Browse files

added a couple of utility functions

parent 4865f2f8
No related branches found
No related tags found
No related merge requests found
# Generated by roxygen2: do not edit by hand
export(loadLibraries)
export(setup)
export(tidyNum)
importFrom(utils,install.packages)
#' Installs and loads packages
#'
#' \code{loadLibraries} checks whether the package is already installed,
#' installing those which are not preinstalled. All the libraries are then loaded.
#'
#' Especially useful when running on virtual machines where package installation
#' is not persistent (Like UoS sve). It will fail if the packages need to be
#' installed but there is no internet access.
#'
#' NB: in R 'require' tries to load a package but throws a warning & moves on if it's not there
#' whereas 'library' throws an error if it can't load the package. Hence 'loadLibraries'
#' https://stackoverflow.com/questions/5595512/what-is-the-difference-between-require-and-library
#' @param ... A list of packages
#' @param repo The repository to load functions from. Defaults to "https://cran.rstudio.com"
#' @importFrom utils install.packages
#'
#' @author Luke Blunden, \email{lsb@@soton.ac.uk} (original)
#' @author Michael Harper \email{m.harper@@soton.ac.uk} (revised version)
#' @export
#'
loadLibraries <- function(..., repo = "https://cran.rstudio.com"){
packages <- c(...)
# Check if package is installed
newPackages <- packages[!(packages %in% utils::installed.packages()[,1])]
# Install if required
if (length(newPackages)){utils::install.packages(newPackages, dependencies = TRUE)}
# Load packages
sapply(packages, require, character.only = TRUE)
}
\ No newline at end of file
#' Tidy long numbers
#'
#' \code{tidyNum} reformats long numbers to include commas and prevents scientific formats
#'
#' @param number an input number or list
#'
#' @author Ben Anderson, \email{b.anderson@@soton.ac.uk}
#' @export
#'
tidyNum <- function(number) {
format(number, big.mark=",", scientific=FALSE)
}
\ No newline at end of file
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/loadLibraries.R
\name{loadLibraries}
\alias{loadLibraries}
\title{Installs and loads packages}
\usage{
loadLibraries(..., repo = "https://cran.rstudio.com")
}
\arguments{
\item{...}{A list of packages}
\item{repo}{The repository to load functions from. Defaults to "https://cran.rstudio.com"}
}
\description{
\code{loadLibraries} checks whether the package is already installed,
installing those which are not preinstalled. All the libraries are then loaded.
}
\details{
Especially useful when running on virtual machines where package installation
is not persistent (Like UoS sve). It will fail if the packages need to be
installed but there is no internet access.
NB: in R 'require' tries to load a package but throws a warning & moves on if it's not there
whereas 'library' throws an error if it can't load the package. Hence 'loadLibraries'
https://stackoverflow.com/questions/5595512/what-is-the-difference-between-require-and-library
}
\author{
Luke Blunden, \email{lsb@soton.ac.uk} (original)
Michael Harper \email{m.harper@soton.ac.uk} (revised version)
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/tidyNum.R
\name{tidyNum}
\alias{tidyNum}
\title{Tidy long numbers}
\usage{
tidyNum(number)
}
\arguments{
\item{number}{an input number or list}
}
\description{
\code{tidyNum} reformats long numbers to include commas and prevents scientific formats
}
\author{
Ben Anderson, \email{b.anderson@soton.ac.uk}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment