diff --git a/DESCRIPTION b/DESCRIPTION index e6d503c..9aec835 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: FastUtils Type: Package -Title: What the Package Does in One 'Title Case' Line +Title: Fast, readable utility functions. Version: 0.1.0 Date: 2024-05-23 Authors@R: c(person("Qile", "Yang", email = "qile.yang@berkeley.edu", role = c("cre","aut","cph"))) @@ -15,6 +15,7 @@ Imports: data.table, dplyr, ggplot2, + hash, Rcpp (>= 1.0.12) LinkingTo: Rcpp Suggests: diff --git a/R/color.R b/R/color.R new file mode 100644 index 0000000..33464c8 --- /dev/null +++ b/R/color.R @@ -0,0 +1,25 @@ +#' @export +#' @source https://stackoverflow.com/questions/649454 +#' +getAvgHex <- function(...) { + Reduce(function(hex1, hex2) { + grDevices::rgb( + t((grDevices::col2rgb(hex1) + grDevices::col2rgb(hex2)) / 2), + maxColorValue = 255 + ) + }, unlist(list(...))) +} + +#' @export +scaleHex <- function(hex, scaleFactor) { + + if (all(scaleFactor == 1)) return(hex) + + hsv_color <- hex %>% + grDevices::col2rgb() %>% + grDevices::rgb2hsv() + + hsv_color["v", ] <- bound(hsv_color["v", ] * scaleFactor, 0, 1) + + grDevices::hsv(hsv_color["h", ], hsv_color["s", ], hsv_color["v", ]) +} diff --git a/R/dataInitialization.R b/R/dataInitialization.R index b2b9097..4928e86 100644 --- a/R/dataInitialization.R +++ b/R/dataInitialization.R @@ -1,5 +1,16 @@ #' @export -EmptyTable <- function() { +initV <- function(typeFunc, x, initVal = NULL) { + if (is(typeFunc, "character")) { + v <- vector(typeFunc, x) + } else { + v <- typeFunc(x) + } + if (is.null(initVal)) return(v) + sapply(v, function(i) initVal) +} + +#' @export +initEmptyTable <- function() { structure( integer(0), dim = 0L, @@ -9,16 +20,26 @@ EmptyTable <- function() { } #' @export -initList <- function(x, initVal = NULL) { - l <- vector("list", x) - if (!is_an_integer(x)) names(l) <- as.character(x) - if (is.null(initVal)) return(l) - lapply(l, function(x) initVal) +tableToNumeric <- function(x) { + structure(as.numeric(x), names = names(x)) +} + +#' @export +namedNumericToTable <- function(x) { + output <- as.integer(x) + names(output) <- names(x) + output <- as.table(output) + names(dimnames(output)) <- "" + output } -#' @export -getlast <- function(x, n = 1) { - index <- length(x) - n + 1 - if (is.list(x)) return(x[[index]]) - x[index] +#' @export +createHash <- function(keys, init_vals = NULL) { + keys <- unique(keys) + numkeys <- length(keys) + switch(as.character(numkeys), + "0" = hash::hash(), + "1" = hash::hash(keys, init_vals), + hash::hash(keys, initList(numkeys, init_vals)) + ) } diff --git a/R/iteration.R b/R/iteration.R index 55863f5..6e37462 100644 --- a/R/iteration.R +++ b/R/iteration.R @@ -4,6 +4,13 @@ getUniquePairsUpTo <- function(x, oneIndexed = TRUE) { rcppGetUniquePairsUpTo(as.integer(x), oneIndexed = oneIndexed) } +#' @export +getlast <- function(x, n = 1) { + index <- length(x) - n + 1 + if (is.list(x)) return(x[[index]]) + x[index] +} + #' @export zip <- function(...) mapply(list, ..., SIMPLIFY = FALSE) diff --git a/R/math.R b/R/math.R index 3684290..3075d11 100644 --- a/R/math.R +++ b/R/math.R @@ -1,15 +1,27 @@ -bound_num <- function(num, lowerbound, upperbound) { - min(max(num, lowerbound), upperbound) +#' @export +bound <- function(num, lowerbound, upperbound) { + sapply(num, function(x) min(max(x, lowerbound), upperbound)) } -is_bound_between <- function(num, lowerbound, upperbound) { +#' @export +isBound <- function(num, lowerbound, upperbound) { (num >= lowerbound) & (num <= upperbound) } +#' @export add <- function(x, y) x + y + +#' @export subtract <- function(x, y) x - y + +#' @export multiply <- function(x, y) x * y + +#' @export divide <- function(x, y) x / y -is_even <- function(x) x %% 2 == 0 -is_odd <- function(x) x %% 2 == 1 +#' @export +isEven <- function(x) x %% 2 == 0 + +#' @export +isOdd <- function(x) x %% 2 == 1 diff --git a/README.md b/README.md index f1efb5f..756d8fe 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![MIT license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/Qile0317/FastUtils/blob/main/LICENSE.md) -R package for readable, and fast utility functions. +R package for fast, readable utility functions. ## Installation