I am trying to use purr map function or its variants to map data across multiple functions ( in my case R-Shiny functions). I am reading the parameters from example.json
.
{
"Section_1": {
"MainHeader": [{
"School": "Montessori"
}],
"boxitems": [{
"tabName": "id1",
"box": [{
"title": "Students graph",
"custofun": ["Bob", "Dan", "Sean"]
},
{
"title": "Teacher graph",
"custofun": ["Robinson"]
}
]
},
{
"tabName": "id2",
"box": [{
"title": "Students graph",
"custofun": ["Felix", "Helix", "Alex"]
},
{
"title": "Teacher graph",
"custofun": ["Phelix"]
}
]
}
]
}
}
I read this into df
format
json <- fromJSON("example.json")
I am trying to generate the tabItem
's and its box
'es dynamically by wrapping them in map function, for example to just map on tabItem
values I can use map.
map(unique(df$id), ~ tabItem(tabName = .x))
This would generate the html code for tabItem
with all the list of ID's I have in the json file. For the json structure I have I need to traverse through ID -> list -> list
. And, pass the respective input parameters to tabItem
, box
Tried the other variants like pmap
but could not solve it. How to use purr map functions recursively in a data frame of this data structure?
Here is my attempt
json$Section_1$boxitems %>% as.tibble() # to check the strucutre
df <- json$Section_1$boxitems %>% select(tabName,box)
df$box <- setNames(df$box,df$tabName)
BoxCustomFunc <- function(tabName,box) {
map(tabName , ~ tabItem(tabName = .x),
map2(x = box, y = box[tabName],
box(title = .x$title,
column(width = 2, get(.y$custofun)(tabName)))
))
}
The current output below. What I get is the tabItem
, what is missing is the box and column html output. It seems the map2
does not even render.
[[1]]
<div role="tabpanel" class="tab-pane" id="shiny-tab-id1"></div>
[[2]]
<div role="tabpanel" class="tab-pane" id="shiny-tab-id2"></div>