diff --git a/greenGridr/R/dependencies.R b/greenGridr/R/dependencies.R new file mode 100644 index 0000000000000000000000000000000000000000..3008b3c3c652198a1a647f73d85eda07af2aeeae --- /dev/null +++ b/greenGridr/R/dependencies.R @@ -0,0 +1,4 @@ +# State required packages +#' @import data.table +#' @import lubridate +#' @import readr diff --git a/greenGridr/R/utilities.R b/greenGridr/R/utilities.R new file mode 100644 index 0000000000000000000000000000000000000000..8e50bfe15c61d59105b0398e5a28a480c93dccd7 --- /dev/null +++ b/greenGridr/R/utilities.R @@ -0,0 +1,63 @@ +#' 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) +} + +#' Find the path to Parent Directory +#' +#' Equivalent of \code{findParentDirectory}. Is useful for running a project +#' across multiple computers where the project is stored in different directories. +#' +#' @param Parent the name of the directory to which the function should track back. +#' Should be the root of the GitHub repository +#' +#' @author Mikey Harper, \email{m.harper@@soton.ac.uk} +#' @export +#' +findParentDirectory <- function(Parent){ + directory <-getwd() + while(basename(directory) != Parent){ + directory <- dirname(directory) + + } + return(directory) +} + + +#' Installs and loads packages +#' +#' \code{myRequiredPackages} checks whether the package is already installed, +#' installing those which are not preinstalled. All the libraries are then load. +#' +#' 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 +#' @param ... A list of packages +#' @param repository The repository to load functions from. Defaults to "https://cran.ma.imperial.ac.uk/" +#' @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 +#' +myRequiredPackages <- function(..., repository = "https://cran.rstudio.com"){ + + packages <- c(...) + + # Find if package isn't 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) +}