dreamRs / shinytreeview

Hierarchical tree input for Shiny apps
GNU General Public License v3.0
41 stars 8 forks source link

Same-named parent & child nodes: Parent node still selectable / not unselectable after setting selectable = FALSE and multiple = TRUE #14

Closed philiph99 closed 1 year ago

philiph99 commented 1 year ago
          I now experienced another issue and created a reproducible example (see below) using the cities dataset. 

After making the first two nodes (continent & country) unselectable, they are now still selected and in the app, I can not unselect them.

What can I do so that in the example below, again only the cities are selectable?

Thank you in advance!

library(shiny)
library(shinytreeview)

my_cities <- rbind(
  shinytreeview::cities, 
  data.frame(continent = "Luxemburg", 
             country = "Luxemburg",
             city = "Luxemburg")
)

args <- list(
  data = my_cities, 
  levels = c("continent", "country", "city")
)

args[names(my_cities)[1:2]] <- list(list(selectable = FALSE))
tree <- do.call(make_tree, args)

ui <- fluidPage(
  treeviewInput(inputId = "tree", label = "Tree", 
                choices = tree, selected = "Luxemburg", 
                multiple = T),
  verbatimTextOutput("tree_output")
)

server <- function(input, output, session) {

  output$tree_output <- renderText({
    input$tree
  })

}

shinyApp(ui, server)

Originally posted by @philiph99 in https://github.com/dreamRs/shinytreeview/issues/11#issuecomment-1564403057

philiph99 commented 1 year ago

Noticed that this issue resolves as soon as I use the following data frame and make "Antarctica" and "Luxemburg" clickable manually:

my_cities <- rbind(
  shinytreeview::cities, 
  data.frame(continent = "Luxemburg", 
             country = NA,
             city = NA)
)

args[names(my_cities)[1:2]] <- list(list(selectable = FALSE))
tree <- do.call(make_tree, args)

tree[[5]]$selectable <- NULL # Antarctica
tree[[4]]$selectable <- NULL # Luxemburg

Full code:

library(shiny)
library(shinytreeview)

my_cities <- rbind(
  shinytreeview::cities, 
  data.frame(continent = "Luxemburg", 
             country = NA,
             city = NA)
)

args <- list(
  data = my_cities, 
  levels = c("continent", "country", "city")
)

args[names(my_cities)[1:2]] <- list(list(selectable = FALSE))
tree <- do.call(make_tree, args)

tree[[5]]$selectable <- NULL
tree[[4]]$selectable <- NULL

ui <- fluidPage(
  treeviewInput(inputId = "tree", label = "Tree", 
                choices = tree, selected = "Luxemburg", 
                multiple = T),
  verbatimTextOutput("tree_output")
)

server <- function(input, output, session) {

  output$tree_output <- renderText({
    input$tree
  })

}

shinyApp(ui, server)