shinytreeview icon indicating copy to clipboard operation
shinytreeview copied to clipboard

Update selection in server.R (shiny)

Open castledan opened this issue 3 years ago • 3 comments

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.

castledan avatar Sep 07 '22 12:09 castledan

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)

pvictor avatar Sep 08 '22 09:09 pvictor

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.

castledan avatar Sep 12 '22 17:09 castledan

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

dmcalli2 avatar Sep 16 '22 13:09 dmcalli2