README.md

drop

Lifecycle:
experimental CRAN
status

Installation

You can install the development version of from GitHub with:

# install.packages("remotes")
remotes::install_github("rappster/drop")

What?

Drop, i.e. remove, certain values in (nested) list structures.

Experimental and scope is “scratch your own itch”.

Why?

Because I always seem to need it at some point in various other projects and always struggled to find/remember previous implementations that worked.

How?

By leveraging rrapply (see SO post)

library(drop)

Drop NULL values

x <- list(
    foo = NULL,
    bar = 1,
    baz = TRUE,
    foobar = "a"
)

x %>% drop_null()
#> $bar
#> [1] 1
#> 
#> $baz
#> [1] TRUE
#> 
#> $foobar
#> [1] "a"
x <- list(
    list(foo = NULL, bar = 1)
)

x %>% drop_null()
#> [[1]]
#> [[1]]$bar
#> [1] 1
x <- list(
    foo = list(bar = NULL, baz = list(x = 1, y = NULL, z = 3)),
    baz = 2
)

x %>% drop_null()
#> $foo
#> $foo$baz
#> $foo$baz$x
#> [1] 1
#> 
#> $foo$baz$z
#> [1] 3
#> 
#> 
#> 
#> $baz
#> [1] 2

Empty input is simply returned

list() %>% drop_null()
#> list()

Drop empty values

x <- list(
    foo = integer(),
    bar = 1,
    baz = TRUE,
    foobar = "a"
)

x %>% drop_empty()
#> $bar
#> [1] 1
#> 
#> $baz
#> [1] TRUE
#> 
#> $foobar
#> [1] "a"
x <- list(
    list(
        foo = character(), bar = 1)
)

x %>% drop_empty()
#> [[1]]
#> [[1]]$bar
#> [1] 1
x <- list(
    foo = list(bar = logical(), baz = list(x = 1, y = numeric(), z = 3)),
    baz = 2
)

x %>% drop_empty()
#> $foo
#> $foo$baz
#> $foo$baz$x
#> [1] 1
#> 
#> $foo$baz$z
#> [1] 3
#> 
#> 
#> 
#> $baz
#> [1] 2

Empty input is simply returned

list() %>% drop_empty()
#> list()


rappster/drop documentation built on May 16, 2022, 12:25 a.m.