6 min read

List Inspection-Part-04


list inspection
Before you can apply a function to every element of a list, you’d better understand the list! Dr. Jenny Bryan, Professor of Biostatistics.

View raw source for this post

Summary

List inspection is key to the effective manipulation and transformation of the data.

Table of Contents

Overview

Professor Bryan’s full quote on list inspection is: “[l]ist inspection is very important and also fairly miserable. Before you can apply a function to every element of a list, you’d better understand the list!” Originally, I didn’t think this post was necessary until I ran into some large lists and also discovered some nice features in the Rstudio development environment.

Additionally, Professor Bryan also created a tutorial dedicated to inspecting lists here. For the rest of the post, we’ll be trying out functions using the gh_repos list in the repurrrsive package. In the past, lists made me miserable because I couldn’t see what I was working with in the environment window.

Basics

A StackOverflow question was asked about how to view a list. The following functions were gleaned from the question. Traditional commands to inspect an object work on a list like str(), head(), and summary().

library(repurrrsive)
# load
data("gh_repos")
# check
is.list(gh_repos)
[1] TRUE
#evaluation omitted due to length
str(gh_repos); head(gh_repos); summary(gh_repos)

The return from str() function can be narrowed through use of string subsetting. For example, str(gh_repos[[1]] will only return information from the first element of the gh_repos data. A second way to limit output from the str() function is through use of the max.level argument and the list.len argument.

str(gh_repos[[1]][[1]], max.level = 1, list.len = 10)
List of 68
 $ id               : int 61160198
 $ name             : chr "after"
 $ full_name        : chr "gaborcsardi/after"
 $ owner            :List of 17
 $ private          : logi FALSE
 $ html_url         : chr "https://github.com/gaborcsardi/after"
 $ description      : chr "Run Code in the Background"
 $ fork             : logi FALSE
 $ url              : chr "https://api.github.com/repos/gaborcsardi/after"
 $ forks_url        : chr "https://api.github.com/repos/gaborcsardi/after/forks"
  [list output truncated]

Rstudio tools

The Rstudio environment offers a several helpful tools. In the course of writing this, I discovered some very helpful features.

A large list.  The red arrow on the left points to the blue drop-down to see the list.  The red arrow on the right shows the magnifying glass to inspect the list.

Figure 1: A large list. The red arrow on the left points to the blue drop-down to see the list. The red arrow on the right shows the magnifying glass to inspect the list.

After clicking on the magnifying glass, a viewer window will open.  The red rectangle shows the blue dropdown boxes for further inspection of the lists.

Figure 2: After clicking on the magnifying glass, a viewer window will open. The red rectangle shows the blue dropdown boxes for further inspection of the lists.

Clicking the green arrow will show the list elements index.  Here it is "gh_repos[[1]][[1]][["id"]]"

Figure 3: Clicking the green arrow will show the list elements index. Here it is “gh_repos[[1]][[1]][[”id”]]”

listviewer

Additionally, the package listviewer can assist with navigating lists though its functionality is similar to the Rstudio environment.

library(listviewer)
jsonedit(gh_repos)
User interface for the `listviewer` package

Figure 4: User interface for the listviewer package

Conclusion

Being able to manipulate and transform lists effectively requires a working knowledge of their contents. Function commands like str() can be helpful but be sure to employ Rstudio’s native GUI functionalities as well. They can be a big productivity boost.

References

[1]
R Core Team, R: A language and environment for statistical computing. Vienna, Austria: R Foundation for Statistical Computing, 2022 [Online]. Available: https://www.R-project.org/
[2]
Y. Xie, C. Dervieux, and A. Presmanes Hill, Blogdown: Create blogs and websites with r markdown. 2022 [Online]. Available: https://CRAN.R-project.org/package=blogdown
[3]
J. de Jong, M. Gainer, and K. Russell, Listviewer: Htmlwidget for interactive views of r lists. 2019 [Online]. Available: https://github.com/timelyportfolio/listviewer
[4]
J. Bryan, Repurrrsive: Examples of recursive lists and nested or split data frames. 2019 [Online]. Available: https://github.com/jennybc/repurrrsive

Disclaimer

The views, analysis and conclusions presented within this paper represent the author’s alone and not of any other person, organization or government entity. While I have made every reasonable effort to ensure that the information in this article was correct, it will nonetheless contain errors, inaccuracies and inconsistencies. It is a working paper subject to revision without notice as additional information becomes available. Any liability is disclaimed as to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause. The author(s) received no financial support for the research, authorship, and/or publication of this article.

Reproducibility

─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.1.3 (2022-03-10)
 os       macOS Big Sur/Monterey 10.16
 system   x86_64, darwin17.0
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Chicago
 date     2022-04-21
 pandoc   2.14.1 @ /usr/local/bin/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 assertthat    0.2.1      2019-03-21 [1] CRAN (R 4.1.0)
 blogdown    * 1.9        2022-03-28 [1] CRAN (R 4.1.2)
 bookdown      0.25       2022-03-16 [1] CRAN (R 4.1.2)
 brio          1.1.3      2021-11-30 [1] CRAN (R 4.1.0)
 bslib         0.3.1.9000 2022-03-04 [1] Github (rstudio/bslib@888fbe0)
 cachem        1.0.6      2021-08-19 [1] CRAN (R 4.1.0)
 callr         3.7.0      2021-04-20 [1] CRAN (R 4.1.0)
 cli           3.2.0      2022-02-14 [1] CRAN (R 4.1.2)
 codetools     0.2-18     2020-11-04 [1] CRAN (R 4.1.3)
 colorspace    2.0-3      2022-02-21 [1] CRAN (R 4.1.2)
 crayon        1.5.1      2022-03-26 [1] CRAN (R 4.1.0)
 DBI           1.1.2      2021-12-20 [1] CRAN (R 4.1.0)
 desc          1.4.1      2022-03-06 [1] CRAN (R 4.1.2)
 devtools    * 2.4.3      2021-11-30 [1] CRAN (R 4.1.0)
 digest        0.6.29     2021-12-01 [1] CRAN (R 4.1.0)
 dplyr         1.0.8      2022-02-08 [1] CRAN (R 4.1.2)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.1.0)
 evaluate      0.15       2022-02-18 [1] CRAN (R 4.1.2)
 fansi         1.0.3      2022-03-24 [1] CRAN (R 4.1.2)
 fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.1.0)
 fs            1.5.2      2021-12-08 [1] CRAN (R 4.1.0)
 generics      0.1.2      2022-01-31 [1] CRAN (R 4.1.2)
 ggplot2     * 3.3.5      2021-06-25 [1] CRAN (R 4.1.0)
 ggthemes    * 4.2.4      2021-01-20 [1] CRAN (R 4.1.0)
 glue          1.6.2      2022-02-24 [1] CRAN (R 4.1.2)
 gtable        0.3.0      2019-03-25 [1] CRAN (R 4.1.0)
 highr         0.9        2021-04-16 [1] CRAN (R 4.1.0)
 htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.0)
 htmlwidgets   1.5.4      2021-09-08 [1] CRAN (R 4.1.0)
 jquerylib     0.1.4      2021-04-26 [1] CRAN (R 4.1.0)
 jsonlite      1.8.0      2022-02-22 [1] CRAN (R 4.1.2)
 knitr         1.38       2022-03-25 [1] CRAN (R 4.1.0)
 lifecycle     1.0.1      2021-09-24 [1] CRAN (R 4.1.0)
 listviewer  * 3.0.0      2019-11-02 [1] CRAN (R 4.1.0)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.1.2)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.1.0)
 munsell       0.5.0.9000 2021-10-19 [1] Github (cwickham/munsell@e539541)
 pillar        1.7.0      2022-02-01 [1] CRAN (R 4.1.2)
 pkgbuild      1.3.1      2021-12-20 [1] CRAN (R 4.1.0)
 pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.1.0)
 pkgload       1.2.4      2021-11-30 [1] CRAN (R 4.1.0)
 png           0.1-7      2013-12-03 [1] CRAN (R 4.1.0)
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.1.0)
 processx      3.5.3      2022-03-25 [1] CRAN (R 4.1.0)
 ps            1.6.0      2021-02-28 [1] CRAN (R 4.1.0)
 purrr         0.3.4      2020-04-17 [1] CRAN (R 4.1.0)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.0)
 remotes       2.4.2      2021-11-30 [1] CRAN (R 4.1.0)
 repurrrsive * 1.0.0      2019-07-15 [1] CRAN (R 4.1.0)
 rlang         1.0.2      2022-03-04 [1] CRAN (R 4.1.2)
 rmarkdown     2.13       2022-03-10 [1] CRAN (R 4.1.2)
 rprojroot     2.0.3      2022-04-02 [1] CRAN (R 4.1.0)
 rstudioapi    0.13       2020-11-12 [1] CRAN (R 4.1.0)
 sass          0.4.1      2022-03-23 [1] CRAN (R 4.1.2)
 scales        1.1.1      2020-05-11 [1] CRAN (R 4.1.0)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.1.0)
 stringi       1.7.6      2021-11-29 [1] CRAN (R 4.1.0)
 stringr       1.4.0      2019-02-10 [1] CRAN (R 4.1.0)
 testthat      3.1.3      2022-03-29 [1] CRAN (R 4.1.2)
 tibble        3.1.6      2021-11-07 [1] CRAN (R 4.1.0)
 tidyselect    1.1.2      2022-02-21 [1] CRAN (R 4.1.2)
 usethis     * 2.1.5      2021-12-09 [1] CRAN (R 4.1.0)
 utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)
 vctrs         0.4.0      2022-03-30 [1] CRAN (R 4.1.2)
 withr         2.5.0      2022-03-03 [1] CRAN (R 4.1.0)
 xfun          0.30       2022-03-02 [1] CRAN (R 4.1.2)
 yaml          2.3.5      2022-02-21 [1] CRAN (R 4.1.2)

 [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────