源代码显示了可能出现问题的位置
mongo.cursor.to.data.frame <- function(cursor, nullToNA=TRUE, ...){
warning("This fails for most NoSQL data structures. I am working on a new solution")
res <- data.frame()
while ( mongo.cursor.next(cursor) ){
val <- mongo.bson.to.list(mongo.cursor.value(cursor))
if( nullToNA == TRUE )
val[sapply(val, is.null)] <- NA
# remove mongo.oid -> data.frame can not deal with that!
val <- val[sapply(val, class) != 'mongo.oid']
res <- rbind.fill(res, as.data.frame(val, ... ))
}
return( as.data.frame(res) )
}
我们可以看到它正在使用plyr::rbind.fill
data.frames rbind
。所以这一切都归结为传入的内容rbind.fill
,即val
。
并且val
是 的结果val <- mongo.bson.to.list(mongo.cursor.value(cursor))
。
因此,只要as.data.frame(val, ...)
可以处理您传递给它的列表结构就可以了。
但是,很容易设想一个NoSQL数据结构会失败:
## consider the JSON structure
## [{"a":[1,2,3],"b":["a","b","c"]},{"d":[1.1,2.2,3.3],"e":[["nested","list"]]}]
##Which in R is the same as
lst = list(list(a = c(1L,2L,3L),
b = c("a","b","c")),
list(d = c(1.1, 2.2, 3.3),
e = list(c("nested", "list"))))
## this errors when coerced to a data.frame
as.data.frame(lst)
Error in data.frame(d = c(1.1, 2.2, 3.3), e = list(c("nested", "list")), :
arguments imply differing number of rows: 3, 2
在这一点上我应该提到mongolite
包,它通常更快,但再次返回一个data.frame
.
还有我对 mongolite 的扩展,mongolitedt
(还没有在 CRAN 上)它仍然更快并且检索数据,但再次受到结果的限制,必须强制转换为data.table