20

When I have data.frame objects, I can simply do View(df), and then I get to see the data.frame in a nice table (even if I can't see all of the rows, I still have an idea of what variables my data contains).

But when I have a list object, the same command does not work. And when the list is large, I have no idea what the list looks like.

I've tried head(mylist) but my console simply cannot display all of the information at once. What's an efficient way to look at a large list in R?

2
  • You can write a custom print method.
    – Pierre L
    Commented Sep 16, 2015 at 20:53
  • @JakubKania My RStudio console doesn't display the top of the list for some reason? I'm assuming my list is way too big (148MB)
    – Adrian
    Commented Sep 16, 2015 at 20:58

11 Answers 11

24

Here's a few ways to look at a list:

Look at one element of a list:

myList[[1]]

Look at the head of one element of a list:

head(myList[[1]])

See the elements that are in a list neatly:

summary(myList)

See the structure of a list (more in depth):

str(myList)

Alternatively, as suggested above you could make a custom print method as such:

printList <- function(list) {

  for (item in 1:length(list)) {

    print(head(list[[item]]))

  }
}

The above will print out the head of each item in the list.

15

I use str to see the structure of any object, especially complex list's

Rstudio shows you the structure by clicking at the blue arrow in the data-window:

enter image description here

11

You can also use a package called listviewer

library(listviewer)
jsonedit( myList )
1
  • This is the best way to get an overview. If you have large complex lists you can simplify with rapply(x, class, how="list") %>% jsonedit Commented Mar 5, 2019 at 14:29
10

If you have a really large list, you can look at part of it using

str(myList, max.level=1)

(If you don't feel like typing out the second argument, it can be written as max=1 since there are no other arguments that start with max.)

I do this often enough that I have an alias in my .Rprofile for it:

str1 <- function(x, ...) str(x, max.level=1, ...)

And a couple others that limit the printed output (see example(str) for an example of using list.len):

strl <- function(x, len=10L, ...) str(x, list.len=len, ...)  # lowercase L in the func name
str1l <- function(x, len=10L, ...) str(x, max.level=1, list.len=len, ...)
1
  • Great suggestion, imo. Commented Sep 16, 2015 at 23:26
5

you can check the "head" of your dataframes using lapply family:

lapply(yourList, head)

which will return the "heads" of you list.

For example:

df1 <- data.frame(x = runif(3), y = runif(3))
df2 <- data.frame(x = runif(3), y = runif(3))
dfs <- list(df1, df2)

lapply(dfs, head)

Returns:

> lapply(dfs, head)
[[1]]
          x         y
1 0.3149013 0.8418625
2 0.8807581 0.5048528
3 0.2490966 0.2373453

[[2]]
          x         y
1 0.4132597 0.5762428
2 0.0303704 0.3399696
3 0.9425158 0.5465939

Instead of "head" you can use any function related to the data.frames, i.e. names, nrow...

3

Seeing as you explicitly specify that you want to use View() with a list, this is probably what you are looking for:

View(myList[[x]])

Where x is the number of the list element that you wish to view.

For example:

View(myList[[1]])

will show you the first element of the list in the standard View() format that you will be used to in RStudio.

If you know the name of the list item you wish to view, you can do this:

View(myList[["itemOne"]])

There are several other ways, but these will probably serve you best.

2

This is a simple edit of giraffehere's excellent answer.

For some lists it is convenient to only print the head of a subset of the nested objects, to print the name of the given slot above the output of head().

Arguments:

#'@param list a list object name
#'@param n an integer - the the objects within the list that you wish to print
#'@param hn an integer - the number of rows you wish head to print

USAGE: printList(mylist, n = 5, hn = 3)

printList <- function(list, n = length(list), hn = 6) {

  for (item in 1:n) {
    cat("\n", names(list[item]), ":\n")
    print(head(list[[item]], hn))

  }
}

For numeric lists, output may be more readable if the number of digits is limited to 3, eg:

printList <- function(list, n = length(list), hn = 6) {

  for (item in 1:n) {
    cat("\n", names(list[item]), ":\n")
    print(head(list[[item]], hn), digits = 3)

  }
}
1

I like using as.matrix() on the list and then can use the standard View() command.

0

I had a similar problem and managed to solve it using as_tibble() on my list (dplyr or tibble packages), then just use View() as usual.

0

In recent versions of RStudio, you can just use View() (or alternatively click on the little blue arrow beside the object in the Global Environment pane).

For example, if we create a list with:

test_list <- list(
  iris,
  mtcars
)

Then either of the above methods will show you:

enter image description here

0

lobstr::tree() is a great function for this - it prints a tree that shows the structure of list-like objects:

library(lobstr)

my_list <- 
  list(
    "hello",
    list(
      c("some", "words", "here"),
      list(c(1:4), c(11:14)
      )
    ),
    list(
      list(
        list(10)
      )
    )
  )

tree(my_list)
#> <list>
#> ├─"hello"
#> ├─<list>
#> │ ├─<chr [3]>"some", "words", "here"
#> │ └─<list>
#> │   ├─<int [4]>1, 2, 3, 4
#> │   └─<int [4]>11, 12, 13, 14
#> └─<list>
#>   └─<list>
#>     └─<list>
#>       └─10

Created on 2024-03-11 with reprex v2.1.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.