Shiny模块内shinyanimate::startAnim()无法正常工作求助
解决shinyanimate在Shiny模块中无法生效的问题
问题根源是模块内调用startAnim时,传入的元素ID没有经过命名空间处理。Shiny模块中的DOM元素实际ID是模块ID-元素ID的格式(比如你的button_mod实际ID为test-button_mod),但你直接传入'button_mod',导致shinyanimate无法定位到目标元素。
只需在模块server函数中,给startAnim的id参数加上ns()处理即可:
if (interactive()) { mod_test_ui <- function(id){ ns <- NS(id) tagList(actionButton(ns("button_mod"), label = "button_mod")) } mod_test_server <- function(id){ moduleServer( id, function(input, output, session){ ns <- session$ns observeEvent(input$button_mod,{ # 关键修改:用ns()处理元素ID shinyanimate::startAnim(session, id=ns('button_mod'), 'bounce') }) }) } shinyApp( ui = basicPage( shinyanimate::withAnim(), mod_test_ui("test"), actionButton("button", label = "button") ), server = function(input, output, session) { mod_test_server("test") observeEvent(input$button,{ shinyanimate::startAnim(session, id='button', 'bounce') }) } ) }
原理说明:
- 模块UI中通过
ns("button_mod")生成了带命名空间的元素ID,因此server端必须使用ns("button_mod")来匹配这个实际DOM ID - 主应用中的按钮不属于任何模块,无需命名空间处理,直接使用原始ID即可正常生效
内容的提问来源于stack exchange,提问作者Oeilbrun
相关产品推荐
相关产品推荐

