简体   繁体   English

替换observeEvent() 以在闪亮的应用程序中恢复自动功能

[英]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):'.我在下面有一个闪亮的应用程序,当我从第一个复选框组“当前选择:”中选择它们时,我使用操作actionbutton “更新”来更新最后一个复选框组“显示视觉效果的代码:”的选择。 The issue is that I want to get rid of the actionbutton and make the selection and the update automatiacally.问题是我想摆脱操作actionbutton并自动进行选择和更新。 I have used observeEvent() for that but Im not sure how to replace them in order to achieve the functionality described above.observeEvent()使用了observeEvent() ,但我不确定如何替换它们以实现上述功能。

#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)

Are you looking for something like this?你在寻找这样的东西吗? This example is drawn from the shiny help on UpdateCheckboxGroupInput .这个例子来自UpdateCheckboxGroupInput上的闪亮帮助。

The trick to aim for is using observe as your reactive environment to keep watch on the first checkbox group.目标的技巧是使用observe作为你的反应环境来监视第一个复选框组。

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
    )
 })
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在闪亮的应用程序中提高observeEvent的性能 - Improve performances of observeEvent in shiny app 在闪亮的应用程序中使用观察事件进行 numericInput - using observeEvent for numericInput in shiny app 应用程序启动时闪亮的observeEvent触发器 - Shiny observeEvent triggers on app launch 如何在 R Shiny 中用 reactiveVal 和 ObserveEvent 替换反应性 - How to replace reactive with reactiveVal and ObserveEvent in R Shiny 在R Shiny中,如何用observe替换observeEvent? - In R Shiny, how to replace observeEvent with observe? 在 Shiny 应用程序中使用 observeEvent 显示 plot - Showing plot using observeEvent in Shiny app 如何在 R Shiny 中用更全面的响应式 function 替换 observeEvent? - How to replace an observeEvent with a more comprehensive reactive function in R Shiny? ObserveEvent中的闪亮应用程序错误? [.default:错误的下标类型&#39;列表&#39;中的错误 - Shiny App Error in ObserveEvent? Error in [.default: invalid subscript type 'list' 使用一个 if() 而不是多个 observeEvent() 在闪亮的应用程序中显示消息 - Use one if() instead of multiple observeEvent() to display message in a shiny app 无法使用observeEvent和eventReactive更新闪亮应用程序中的输入 - Can't update inputs in shiny app with observeEvent and eventReactive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM