I've seen many similar questions, but couldn't adapt to my situation. I have data that comes as a nested list, and want to convert it to a data frame in a certain way.
my_data_object <-
list(my_variables = list(
age = list(
type = "numeric",
originType = "slider",
originSettings = structure(list(), .Names = character(0)),
originIndex = 5L,
title = "what is your age?",
valueDescriptions = NULL
),
med_field = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 6L,
title = "what medical branch are you at?",
valueDescriptions = list(card = "Cardiology", ophth = "Ophthalmology",
derm = "Dermatology")
),
covid_vaccine = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 8L,
title = "when do you plan to get vaccinated?",
valueDescriptions = list(
next_mo = "No later than next month",
within_six_mo = "No later than six months from now",
never = "I will not get vaccinated"
)
)
))
Desired Output
var_name type originType title
<chr> <chr> <chr> <chr>
1 age numeric slider what is your age?
2 med_field string choice what medical branch are you at?
3 covid_vaccine string choice when do you plan to get vaccinated?
My unsuccessful attempt
library(tibble)
library(tidyr)
my_data_object %>%
enframe() %>%
unnest_longer(value) %>%
unnest(value)
## # A tibble: 18 x 3
## name value value_id
## <chr> <named list> <chr>
## 1 my_variables <chr [1]> age
## 2 my_variables <chr [1]> age
## 3 my_variables <named list [0]> age
## 4 my_variables <int [1]> age
## 5 my_variables <chr [1]> age
## 6 my_variables <NULL> age
## 7 my_variables <chr [1]> med_field
## 8 my_variables <chr [1]> med_field
## 9 my_variables <named list [0]> med_field
## 10 my_variables <int [1]> med_field
## 11 my_variables <chr [1]> med_field
## 12 my_variables <named list [3]> med_field
## 13 my_variables <chr [1]> covid_vaccine
## 14 my_variables <chr [1]> covid_vaccine
## 15 my_variables <named list [0]> covid_vaccine
## 16 my_variables <int [1]> covid_vaccine
## 17 my_variables <chr [1]> covid_vaccine
## 18 my_variables <named list [3]> covid_vaccine
I'm trying to get this using tidyverse functions, but so far it seems I'm not headed the right direction. I Will be grateful for guidance.
EDIT
Unlike the example data I provided originally, in reality my data comes in a bit different hierarchy. I thought this would be simple to generalize once I have the method but turns it's not. So if we consider that data comes such as the following, but truly I only care about the my_variables sub-list.
my_data_object_2 <-
list(
other_variables = list(
whatever_var_1 = list(
type = "numeric",
originType = "slider",
originSettings = structure(list(), .Names = character(0)),
originIndex = 5L,
title = "blah question",
valueDescriptions = NULL
)
),
my_variables = list(
age = list(
type = "numeric",
originType = "slider",
originSettings = structure(list(), .Names = character(0)),
originIndex = 5L,
title = "what is your age?",
valueDescriptions = NULL
),
med_field = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 6L,
title = "what medical branch are you at?",
valueDescriptions = list(card = "Cardiology", ophth = "Ophthalmology",
derm = "Dermatology")
),
covid_vaccine = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 8L,
title = "when do you plan to get vaccinated?",
valueDescriptions = list(
next_mo = "No later than next month",
within_six_mo = "No later than six months from now",
never = "I will not get vaccinated"
)
)
)
)
So how could I "zoom in"/"extract" my_variables and only then get the table I specified in "Desired Output" above?