Is there a way to see the content of a list column in the console?

Hello there,

List-columns are really the best thing in the world, but I wonder if there is a way to see in the terminal (at least) the beginning of the contents of my list column.

Say,

frame <- data_frame(mylist = list(list('hello', 'there'), list('hello', 'world')),
                    value = c(1, 2))

> frame
# A tibble: 2 x 2
  mylist     value
  <list>     <dbl>
1 <list [2]>     1
2 <list [2]>     2

Here I have no idea what's inside the list. Can I tweak pillar or tibble so that the column mylist would appear as list('hello', 'there') even at the cost of truncating the output?

Something like

> frame
# A tibble: 2 x 2
  mylist                 value
  <chr>                  <dbl>
1 list('hello', 'there')     1
2 list('hello', 'world')     2

That would be much more useful that having to use listviewer::jsonedit all the time :slight_smile:
Thanks!

I don't know about customisation using pillar but glimpse in dplyr can help.

library(dplyr)
glimpse(frame)
#> Observations: 2
#> Variables: 2
#> $ mylist <list> [["hello", "there"], ["hello", "world"]]
#> $ value  <dbl> 1, 2

Created on 2018-08-29 by the reprex package (v0.2.0).

thanks @cderv but i was thinking more of something that would tweak the printing directly

You can create custom print methods for tibble:
https://tibble.tidyverse.org/articles/extending.html

There are also some specialized print methods in some packages, I don't know much more than that about them, though:
http://library.open.oregonstate.edu/computationalbiology/chapter/objects-and-classes-in-r/

2 Likes

ha!! thanks mara! that looks pretty neat but do you have any idea how that could work here? the customization code looks pretty involved...

Like cderv suggested, glimpse() is helpful here, you could also use str().

If you want things to print a certain way without using a function, I think that's precisely what customized print methods are for. The fixing list columns section goes into more detail: https://tibble.tidyverse.org/articles/extending.html#fixing-list-columns

1 Like

Part of the difficulty of printing a list column is that every list looks different :frowning: You could write a print method, but it'd be difficult to write one that'd be useful for any old list column :confused:

3 Likes

yeah, that is true. somehow i would like to print whatever would show up in the terminal after str... but perhaps mara is right the easiest would be to play around with glimpse...