Shiny中pickerinput值变更时如何给按钮添加触发式发光效果
解决方案
问题根源
你当前使用的无触发条件的observe()会在input$p1值变化时立刻执行,包括Shiny页面初始化阶段给pickerInput渲染默认选中值的场景,因此会触发初始发光效果。
修改方法
将原observe()替换为observeEvent(),同时添加ignoreInit = TRUE参数,跳过页面初始化阶段的首次触发,仅在用户主动修改pickerInput选中值时才触发发光逻辑,完全不影响pickerInput默认选中值的配置。
修改后的完整代码
library("shiny") library(shinyWidgets) library(shinyjs) ui <- tagList(shinyjs::useShinyjs(), fluidPage( tags$h2("Select / Deselect all"), actionButton("button_1", "Refresh Plot",style= "animation: none;"), pickerInput( inputId = "p1", label = "Select all option", choices = rownames(mtcars), multiple = TRUE, options = list(`actions-box` = TRUE), selected = rownames(mtcars)[1] )), tags$head(tags$style(HTML('@keyframes glowing { 0% { background-color: #4a257a !important; box-shadow: 0 0 5px #0795ab; } 50% { background-color: #6632ad !important; box-shadow: 0 0 20px #43b0d1; } 100% { background-color: #4a257a !important; box-shadow: 0 0 5px #0795ab; } } ')))) server <- function(input, output) { observeEvent(input$p1, { runjs('$("#button_1").css("animation","glowing 5000ms infinite")') delay(5000, runjs('$("#button_1").css("animation","none")')) print(input$p1) }, ignoreInit = TRUE) } shinyApp(ui, server)
内容的提问来源于stack exchange,提问作者Tanmay McGrath
相关产品推荐
相关产品推荐

