diff --git a/DESCRIPTION b/DESCRIPTION index 66fdc1f..db42e81 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -5,7 +5,7 @@ Version: 0.1.0 Date: 2024-05-23 Authors@R: c(person("Qile", "Yang", email = "qile.yang@berkeley.edu", role = c("cre","aut","cph"))) Maintainer: Qile Yang -Description: A wide variety of tools for general data analysis, wrangling, package development. All functions have vectorized implementations whenever possible. +Description: A wide variety of tools for general data analysis, wrangling, statistics, visualizations, package development, and more. All functions have vectorized implementations whenever possible. BugReports: https://github.com/Qile0317/FastUtils/issues/ License: MIT + file LICENSE Encoding: UTF-8 diff --git a/NAMESPACE b/NAMESPACE index 30c5a00..d61db72 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -27,6 +27,7 @@ export(initEmptyTable) export(initList) export(initTestthat) export(initV) +export(installAndLoad) export(isBound) export(isCamelCase) export(isEven) diff --git a/R/packageLoading.R b/R/packageLoading.R new file mode 100644 index 0000000..71d41d3 --- /dev/null +++ b/R/packageLoading.R @@ -0,0 +1,87 @@ +#' Install and Load Packages from Various Sources +#' +#' This function is designed to facilitate the installation and loading of R packages +#' from CRAN, Bioconductor, or GitHub. It checks if packages are installed; if not, it installs them, +#' and then loads them into the R session. Most importantly, it can handle an arbitrary number +#' of packages at once and install all possible dependencies +#' +#' @param cran A character vector of CRAN package names to install and load. +#' @param bioc A character vector of Bioconductor package names to install and load. +#' @param gh A character vector of GitHub repositories in the format "username/repo" to install and load. +#' +#' @details +#' The function first checks if necessary namespaces (`BiocManager` for Bioconductor packages and +#' `devtools` for GitHub packages) are installed, and installs them if they are not present. +#' It then proceeds to install and load packages from the specified sources. For GitHub packages, +#' installation is done using `devtools::install_github()`. +#' +#' @examples +#' \dontrun{ +#' # Install and load a CRAN package +#' installAndLoad(cran = c("ggplot2")) +#' +#' # Install and load a Bioconductor package +#' installAndLoad(bioc = c("GenomicFeatures")) +#' +#' # Install and load a package from GitHub +#' installAndLoad(gh = c("hadley/lubridate")) +#' +#' # Install and load packages from mixed sources +#' installAndLoad(cran = c("dplyr"), bioc = c("BiocGenerics"), gh = c("r-lib/testthat")) +#' } +#' +#' @return None, function is used for side effects (installation and loading of packages). +#' @export +#' @keywords packageLoading +#' +installAndLoad <- function(cran = NULL, bioc = NULL, gh = NULL) { + + if (is.null(cran) && is.null(bioc) && is.null(gh)) { + message("no packages inputted") + return() + } + + # Load required libraries for installation + if (!is.null(bioc)) + if (!requireNamespace("BiocManager", quietly = TRUE)) + install.packages("BiocManager") + + if (!is.null(gh)) + if (!requireNamespace("devtools", quietly = TRUE)) + install.packages("devtools") + + # Helper function to install and load a package + install_and_load <- function(packagepath) { + + if (is.null(packagepath)) return() + + # github packages must be prioritized + if (source == "GitHub") { + package <- strsplit(packagepath, "/")[[1]][2] + devtools::install_github( + packagepath, force = FALSE, quiet = TRUE, dependencies = TRUE + ) + library(package, character.only = TRUE) + return() + } + + package <- packagepath + + if (!require(package, character.only = TRUE, quietly = TRUE)) { + + if (source == "CRAN") { + install.packages(package, dependencies = TRUE) + } else if (source == "Bioconductor") { + BiocManager::install(package) + } + } + + library(package, character.only = TRUE) + } + + for (el in zip(c("CRAN", "Bioconductor", "Github"), c(cran, bioc, gh))) { + for (pkg in el[[2]]) { + suppressPackageStartupMessages(install_and_load(pkg, repo, el[[1]])) + } + } +} diff --git a/README.md b/README.md index 178349f..ea7c2a3 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![MIT license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/Qile0317/FastUtils/blob/main/LICENSE.md) -R package for fast, readable utility functions for general use. All functions have vectorized implementations whenever possible. +R package with a wide variety of fast, readable functions for general data analysis, wrangling, statistics, visualizations, package development, and more. All functions have vectorized implementations whenever possible. ## Installation @@ -23,13 +23,13 @@ library(devtools) devtools::install_github("Qile0317/FastUtils") ``` -## Usage +## Usage and Documentation ```R library(FastUtils) ``` -There is a large collection of utility functions to use. Browse the reference manual with ```help(package = "FastUtils")``` to see all functions. +There is a large collection of utility functions to use. Browse the reference manual with ```help(package = "FastUtils")``` to see all functions. Alternatively, check the reference manual online here: . ### Package Conventions @@ -37,7 +37,7 @@ Almost all exported functions and parameters are named with `camelCase`, with th ## Contributing -Github pull requests from forked branches are more than welcome as it is mostly a solo-project at the moment. For major changes, please open an issue first to discuss what you would like to change. Please also make sure to update tests as appropriate. +Github pull requests from forked branches are more than welcome as it is mostly a solo-project at the moment. For major changes, please open an issue first to discuss what you would like to change. Please also make sure to update tests as appropriate. See `CONTRIBUTING.md` for more information. ## Contact diff --git a/man/FastUtils-package.Rd b/man/FastUtils-package.Rd index f30dfed..b182068 100644 --- a/man/FastUtils-package.Rd +++ b/man/FastUtils-package.Rd @@ -6,7 +6,7 @@ \alias{FastUtils-package} \title{FastUtils: Fast, Readable Uutility Functions} \description{ -A wide variety of tools for general data analysis, wrangling, package development. All functions have vectorized implementations whenever possible. +A wide variety of tools for general data analysis, wrangling, statistics, visualizations, package development, and more. All functions have vectorized implementations whenever possible. } \seealso{ Useful links: diff --git a/man/installAndLoad.Rd b/man/installAndLoad.Rd new file mode 100644 index 0000000..1103f3b --- /dev/null +++ b/man/installAndLoad.Rd @@ -0,0 +1,47 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/packageLoading.R +\name{installAndLoad} +\alias{installAndLoad} +\title{Install and Load Packages from Various Sources} +\usage{ +installAndLoad(cran = NULL, bioc = NULL, gh = NULL) +} +\arguments{ +\item{cran}{A character vector of CRAN package names to install and load.} + +\item{bioc}{A character vector of Bioconductor package names to install and load.} + +\item{gh}{A character vector of GitHub repositories in the format "username/repo" to install and load.} +} +\value{ +None, function is used for side effects (installation and loading of packages). +} +\description{ +This function is designed to facilitate the installation and loading of R packages +from CRAN, Bioconductor, or GitHub. It checks if packages are installed; if not, it installs them, +and then loads them into the R session. Most importantly, it can handle an arbitrary number +of packages at once and install all possible dependencies +} +\details{ +The function first checks if necessary namespaces (\code{BiocManager} for Bioconductor packages and +\code{devtools} for GitHub packages) are installed, and installs them if they are not present. +It then proceeds to install and load packages from the specified sources. For GitHub packages, +installation is done using \code{devtools::install_github()}. +} +\examples{ +\dontrun{ +# Install and load a CRAN package +installAndLoad(cran = c("ggplot2")) + +# Install and load a Bioconductor package +installAndLoad(bioc = c("GenomicFeatures")) + +# Install and load a package from GitHub +installAndLoad(gh = c("hadley/lubridate")) + +# Install and load packages from mixed sources +installAndLoad(cran = c("dplyr"), bioc = c("BiocGenerics"), gh = c("r-lib/testthat")) +} + +} +\keyword{packageLoading}