find_element: Find Element in a Nested List with the Given Name

View source: R/helpers.R

find_elementR Documentation

Find Element in a Nested List with the Given Name

Description

Find the value of the element that contains the given string in its name in a nested list. Based off of advice here.

Usage

find_element(nested_list, search_for)

Arguments

nested_list

a nested list, such as the type returned by fromJSON

search_for

a character string that is the name of the element we want to find in nested_list

Value

any values of the list where the name contains the substring search_for

Examples

{
  # create list of data frames for example
  df_nest <- list(
    Y = data.frame(x.test = c(1, 2, 3)),
    Z = data.frame(w = c("a", "b", "c"))
  )

  # extract element containing 'test'
  find_element(df_nest, "test")

  # create nested list for example
  list_nest <- list(
    Y = c(1, 2, 3),
    z = list(
      x.test = c(1, 2, 3),
      y = c("no", "yes")
    )
  )
  find_element(list_nest, "test")
}

q-w-a/regulationsgov documentation built on May 3, 2022, 8:23 p.m.