Q: Extract values from list of arbitrary depth I have a messy, highly nested, list:
m <- list('form' = list('elements' = list('name' = 'Bob', 'code' = 12), 'name' = 'Mary', 'code' = 15))

> m
$form
$form$elements
$form$elements$name
[1] "Bob"

$form$elements$code
[1] 12


$form$name
[1] "Mary"

$form$code
[1] 15

How can I extract from the object m the name and code, regardless as to how nested name and code appears within a list?
Expected output:
# A tibble: 2 x 2
  name   code
  <chr> <dbl>
1 Bob      12
2 Mary     15


A: 1) rrapply Flatten m using rrapply giving r and then separate the name and code fields of unlist(r) using tapply, remove the dimensions using c, convert to data.frame and set the order of the columns.
Note that this is not hard coded to name and code and would work with other fields and numbers of fields.
library(rrapply)

r <- rrapply(m, f = c, how = "flatten")
nms <- names(r)
as.data.frame(c(tapply(unname(r), nms, unlist)))[unique(nms)]

giving:
  name code
1  Bob   12
2 Mary   15

An alternative to the final two lines of code above would be:
out <- unstack(stack(r))
out[] <- lapply(out, type.convert)

If there could be other fields in m in addition to name and code that we want ignored then use this in place of the statement that defines r above:
cond <- function(x, .xname) .xname %in% c("name", "code")
r <- rrapply(m, cond, c, how = "flatten")

2) Base R A base R solution is the following which unlists m, and then uses tapply as in (1) grouping by the suffixes of names(r).  Like (1) this is a general approach that is not hard coded to name and code.  Note that tools comes with R so it is part of Base R.
r <- unlist(m)
nms <- tools::file.ext(names(r))
as.data.frame(c(tapply(unname(r), nms, unlist)))[unique(nms)]


A: This could help formating the list into a dataframe and then reshaping it:
library(tidyverse)
#Process
y1 <- as.data.frame(lapply(m,unlist),stringsAsFactors = F)
y1$id <- rownames(y1)
rownames(y1)<-NULL
#Dplyr mutation
y1 %>% mutate(Var=ifelse(grepl('name',id,),'name',
                         ifelse(grepl('code',id),'code',NA))) %>%
  select(-id) %>% group_by(Var) %>%
  mutate(i=1:n())%>% pivot_wider(names_from = Var,values_from = form) %>%
  select(-i) %>% mutate(code=as.numeric(code))

Output:
# A tibble: 2 x 2
  name   code
  <chr> <dbl>
1 Bob      12
2 Mary     15

