dreamRs / shinytreeview

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

Update selection in server.R (shiny) #8

Open castledan opened 2 years ago

castledan commented 2 years ago

Hi,

Is there a way to change the value of the tree input on the client? Something like what updateCheckboxGroupInput does for checkboxGroupInput. What I would like to include in my app is a "Reset selection" button, which reverts the selection of the nodes in the tree to the default one.

pvictor commented 2 years ago

If you re-install from GitHub you will have a updateTreeview function to update selected value.

To clear the selection use character(0), note that this doesn't work if you have set prevent_unselect option to TRUE.

Here's an example :

# updateTreeview ----------------------------------------------------------

library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
  tags$h3("Update label & selected value"),
  treeviewInput(
    inputId = "tree",
    label = "Choose a city:",
    choices = make_tree(cities, c("continent", "country", "city")),
    multiple = FALSE,
    prevent_unselect = TRUE
  ),
  verbatimTextOutput(outputId = "result"),
  textInput("label", "New label:", "Choose a city:"),
  radioButtons(
    "selected", "Selected:",
    choices = unique(c(cities$continent, cities$country, cities$city)),
    inline = TRUE
  )
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$tree
  })
  observe(updateTreeview(inputId = "tree", label = input$label))
  observe(updateTreeview(inputId = "tree", selected = input$selected))
}

if (interactive())
  shinyApp(ui, server)
castledan commented 2 years ago

Thank you very much, it works well! I'll take advantage one last time of your availability and responsiveness and post another issue on a different topic.

dmcalli2 commented 2 years ago

Thanks for sharing package. Looks great. Is it likely that you will add a "choices" argument to UpdateTreeview (as in the UpdateSelectInput)?