Skip to content

Commit

Permalink
Merge pull request #48 from natverse/fix/dataframe-factors
Browse files Browse the repository at this point in the history
prefer character vectors over factors for result dataframes
  • Loading branch information
jefferis authored Jan 28, 2020
2 parents 007709c + e18293c commit ab4a796
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
8 changes: 6 additions & 2 deletions R/fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ neuprint_error_check <- function(req) {
#' @param cols Character vector specifying which columns to include (by default
#' all of those named in \code{x}, see details).
#' @param return_empty_df Return a zero row data frame when there is no result.
#' @param stringsAsFactors Whether to return character vector columns as
#' factors. Note that the default of \code{FALSE} differs from
#' \code{\link{data.frame}} and friends.
#' @param ... Additional arguments passed to \code{\link{as.data.frame}}
#' @export
neuprint_list2df <- function(x, cols=NULL, return_empty_df=FALSE, ...) {
neuprint_list2df <- function(x, cols=NULL, return_empty_df=FALSE,
stringsAsFactors=FALSE, ...) {

if(length(x)>=2 && all(c("columns", "data") %in% names(x))) {
if(is.null(cols)) cols=unlist(x$columns)
Expand Down Expand Up @@ -97,7 +101,7 @@ neuprint_list2df <- function(x, cols=NULL, return_empty_df=FALSE, ...) {
}
l[[cols[i]]]=raw_col
}
as.data.frame(l, ...)
as.data.frame(l, stringsAsFactors=stringsAsFactors, ...)
}

#' @importFrom memoise memoise
Expand Down
12 changes: 11 additions & 1 deletion man/neuprint_list2df.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-fetch.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test_that("multiplication works", {
test_that("neuprint_list2df works", {
lin=list(
columns = list(
"id",
Expand Down
Binary file modified tests/testthat/testdata/neuprint_list2df.rds
Binary file not shown.
5 changes: 4 additions & 1 deletion vignettes/customquery.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ If you are getting a nested list specifying
a table (i.e. a `data.frame` or spreadsheet like result) then there is a
function `neuprint_list2df()` which will do a lot of the heavy lifting.
It is strongly recommended to make use of this whenever possible. See
`neuprint_get_meta()` for an example.
`neuprint_get_meta()` for an example. Note that `neuprint_list2df()` has an
argument with default `stringsAsFactors=FALSE` to ensure that columns in the
output `data.frame` will be character vectors unless you take steps to make
them factors.

## Need help?

Expand Down

0 comments on commit ab4a796

Please sign in to comment.