Skip to content

Commit

Permalink
c2()
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangchuangYu committed Dec 3, 2024
1 parent 8ac5209 commit c8f04fb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6485-8781")))
Description: Miscellaneous functions commonly used by 'YuLab-SMU'.
Imports:
Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
35 changes: 28 additions & 7 deletions inst/prototype/concat.r → R/concat.r
Original file line number Diff line number Diff line change
@@ -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))

Expand Down Expand Up @@ -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))
}
Expand All @@ -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]])
}
25 changes: 25 additions & 0 deletions man/c2.Rd

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

0 comments on commit c8f04fb

Please sign in to comment.