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

repr does not handle nested dataframes #71

Open
jankatins opened this issue Jun 19, 2016 · 5 comments
Open

repr does not handle nested dataframes #71

jankatins opened this issue Jun 19, 2016 · 5 comments

Comments

@jankatins
Copy link
Contributor

jankatins commented Jun 19, 2016

# https://blog.rstudio.org/2016/02/02/tidyr-0-4-0/
library(gapminder)
library(dplyr)
library(tidyr)
by_country <- gapminder %>% 
  group_by(continent, country) %>% 
  nest()
print(by_country) # prints ok
by_country # has the content of the data column messed up

One solution could be to add a if df contains a nested df/list/... column, refuse to rich print with a warning...

[I'm not sure if data.frames are allowed to contain nested data-frames, but I do have such a data.frame here coming from a JSON structure:

download.file("http://jsonstudio.com/wp-content/uploads/2014/02/world_bank.zip", "world_bank.zip")
world_bank <- jsonlite::stream_in(unz("world_bank.zip", "world_bank.json"))

]

@jankatins
Copy link
Contributor Author

tibble uses https://github.com/hadley/tibble/blob/7b7a0c15c527bae75676f7ba786914697bb78b11/R/type-sum.r for the short summeries of nested objects

@flying-sheep
Copy link
Member

flying-sheep commented Jun 20, 2016

what is a “nested data frame”? data.frames are lists of vectors of primitive types:

a_data_frame <- structure(
    list(Foo = 1:3, Bar = letters[1:3]),
    class = 'data.frame',
    row.names = paste0('R', 1:3))

so there’s no way to “nest” them. whatever your libraries do there isn’t returning a data.frame

@MichaelChirico
Copy link

data.frames are lists of vectors of primitive types:

This is not true. It's quite easy to create nested data.frame structures

DF = data.frame(a=1:10)
DF$l = as.list(DF$a)
DF$nested = data.frame(b = 10:1, c = letters[1:10])
DF$mat = as.matrix(DF$nested)
DF
#     a  l nested.b nested.c mat.b mat.c
# 1   1  1       10        a    10     a
# 2   2  2        9        b     9     b
# 3   3  3        8        c     8     c
# 4   4  4        7        d     7     d
# 5   5  5        6        e     6     e
# 6   6  6        5        f     5     f
# 7   7  7        4        g     4     g
# 8   8  8        3        h     3     h
# 9   9  9        2        i     2     i
# 10 10 10        1        j     1     j

@MichaelChirico
Copy link

?data.frame describes the class thus:

The function data.frame() creates data frames, tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R's modeling software.

And in Details:

A data frame is a list of variables of the same number of rows with unique row names

?data.frame also explicitly calls out how recursive structures are handled in the data.frame() constructor:

If a list or data frame or matrix is passed to data.frame it is as if each component or column had been passed as a separate argument (except for matrices protected by I).

@flying-sheep
Copy link
Member

hm, tidyverse thinks differently about nesting data.frames, but of course repr’s code shouldn’t crash when trying to render either of them. PRs welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants