Skip to content

Commit

Permalink
added package loader and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed Jun 5, 2024
1 parent 5530399 commit 3a37b86
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Version: 0.1.0
Date: 2024-05-23
Authors@R: c(person("Qile", "Yang", email = "[email protected]", role = c("cre","aut","cph")))
Maintainer: Qile Yang <[email protected]>
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
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(initEmptyTable)
export(initList)
export(initTestthat)
export(initV)
export(installAndLoad)
export(isBound)
export(isCamelCase)
export(isEven)
Expand Down
87 changes: 87 additions & 0 deletions R/packageLoading.R
Original file line number Diff line number Diff line change
@@ -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]]))
}
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![MIT license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/Qile0317/FastUtils/blob/main/LICENSE.md)
<!-- badges: end -->

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

Expand All @@ -23,21 +23,21 @@ 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: <https://qile0317.github.io/FastUtils/reference/index.html>.

### Package Conventions

Almost all exported functions and parameters are named with `camelCase`, with the exception of those that try to modify or add on to existing functions from other packages with established ecosystems such as `testthat`.

## 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

Expand Down
2 changes: 1 addition & 1 deletion man/FastUtils-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions man/installAndLoad.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a37b86

Please sign in to comment.