0

Summary: When I put a shinyTree (from the R package of the same name) in a dropdownButton (from the shinyWidgets package), it seems that the tree in question is not accessible from the input variable until the user has interacted with the tree in some way. Is there a way I can force the tree to load when the app is loaded at the start?

Here is a minimal (almost) example:

library(shiny)
library(shinyWidgets)
library(shinyTree)
library(magrittr)

render.as.text <- function(t) {
  if (is.null(t)) {
    'argument is NULL'
  } else {
    q <- get_selected(t)
    if (length(q) == 0) {
      'none selected'
    } else {
      paste(q, sep=', ', collapse=', ') 
    }
  }
}

ui <- fluidPage(
  dropdownButton(shinyTree('tree', checkbox=TRUE), circle=FALSE, inputId='dd')
)

server <- function(input, output, session) {
  output$tree <- renderTree({ list(OptionA = structure(list(OptionAA='', OptionAB=''), stopened=TRUE), OptionB='') })
  updateActionButton(session, 'dd', label='initial label')
  
  observe({
    updateActionButton(session, 'dd', label=render.as.text(input$tree))
  }) %>% bindEvent(input$dd_state)  
}

shinyApp(ui = ui, server = server)

When I run this app, the button begins with the text 'initial label' (as expected), then when I click on it the first time the text changes to 'argument is NULL' (showing that the variable input$tree has a NULL value), but the tree displays correctly in the dropdown. If I then click the button again to close the dropdown, the text 'argument is NULL' remains - it remains until I actually select one or more of the tree's checkboxes after which (and after clicking the button again) it will display either a comma-separated list of selected options or 'none selected' as expected.

I would very much like to be able to refer to input$tree beginning with the first time the user takes any action in the app - ideally I am looking for some command to insert in either the server or ui code (or both) which will mean that the first time the user clicks the button its text will change directly to 'none selected' because input$tree is no longer NULL. Is this possible?

Yiab
  • 21
  • 1

1 Answers1

1

Would just changing the default of your render.as.text function work for your purpose?

library(shiny)
library(shinyWidgets)
library(shinyTree)
library(magrittr)

render.as.text <- function(t) {
  if (is.null(t)) {
    'none selected'
  } else {
    q <- get_selected(t)
    if (length(q) == 0) {
      'none selected'
    } else {
      paste(q, sep=', ', collapse=', ') 
    }
  }
}

ui <- fluidPage(
  dropdownButton(shinyTree('tree', checkbox=TRUE), circle=FALSE, inputId='dd')
)

server <- function(input, output, session) {
  output$tree <- renderTree({ list(OptionA = structure(list(OptionAA='', OptionAB=''), stopened=TRUE), OptionB='') })
  updateActionButton(session, 'dd', label='initial label')
  
  observe({
    updateActionButton(session, 'dd', label=render.as.text(input$tree))
  }) %>% bindEvent(input$dd_state)  
}

shinyApp(ui = ui, server = server)
Lennyy
  • 5,932
  • 2
  • 10
  • 23
  • No, it would not. The real situation feeds the selected options of the tree into a data frame and then to ggplot2, without an actual input tree there my only option is to just give the user an error message until they go through and select something from the tree (actually, each of four trees). – Yiab Mar 22 '22 at 01:07
  • To be clear what I would like to happen is if the user tries to render the ggplot without making a selection from every category, default options are selected for them and the ggplot renders anyway. I can do that if the trees exist, but I can't really do that if they are NULL (although I can fake it to a degree by rendering the graph with default options but not actually selecting those default options in the trees). – Yiab Mar 22 '22 at 02:26