dreamRs / shinytreeview

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

Default selection does not work with treecheckInput #7

Closed castledan closed 1 year ago

castledan commented 1 year ago

Hi,

Thank you for this package, it's very useful.

I do not manage to select nodes by default when using treecheckInput. When running the code below, no nodes get selected in my shiny app. If I use treeviewInput instead, with the same code, all main nodes get selected.

Any idea?

library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
  treecheckInput(
    inputId = "tree",
    choices = make_tree(
      cities, 
      c("continent", "country", "city"),
      selected = cities$continent
    )
  ),
  verbatimTextOutput(outputId = "result")
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$tree
  })
}

if (interactive())
  shinyApp(ui, server)
pvictor commented 1 year ago

Hello, Thanks for reporting this issue. Should be fixed if you re-install from GitHub:

remotes::install_github("dreamRs/shinytreeview")

One small change with your example: selected = argument must be used in treecheckInput() not in make_tree(), e.g. :

library(shiny)
library(shinytreeview)

data("cities")

ui <- fluidPage(
  treecheckInput(
    inputId = "tree",
    choices = make_tree(
      cities, 
      c("continent", "country", "city")
    ),
    selected = "Africa"
  ),
  verbatimTextOutput(outputId = "result")
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$tree
  })
}

if (interactive())
  shinyApp(ui, server)

Victor

castledan commented 1 year ago

Hi Victor,

Thank you for your reply. The selection works now.

However, I just found something else. If you run the code snippet below, the node (New continent) does not get selected. After some trials with special characters, I found out this has to do with the use of the brackets (. Can this be fixed?

Also, is there a way to change the value of the tree input on the client? Something like what updateCheckboxGroupInput does for checkboxGroupInput.

library(shiny)
library(shinytreeview)

data("cities")
cities <- rbind(cities, c('(New continent)', 'New country', 'New city'))

ui <- fluidPage(
  treecheckInput(
    inputId = "tree",
    choices = make_tree(
      cities, 
      c("continent", "country", "city")
    ),
    selected = cities$continent
  ),
  verbatimTextOutput(outputId = "result")
)

server <- function(input, output, session) {
  output$result <- renderPrint({
    input$tree
  })
}

if (interactive())
  shinyApp(ui, server)
pvictor commented 1 year ago

Ah yes, actually it search a reg exp with the name of the node, I have escaped special characters, should work now.

castledan commented 1 year ago

Thank you! I posted the other question as a new issue, as I figured it was off-topic here.

pvictor commented 1 year ago

Perfect, thank you, I will take a look later