diff --git a/NAMESPACE b/NAMESPACE index c94578d00a24a24b6dd2ddc1d102727de07473d6..aed6f16aa3f9705afb6db8887878053ff17c1331 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(loadLibraries) export(setup) +export(tidyNum) +importFrom(utils,install.packages) diff --git a/R/loadLibraries.R b/R/loadLibraries.R new file mode 100644 index 0000000000000000000000000000000000000000..d523862b4c41684085f07e958e932e73fa4bcf22 --- /dev/null +++ b/R/loadLibraries.R @@ -0,0 +1,33 @@ +#' 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 diff --git a/R/tidyNum.R b/R/tidyNum.R new file mode 100644 index 0000000000000000000000000000000000000000..f435e76755efef77704e5b0691bd11230e583e35 --- /dev/null +++ b/R/tidyNum.R @@ -0,0 +1,12 @@ +#' 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 diff --git a/man/loadLibraries.Rd b/man/loadLibraries.Rd new file mode 100644 index 0000000000000000000000000000000000000000..6a0223f4215a3f036106f04289ce9ffc0a7c522b --- /dev/null +++ b/man/loadLibraries.Rd @@ -0,0 +1,31 @@ +% 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) +} diff --git a/man/tidyNum.Rd b/man/tidyNum.Rd new file mode 100644 index 0000000000000000000000000000000000000000..a6e08d50e60b0e0995eb6bb1923b96a57edb8dc7 --- /dev/null +++ b/man/tidyNum.Rd @@ -0,0 +1,17 @@ +% 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} +}