[英]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.