Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cf_meta: add keep.all argument to keep all columns #30

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
#' 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 @@
#' # / 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 @@
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

Check warning on line 99 in R/meta.R

View check run for this annotation

Codecov / codecov/patch

R/meta.R#L99

Added line #L99 was not covered by tests
}

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"))
})
Loading