RJSONIO icon indicating copy to clipboard operation
RJSONIO copied to clipboard

Unexpected result with toJSON(..., byrow=TRUE) on empty data frame

Open daniel-ohayon opened this issue 9 years ago • 0 comments

Hi, I noticed the following behaviour with toJSON:

cat(toJSON(data.frame(logical(0)), byrow=TRUE), '\n')
[
 [
 null 
],
[] 
] 

When I would instead expect [] (?)

I believe the problem comes from this line

  if(byrow) {
      tmp = lapply(1:nrow(x), function(i) { row = as.list(x[i, ])
                                           if(colNames)
                                             row                                             
                                           else
                                             unname(row)
                                         })

where 1:nrow(x) should be replaced by seq_len(nrow(x)) (because 1:0 is actually the vector c(1,0)).

daniel-ohayon avatar May 27 '16 09:05 daniel-ohayon