R shiny app : shinyanimate::startAnim() inside module not working

21 Views Asked by At

I'm new to the shinyanimate package. It works just fine outside modules, but inside modules it doesn't work (but no error nor warning). I'm pretty sure it has something to do with session and ns(), but I can't pinpoint the problem.

Here is my minimum example, with two buttons : the "button" lives in the app, the "button_mod" lives in the module.

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,{
        shinyanimate::startAnim(session, id='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')
      })
    }
  )
}

I already tried to pass "session" to the module.

I'm expecting the 2 buttons to have the same behaviour and joyfully bounce when clicked... Anyone has a suggestion ? Thanks

1

There are 1 best solutions below

2
Oeilbrun On

Ok nevermind I just had an epiphany. startAnim in a module needs an id like "mymodulename-myid" and not just "myid". Changing the id of the "button_mod" button to "test-button_mod" just does the trick.

New mod_test_server of my minimum example :

  mod_test_server <- function(id){
    moduleServer( id, function(input, output, session){
    ns <- session$ns
    observeEvent(input$button_mod,{
    shinyanimate::startAnim(session, id='test-button_mod', 'bounce')
  })
  })}

Anyway... Thanks for the shinyanimate package, and I hope my post would still be useful for struggling beginners !