From c8f04fbc0eb3d0b40e0dfd08a0da1825b8d7ab47 Mon Sep 17 00:00:00 2001 From: Guangchuang Yu Date: Tue, 3 Dec 2024 17:37:41 +0800 Subject: [PATCH] c2() --- DESCRIPTION | 2 +- Makefile | 7 ++++--- NAMESPACE | 7 +++++++ NEWS.md | 4 ++++ {inst/prototype => R}/concat.r | 35 +++++++++++++++++++++++++++------- man/c2.Rd | 25 ++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 11 deletions(-) rename {inst/prototype => R}/concat.r (68%) create mode 100644 man/c2.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 6c07285..e056331 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: yulab.utils Title: Supporting Functions for Packages Maintained by 'YuLab-SMU' -Version: 0.1.8 +Version: 0.1.8.001 Authors@R: c(person("Guangchuang", "Yu", email = "guangchuangyu@gmail.com", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781"))) Description: Miscellaneous functions commonly used by 'YuLab-SMU'. Imports: diff --git a/Makefile b/Makefile index 88a0529..b76a553 100644 --- a/Makefile +++ b/Makefile @@ -16,9 +16,10 @@ readme2: Rscript -e 'rmarkdown::render("README.Rmd", "html_document")' build: - cd ..;\ - R CMD build $(PKGSRC) - + #cd ..;\ + #R CMD build $(PKGSRC) + Rscript -e 'devtools::build()' + build2: cd ..;\ R CMD build --no-build-vignettes $(PKGSRC) diff --git a/NAMESPACE b/NAMESPACE index 0d63f27..6e56028 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,10 +1,17 @@ # Generated by roxygen2: do not edit by hand +S3method("[",chunked_array) +S3method(as.vector,chunked_array) +S3method(is.character,chunked_array) +S3method(is.numeric,chunked_array) +S3method(length,chunked_array) +S3method(print,chunked_array) S3method(print,exec) export(Biocpkg) export(CRANpkg) export(Githubpkg) export(auto_set_regexpr_style) +export(c2) export(check_pkg) export(combinations) export(exec) diff --git a/NEWS.md b/NEWS.md index fedf8dc..0985cc3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +# yulab.utils 0.1.8.001 + ++ `c2()` to concate two vectors into a 'chunked_array' object (2024-12-03, Tue) + # yulab.utils 0.1.8 + add new citation (The Innovation 2024) (2024-11-07, Thu) diff --git a/inst/prototype/concat.r b/R/concat.r similarity index 68% rename from inst/prototype/concat.r rename to R/concat.r index da33fc4..7bc8854 100644 --- a/inst/prototype/concat.r +++ b/R/concat.r @@ -1,4 +1,17 @@ +#' chunked array +#' +#' concate two vector/chunked_array into a chunked_array object +#' @title c2 +#' @param x a vector or chunked_array object +#' @param y a vector or chunked_array object +#' @return chunked_array object +#' @author Guangchuang Yu +#' @export c2 <- function(x, y) { + + if (length(x) == 0) return(y) + if (length(y) == 0) return(x) + same_mode <- (is.numeric(x) && is.numeric(y)) || (is.character(x) && is.character(y)) @@ -36,18 +49,21 @@ as_chunked_array <- function(x) { #' @method as.vector chunked_array +#' @export as.vector.chunked_array <- function(x, mode = "any") { unlist(x$vector_list) } #' @method print chunked_array -print.chunked_array <- function(x) { +#' @export +print.chunked_array <- function(x, ...) { n <- length(x) msg <- sprintf("chunked array with size of %d\n", n) cat(msg) } #' @method length chunked_array +#' @export length.chunked_array <- function(x) { last_item(x$idx) + length(last_item(x$vector_list)) } @@ -59,22 +75,27 @@ last_item <- function(x) { } #' @method [ chunked_array +#' @export `[.chunked_array` <- function(x, i, ...) { - array_idx <- vapply(i, \(ii) last_item(which(ii > x$idx)), numeric(1)) - - pos <- i - x$idx[array_idx] - - output <- ifelse(is.numeric(x), numeric(1), character(1)) - vapply(seq_along(i), \(j) x$vector_list[[array_idx[j]]][pos[j]], output) + # array_idx <- vapply(i, \(ii) last_item(which(ii > x$idx)), numeric(1)) + # + # pos <- i - x$idx[array_idx] + # + # output <- ifelse(is.numeric(x), numeric(1), character(1)) + # vapply(seq_along(i), \(j) x$vector_list[[array_idx[j]]][pos[j]], output) + + as.vector(x)[i] # faster :) } #' @method is.numeric chunked_array +#' @export is.numeric.chunked_array <- function(x) { is.numeric(x$vector_list[[1]]) } #' @method is.character chunked_array +#' @export is.character.chunked_array <- function(x) { is.character(x$vector_list[[1]]) } diff --git a/man/c2.Rd b/man/c2.Rd new file mode 100644 index 0000000..3dc0ab5 --- /dev/null +++ b/man/c2.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/concat.r +\name{c2} +\alias{c2} +\title{c2} +\usage{ +c2(x, y) +} +\arguments{ +\item{x}{a vector or chunked_array object} + +\item{y}{a vector or chunked_array object} +} +\value{ +chunked_array object +} +\description{ +chunked array +} +\details{ +concate two vector/chunked_array into a chunked_array object +} +\author{ +Guangchuang Yu +}