Search code examples
rshinyshinywidgets

Replace observeEvent() to restore automatic functionality in a shiny app


I have the shiny app below in which I use the actionbutton 'Update' to update the choices of the last checkbox group 'Display Tickers for Visuals:' when I select them from the 1st checkbox group 'Current Selection(s):'. The issue is that I want to get rid of the actionbutton and make the selection and the update automatiacally. I have used observeEvent() for that but Im not sure how to replace them in order to achieve the functionality described above.

#app
library(shiny)
library(shinyWidgets)

server <- function(input, output,session) {
  ## Pickerbox Ticker Selection ----
  output$str <- renderUI({
    pickerInput(
      inputId = "strategy",
      label = HTML("<p><span style='color: black'>Select Strategies (Max of 8):</span></p>"),
      width = "100%",
      choices =c("VOO","VO", "VIOO","df","fg"),
      selected = c("VOO","VO", "VIOO"),
      multiple = TRUE
      )
  })


  ## Checkbox Inputs ---- 

  #selected tickers
  output$check_group_buttons <- renderUI({
    prettyCheckboxGroup(
      inputId = "checked_tickers",
      selected = input$strategy[1:length(input$strategy)],
      label = HTML("<p><span style='color: black'>Current Selection(s):</span></p>"),
      choices = input$strategy[1:length(input$strategy)],
      #direction = "vertical",
      inline = TRUE,
      icon = icon("check-square-o"),
      status = "success",
      outline = TRUE,
      animation = "pulse"
    )
  })

  #display tickers
  output$check_group_buttons2 <- renderUI({
    prettyCheckboxGroup(
      inputId = "checked_tickers_to_chart",
      label = HTML("<p><span style='color: black'>Display Tickers for Visuals:</span></p>"),
      selected = input$strategy[1:length(input$strategy)],
      choices =  input$strategy[1:length(input$strategy)],
      inline = TRUE,
      icon = icon("check-square-o"),
      status = "success",
      outline = TRUE,
      animation = "pulse"
    )
  })


  ## Display Tickers ----
  observeEvent(input$update_tickers_for_picker,{
    updatePrettyCheckboxGroup(session = session,
                              inputId = "checked_tickers_to_chart",
                              choices = input$strategy,
                              selected = input$strategy,
                              prettyOptions = list(
                                icon = icon("check-square-o"),
                                status = "success",
                                outline = TRUE,
                                animation = "jelly")
    )

  })
  #Managed update of universe selections
  updated_tickers_for_picker <- eventReactive(input$update_tickers_for_picker,{
    input$checked_tickers
  })

  observeEvent(input$update_tickers_for_picker,{
    updatePickerInput(session = session,
                      inputId = "strategy",
                      choices = c("VOO","VO", "VIOO","df","fg"),
                      selected = updated_tickers_for_picker()
    )

  })
}

ui <- fluidPage(
  sidebarPanel(
    uiOutput("str", width = 6),

    uiOutput("check_group_buttons"),
    actionButton(inputId = "update_tickers_for_picker",
                 label = "Update",icon = icon("play-circle"),
                 style="color: #fff; background-color: #198dc7; border-color: #222d32; border-radius: 70px"),
    uiOutput("check_group_buttons2")
  ),

          mainPanel(

          ))

shinyApp(ui = ui, server = server)

Solution

  • Are you looking for something like this? This example is drawn from the shiny help on UpdateCheckboxGroupInput.

    The trick to aim for is using observe as your reactive environment to keep watch on the first checkbox group.

    Put this in your server, near the end.

    ...
     #Managed update of universe selections
     observe({
        updated_tickers_for_picker <- input$checked_tickers
    
        if (is.null(updated_tickers_for_picker))
          updated_tickers_for_picker <- character(0)
    
        updatePrettyCheckboxGroup(session = session,
                                 inputId = "checked_tickers_to_chart",
                                 choices = updated_tickers_for_picker,
                                 selected = updated_tickers_for_picker,
                                 prettyOptions = list(
                                   icon = icon("check-square-o"),
                                   status = "success",
                                   outline = TRUE,
                                   animation = "jelly"),
                                 inline= TRUE
        )
     })
    ...