How to use map_at properly in a nested list?

I have a nested list of lists that include data tables at the bottom level. My goal is to map a function to a single table to transform the data. I'm currently trying to use purrr:map_depth and purrr::map_at in conjunction to create the plot. The reason I need to use map_at or map_if is the plot function I'm using takes different arguments depending on the table.

example below

library(data.table)
library(purrr)

example = list(
  group1 = list(
    all = data.table(
      x = 1:10,
      y = 10:1),
    not_all = data.table(
      x2 = 11:20,
      y2 = 20:11)
    ),
  group2 = list(
    all = data.table(
      x = 1:10,
      y = 10:1),
    not_all = data.table(
      x2 = 11:20,
      y2 = 20:11)
  ),
  group3 = list(
    all = data.table(
      x = 1:10,
      y = 10:1),
    not_all = data.table(
      x2 = 11:20,
      y2 = 20:11)
  ),
  group4 = list(
    all = data.table(
      x = 1:10,
      y = 10:1),
    not_all = data.table(
      x2 = 11:20,
      y2 = 20:11)
  )
)

I'm planning to use highcharter::data_to_boxplot as the mapping function.

So far I've been unable to extract a single table to map to and don't have a solid grasp of the purrr syntax yet.

map_depth(example, 2, map_at(., "all", data_to_boxplot, variable = x))
# Error: character indexing requires a named object

map_depth(example, 2, ~map_at(., "all", data_to_boxplot, variable = x))
# this prints out the entire list

# would like to try something like this too but can't figure out the piping correctly
map_depth(example, 2) %>%
  map_at(., "all", data_to_boxplot, variable = x)

# Error in as_mapper(.f, ...) : argument ".f" is missing, with no default

Any help would be greatly appreciated!



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)