From 3bc2196fffea17420f054bbc1d0d0ff323b40c5d Mon Sep 17 00:00:00 2001
From: "B.Anderson" <ba1e12@srv02405.soton.ac.uk>
Date: Sun, 17 May 2020 23:40:17 +0100
Subject: [PATCH] added a couple of utility functions

---
 NAMESPACE            |  3 +++
 R/loadLibraries.R    | 33 +++++++++++++++++++++++++++++++++
 R/tidyNum.R          | 12 ++++++++++++
 man/loadLibraries.Rd | 31 +++++++++++++++++++++++++++++++
 man/tidyNum.Rd       | 17 +++++++++++++++++
 5 files changed, 96 insertions(+)
 create mode 100644 R/loadLibraries.R
 create mode 100644 R/tidyNum.R
 create mode 100644 man/loadLibraries.Rd
 create mode 100644 man/tidyNum.Rd

diff --git a/NAMESPACE b/NAMESPACE
index c94578d..aed6f16 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 0000000..d523862
--- /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 0000000..f435e76
--- /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 0000000..6a0223f
--- /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 0000000..a6e08d5
--- /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}
+}
-- 
GitLab