Is there any way to view the list

When I have data.frame objects, I can just do View(df) , and then I see data.frame in a good table (even if I can’t see all the rows, I still have the idea that the variables are my data contain).

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

I tried head(mylist) , but my console simply cannot display all the information at once. What is an effective way to look at a large list in R?

+8
source share
8 answers

I use str to view the structure of any object, especially a complex list

Rstudio shows the structure by clicking on the blue arrow in the data window:

enter image description here

+9
source

Here are some ways to look at the list:

Look at one list item:

 myList[[1]] 

Look at the head of one list item:

 head(myList[[1]]) 

See the items that are in the list neatly:

 summary(myList) 

See list structure (in more detail):

 str(myList) 

Alternatively, as suggested above, you can create your own printing method as such:

 printList <- function(list) { for (item in 1:length(list)) { print(head(list[[item]])) } } 

The above will print the title of each item in the list.

+9
source

You can also use listviewer package

 library(listviewer) jsonedit( myList ) 
+6
source

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

 str(myList, max=1) 
+4
source

you can check the "header" of your data frames using the lapply family:

 lapply(yourList, head) 

which will return the "heads" from your list.

For instance:

 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]] xy 1 0.3149013 0.8418625 2 0.8807581 0.5048528 3 0.2490966 0.2373453 [[2]] xy 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 data.frames, i.e. names , nrow ...

+1
source

Since you explicitly state 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 item you want to view.

For instance:

 View(myList[[1]]) 

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

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

 View(myList[["itemOne"]]) 

There are several other ways, but they are likely to be of use to you.

+1
source

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

For some lists, it’s convenient to print only the heading of a subset of nested objects to print the name of this 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 

USE: 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 numerical lists, the output may be more readable if the number of digits is limited to 3, for example:

 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) } } 
0
source

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

0
source

All Articles