Skip to content

Commit

Permalink
renamed zip and enumerate
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed Jun 22, 2024
1 parent e980c36 commit 1cb61a0
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 52 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export(createMutator)
export(divide)
export(enclose)
export(encloseBr)
export(enumerate)
export(enumerateit)
export(findMissingRdSections)
export(fmrs)
export(getAvgHex)
Expand Down Expand Up @@ -60,7 +60,7 @@ export(trySplit)
export(val)
export(val1)
export(warningp)
export(zip)
export(zipit)
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp,evalCpp)
importFrom(magrittr,"%>%")
Expand Down
24 changes: 15 additions & 9 deletions R/iteration.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
#' Zip Multiple Vectors or Lists
#'
#' This function combines multiple vectors or lists element-wise into a list of lists.
#' A slightly lighter weight alternative to `itertools::izip()`
#'
#' @param ... Vectors or lists to be combined.
#'
#' @return A list of lists, where each inner list contains the elements from the corresponding positions in the input vectors or lists.
#' @export
#' @keywords iteration
#' @seealso [enumerateit()]
#' @examples
#' # Zip two vectors
#' zip(c(1, 2, 3), c("a", "b", "c"))
#' zipit(c(1, 2, 3), c("a", "b", "c"))
#' # Zip three vectors
#' zip(c(1, 2), c("x", "y"), c(TRUE, FALSE))
zip <- function(...) mapply(list, ..., SIMPLIFY = FALSE)
#' zipit(c(1, 2), c("x", "y"), c(TRUE, FALSE))
zipit <- function(...) mapply(list, ..., SIMPLIFY = FALSE)

#' Enumerate Elements with Indices
#'
#' This function pairs elements of vectors or lists with their indices. The output
#' is meant to be used in a for loop, and each element extracted with the
#' [ind()], [val()], or [val1()] functions.
#' [ind()], [val()], or [val1()] functions. A slightly lighter weight alternative
#' to `itertools::enumerate()`
#'
#' @param ... Vectors or lists to be enumerated.
#' @param zero_indexed A logical indicating whether indexing should start from zero. Default is FALSE.
Expand All @@ -31,13 +34,13 @@ zip <- function(...) mapply(list, ..., SIMPLIFY = FALSE)
#'
#' @examples
#' # Enumerate a vector
#' enumerate(c("a", "b", "c"))
#' enumerateit(c("a", "b", "c"))
#' # Enumerate a vector starting from zero
#' enumerate(c("a", "b", "c"), zero_indexed = TRUE)
#' enumerateit(c("a", "b", "c"), zero_indexed = TRUE)
#' # Enumerate two vectors
#' enumerate(c(1, 2), c("x", "y"))
enumerate <- function(..., zero_indexed = FALSE) {
zip(seq_along(..1) - zero_indexed, ...)
#' enumerateit(c(1, 2), c("x", "y"))
enumerateit <- function(..., zero_indexed = FALSE) {
zipit(seq_along(..1) - zero_indexed, ...)
}

#' Get Index from Enumerated Element
Expand All @@ -49,6 +52,7 @@ enumerate <- function(..., zero_indexed = FALSE) {
#' @return The index of the enumerated element.
#' @export
#' @keywords iteration
#' @seealso [enumerateit()]
#' @examples
#' # Extract index from an enumerated element
#' elem <- list(1, "a")
Expand All @@ -65,6 +69,7 @@ ind <- function(elem) elem[[1]]
#' @return The value at the specified index in the enumerated element.
#' @export
#' @keywords iteration
#' @seealso [enumerateit()]
#' @examples
#' # Extract value from an enumerated element by index
#' elem <- list(1, "a", "b")
Expand All @@ -80,6 +85,7 @@ val <- function(elem, index) elem[[index + 1]]
#' @return The first value in the enumerated element.
#' @export
#' @keywords iteration
#' @seealso [enumerateit()]
#' @examples
#' # Extract the first value from an enumerated element
#' elem <- list(1, "a", "b")
Expand Down
6 changes: 3 additions & 3 deletions R/math.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#' @param num A numeric vector to be bounded.
#' @param lowerbound The lower bound of the range.
#' @param upperbound The upper bound of the range.
#'
#'
#' @return A numeric vector with elements bounded within the specified range.
#' @export
#' @keywords math
#'
#'
#' @examples
#' bound(1, 0, 2)
#' bound(1:10, -1, 5)
#'
#'
bound <- function(num, lowerbound, upperbound) {
sapply(num, function(x) min(max(x, lowerbound), upperbound))
}
Expand Down
56 changes: 34 additions & 22 deletions R/packageLoading.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#' @export
#' @keywords packageLoading
#'
installAndLoad <- function(cran = NULL, bioc = NULL, gh = NULL) {
installAndLoad <- function(cran = NULL, bioc = NULL, gh = NULL, lib = .libPaths()[1], verbose = FALSE) {

if (is.null(cran) && is.null(bioc) && is.null(gh)) {
message("no packages inputted")
Expand All @@ -44,44 +44,56 @@ installAndLoad <- function(cran = NULL, bioc = NULL, gh = NULL) {
# Load required libraries for installation
if (!is.null(bioc))
if (!requireNamespace("BiocManager", quietly = TRUE))
utils::install.packages("BiocManager")
utils::install.packages("BiocManager", lib = lib)

if (!is.null(gh))
if (!requireNamespace("devtools", quietly = TRUE))
utils::install.packages("devtools")
utils::install.packages("devtools", lib = lib)

# Helper function to install and load a package
install_and_load <- function(packagepath) {
install_and_load <- function(packagepath, source) {

if (is.null(packagepath)) return()

# github packages must be prioritized
# github packages must be prioritized to account for naming conventions
if (source == "GitHub") {
package <- strsplit(packagepath, "/")[[1]][2]
devtools::install_github(
packagepath, force = FALSE, quiet = TRUE, dependencies = TRUE
)
library(package, character.only = TRUE)
return()

} else {
package <- packagepath
}

package <- packagepath

if (!require(package, character.only = TRUE, quietly = TRUE)) {
if (require(package, character.only = TRUE, quietly = TRUE)) return()

if (source == "CRAN") {
utils::install.packages(package, dependencies = TRUE)
} else if (source == "Bioconductor") {
BiocManager::install(package)
}
if (source == "CRAN") {
utils::install.packages(
package,
dependencies = TRUE,
lib = lib
)
} else if (source == "Bioconductor") {
BiocManager::install(
package,
lib = lib,
dependencies = TRUE
)
} else {
warning("* installing `", package, "` from a raw GitHub repository")
devtools::install_github(
packagepath,
force = FALSE,
quiet = TRUE,
dependencies = TRUE,
lib = lib
)
}

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, el[[1]]))
}
for (el in zipi(list("CRAN", "Bioconductor", "GitHub"), list(cran, bioc, gh))) {
sapply(el[[2]], function(pkg) capture.output(suppressMessages((install_and_load(pkg, el[[1]])))))
}

return()
}
15 changes: 8 additions & 7 deletions man/enumerate.Rd → man/enumerateit.Rd

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

3 changes: 3 additions & 0 deletions man/ind.Rd

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

8 changes: 7 additions & 1 deletion man/installAndLoad.Rd

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

3 changes: 3 additions & 0 deletions man/val.Rd

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

3 changes: 3 additions & 0 deletions man/val1.Rd

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

14 changes: 9 additions & 5 deletions man/zip.Rd → man/zipit.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-iteration.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ test_that("Generate Unique Pairs Up To a Number", {
})

test_that("Zip Multiple Vectors or Lists", {
expect_equal(zip(1:3, letters[1:3]), list(list(1, "a"), list(2, "b"), list(3, "c")))
expect_equal(zipit(1:3, letters[1:3]), list(list(1, "a"), list(2, "b"), list(3, "c")))
})

test_that("Enumerate Elements with Indices", {
expect_equal(enumerate(letters[1:3]), list(list(1, "a"), list(2, "b"), list(3, "c")))
expect_equal(enumerate(letters[1:3], zero_indexed = TRUE), list(list(0, "a"), list(1, "b"), list(2, "c")))
expect_equal(enumerateit(letters[1:3]), list(list(1, "a"), list(2, "b"), list(3, "c")))
expect_equal(enumerateit(letters[1:3], zero_indexed = TRUE), list(list(0, "a"), list(1, "b"), list(2, "c")))
})

test_that("Get Index from Enumerated Element", {
Expand Down

0 comments on commit 1cb61a0

Please sign in to comment.