Skip to content

Commit

Permalink
Return correct R types for query results as="data.frame"
Browse files Browse the repository at this point in the history
Issue: /issues/250

When a Solr query result is returned as a data.frame, ensure that multi-valued fields are returned as a vector of the correct corresponding R type, and not as a character string of the concatenated values.
  • Loading branch information
gothub committed Nov 13, 2020
1 parent d268a01 commit 4323543
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions R/D1Node.R
Original file line number Diff line number Diff line change
Expand Up @@ -856,18 +856,28 @@ setMethod("query", signature("D1Node"), function(x, solrQuery=as.character(NA),
res <- parseSolrResult(xmlDoc, parse)
dfAll <- data.frame()
if (length(res) > 0) {
# Simplify each result and cast to a data.frame
# Simplify each result and cast to a data.frame, returning a list of
# data.frames that will be combined by rbind.fill
simplified <- lapply(res, function(r) {
# Simplify multi-valued fields into space-separted character vectors
# Simplify multi-valued fields into space-separated character vectors
for (n in names(r)) {
if(typeof(r[[n]]) == "list") {
r[[n]] <- paste(r[[n]], collapse = "|")
# Get R type from result set
c1 <- class(r[[n]][[1]])
# flatten list, then reassign R type, as unlist removes attributes
u1 <- unlist(r[[n]])
# Reassign type to values
class(u1) <- c1
# Wrap value vector in a list so as.data.frame to appease as.data.frame, other
# will get error "arguments imply differing number of rows: 1, 2, 3 "
r[[n]] <- I(list(u1))
}
}

as.data.frame(r, stringsAsFactors = FALSE)
})

# rbind.file combines a list of data.frames into one
dfAll <- do.call(rbind.fill, simplified)
}
res <- dfAll
Expand Down

0 comments on commit 4323543

Please sign in to comment.