Skip to content

Commit

Permalink
Merge pull request #30 from natverse/feature/keep.all
Browse files Browse the repository at this point in the history
cf_meta: add keep.all argument to keep all columns
  • Loading branch information
jefferis authored Nov 2, 2024
2 parents a2b87e4 + 455e787 commit 13ce176
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
7 changes: 5 additions & 2 deletions R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ npconn <- function(dataset) {
#' extension package.)
#' @param MoreArgs A named list of arguments to be passed when fetching metadata
#' for a given function. See details.
#' @param keep.all When fetching metadata from different datasets, whether to
#' keep all metadata columns rather than just those in common
#' (default=\code{FALSE})
#'
#' @inheritParams cf_partners
#'
Expand All @@ -39,7 +42,7 @@ npconn <- function(dataset) {
#' # / introduces a regular expression
#' mbonmeta=cf_meta(cf_ids(hemibrain='/MBON.+'))
#' }
cf_meta <- function(ids, bind.rows=TRUE, integer64=FALSE,
cf_meta <- function(ids, bind.rows=TRUE, integer64=FALSE, keep.all=FALSE,
MoreArgs=list(flywire=list(type=c("cell_type","hemibrain_type")))) {
if(is.character(ids) || inherits(ids, 'dendrogram') || inherits(ids, 'hclust'))
ids=keys2df(ids)
Expand Down Expand Up @@ -93,7 +96,7 @@ cf_meta <- function(ids, bind.rows=TRUE, integer64=FALSE,
res[[n]]=tres
}
if(length(res)==0) return(NULL)
if(bind.rows) bind_rows2(res) else res
if(bind.rows) bind_rows2(res, keep.all=keep.all) else res
}

flywire_meta <- function(ids, type=c("cell_type","hemibrain_type"), ...) {
Expand Down
15 changes: 9 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# private function to bind rows keeping common columns
bind_rows2 <- function(l) {
bind_rows2 <- function(l, keep.all=FALSE) {
ll=lengths(l)
l=l[ll>0]
if(length(l)==0) return(NULL)
if(length(l)==1) return(l[[1]])

nn=lapply(l, names)
commoncols=Reduce(intersect, nn[-1], init=nn[[1]])
l=lapply(l, "[", commoncols)

l <- do.call(function(...) rbind(..., make.row.names=FALSE), l)
if(!keep.all) {
nn=lapply(l, names)
commoncols=Reduce(intersect, nn[-1], init=nn[[1]])
l=lapply(l, "[", commoncols)
l <- do.call(function(...) rbind(..., make.row.names=FALSE), l)
} else {
l <- dplyr::bind_rows(l)
}
l
}

Expand Down
5 changes: 5 additions & 0 deletions man/cf_meta.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ test_that("metadata", {
expect_true(all(grepl("descending",
cf_meta(cf_ids(manc='DNa02'))$class)))

expect_s3_class(
dna02meta <- cf_meta(cf_ids(hemibrain = 'DNa02', manc='DNa02')),
'data.frame')
expect_s3_class(
dna02meta2 <- cf_meta(cf_ids(hemibrain = 'DNa02', manc='DNa02'), keep.all = T),
'data.frame')
expect_contains(colnames(dna02meta2), c("serial", "birthtime"))
})

0 comments on commit 13ce176

Please sign in to comment.