rlist

repository·master·Indexed 41 days ago

https://github.com/renkun-ken/rlist

A toolbox for working with non-tabular, non-relational data stored in R list objects. It provides pipeline-friendly functions such as list.filter(), list.select(), list.map(), and list.stack() for manipulating complex nested structures. The package supports lambda expressions and a fluent API via the List() environment for method chaining using the $ operator.

Tokens
1.9K
Snippets
12
Records
12
Agent score
50%

What's inside rlist

  1. Use lambda expressions in rlist functions

    master

    Most functions in rlist that accept expressions support several forms of lambda expressions using the ~ syntax. This allows for more complex logic using the element (x), its index (i), or its name (name).

    Supported Forms:

    • Implicit: expression (uses default symbols ., .i, and .name)
    • Univariate: x ~ expression or f(x) ~ expression (where x is the list member)
    • Multivariate: f(x,i) ~ expression or f(x,i,name) ~ expression (where i is the index and name is the name)

    Examples:

    # Using implicit lambda with . for the element
    list.map(nums, c(min=min(.), max=max(.)))
    
    # Using univariate lambda with x
    list.filter(nums, x ~ mean(x) >= 3)
    
    # Using multivariate lambda with x and i
    list.map(nums, f(x,i) ~ sum(x,i))
    list.map(nums, c(min=min(.),max=max(.)))
    list.filter(nums, x ~ mean(x)>=3)
    list.map(nums, f(x,i) ~ sum(x,i))
  2. Use the List() environment for fluent API access

    master

    The List() function wraps a list and all rlist functions into a single object. This allows you to access functions as methods using the $ operator, creating a highly fluent interface.

    # Wrap the list in a List environment
    ldevs <- List(devs)
    
    # Access methods via $
    ldevs$filter("music" %in% interest & "r" %in% names(lang))$ 
      select(name, age)$ 
      stack()$ 
      data
  3. Chain operations using the native pipe operator

    master

    Because rlist functions are designed to be pipe-friendly, you can chain multiple operations using the R native pipe operator |>. This allows for a fluent style of querying non-relational list data.

    devs |>
      list.filter("music" %in% interest & "r" %in% names(lang)) |>
      list.select(name, age) |>
      list.stack()
  4. Chain rlist operations using the pipe operator

    master

    Since most rlist functions are designed to be pipeline-friendly, you can chain them using the R native pipe operator |>. This allows for fluent data processing workflows.

    Example: Filtering developers, selecting specific fields, and stacking the result into a data frame:

    devs |> 
      list.filter("music" %in% interest & "r" %in% names(lang)) |> 
      list.select(name, age) |> 
      list.stack()
    devs |> 
      list.filter("music" %in% interest & "r" %in% names(lang)) |> 
      list.select(name,age) |> 
      list.stack()
  5. Chain operations using Pipe syntax

    master

    The rlist functions are designed to be "pipe-friendly." You can use the R native pipe |> to chain multiple operations like filtering, selecting, and stacking into a single fluent workflow.

    # Example: Filter, select, and convert to data.frame using pipes
    devs |> 
      list.filter("music" %in% interest & "r" %in% names(lang)) |> 
      list.select(name, age) |> 
      list.stack()
  6. Use the List environment for method chaining

    master

    The List() function wraps a list within an environment where rlist functions are defined as methods. This allows you to call functions using the $ operator, similar to object-oriented programming.

    Example: Using the List environment to filter, select, and stack data:

    ldevs <- List(devs)
    ldevs$filter("music" %in% interest & "r" %in% names(lang))$ 
      select(name, age)$ 
      stack()$ 
      data
    ldevs <- List(devs)
    ldevs$filter("music" %in% interest & "r" %in% names(lang))$$
      select(name,age)$
      stack()$
      data
  7. Install rlist

    master

    You can install rlist from CRAN or the latest development version from GitHub.

    To install from CRAN:

    install.packages("rlist")

    To install the latest version from GitHub using devtools:

    devtools::install_github("renkun-ken/rlist")
    install.packages("rlist")
  8. Transform list elements with list.map()

    master

    Use list.map() to apply a transformation to every element in a list, returning a new list of the same length containing the results of the transformation.

    Example: Mapping each developer to the count of their interests:

    list.map(devs, length(interest))
    str( list.map(devs, length(interest)) )
  9. Use the List() environment for fluent syntax

    master

    The List() function wraps a list and its associated rlist functions into a single environment. This allows you to call methods using the $ operator, creating a highly fluent interface similar to object-oriented programming.

    ldevs <- List(devs)
    
    # Method chaining via $ operator
    result <- ldevs$filter("music" %in% interest & "r" %in% names(lang))$ 
      select(name, age)$ 
      stack()$ 
      data
  10. Select specific fields with list.select()

    master

    Use list.select() to extract a subset of fields from each element in a list. This is useful for reducing the complexity of non-tabular data.

    Example: Selecting only the name and age fields from a list of developers:

    list.select(devs, name, age)
    str( list.select(devs, name, age) )
  11. Filter list elements with list.filter()

    master

    Use list.filter() to subset a list based on a logical condition applied to its elements. The condition can use field names directly or lambda expressions.

    Example: Filtering a list of developers for those who like "music" and have used "R" for at least 3 years:

    list.filter(devs, "music" %in% interest & lang$r >= 3)
    str( list.filter(devs, "music" %in% interest & lang$r >= 3) )
  12. Core list manipulation functions: filter, select, and map

    master

    The rlist package provides high-order functions designed to work with non-tabular (non-data.frame) list objects. Most functions are "pipe-friendly."

    • list.filter(x, condition): Extracts elements from list x that satisfy the condition.
    • list.select(x, ...): Selects specific fields/elements from each item in list x.
    • list.map(x, expression): Transforms each element in list x using the provided expression or lambda.
    • list.stack(x): Converts a list of elements into a data.frame (stacking them).
    library(rlist)
    
    # Example data
    devs <- list(
      p1=list(name="Ken",age=24, interest=c("reading","music"), lang=list(r=2)),
      p2=list(name="James",age=25, interest=c("sports","music"), lang=list(r=3))
    )
    
    # Filter: developers who like music
    filtered <- list.filter(devs, "music" %in% interest)
    
    # Select: only names and ages
    selected <- list.select(devs, name, age)
    
    # Map: transform to length of interests
    mapped <- list.map(devs, length(interest))
    
    # Stack: convert list to data.frame
    df <- devs |> list.select(name, age) |> list.stack()