I have a column where each row element is serpearted by "/":
data.frame(column=c("a","a/air","a/aero/breath","b","b/boy","b/bag/band/brand"))
How can I convert it into nested lists after each "/". So the aim is to get:
list(a=list("air"=1,aero=list("breath"=1)),b=list("boy"=1,bag=list(band=list("brand"=1))))
I need this for the shinyTree package to make a tree out of the column.
I have added the "=1" at the end of the last elements in the hierarchy as it is required to show up in the shinyTree output. The list can then be put in the code below to get the tree
:
library(shiny)
library(shinyTree)
tree <- list(a=list("air"=1,aero=list("breath"=1)),b=list("boy"=1,bag=list(band=list("brand"=1))))
typeof(tree)
ui <- fluidPage(
fluidPage(
sidebarLayout(
sidebarPanel(
actionButton('reset', 'Reset nodes')
),
mainPanel(
shinyTree("tree", ),
hr(),
"Selected nodes:",
verbatimTextOutput("idSelected")#,
)
)
)
)
server <- function(input, output, session) {
treeSelection <- reactiveVal(list())
output$tree = renderTree({
tree
})
observeEvent(input$reset, {
updateTree(session, "tree", data = tree)
treeSelection(list())
})
observeEvent(input$tree, {
treeSelection(get_selected(input$tree, format = "classid"))
})
output$idSelected <- renderPrint({
treeSelection()
})
}
shinyApp(ui, server)