JohnCoene / waiter

🕰️ Loading screens for Shiny
https://waiter.john-coene.com/
Other
495 stars 25 forks source link

Waitress with Shiny modules #151

Open fricour opened 4 months ago

fricour commented 4 months ago

Hi,

Thank you for this great package that I am currently exploring !

I am not sure whether it's a bug or if I am doing something wrong but I did not find a way to use waitress with shiny modules.

Let's say I am using a simple app like this one (no modules)

library(shiny)
library(waiter)

ui <- fluidPage(
  useWaitress(),
  actionButton("btn", "render"),
  plotOutput("plot", width = 400)
)

server <- function(input, output){

  waitress <- Waitress$new("#btn", theme = "overlay-percent", infinite = TRUE)

  data <- eventReactive(input$btn,{

    # start waitress
    waitress$start()

    # do stuff
    Sys.sleep(3)
    tmp <- runif(100)

    # hide when done
    waitress$close() 

    return(tmp)
  })

  output$plot <- renderPlot({
    hist(data())
  })

}

shinyApp(ui, server)

It works well.

Now, if I am building the same app with modules

mod_ui <- function(id) {
  ns <- NS(id)

  tagList(
    useWaitress(),
    actionButton(ns("btn"), "render"),
    plotOutput(ns("plot"), width = 400)
  )
}

mod_server <- function(id){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns

    waitress <- Waitress$new(ns("#btn"), theme = "overlay-percent", infinite = TRUE)

    data <- eventReactive(input$btn,{

      # start waitress
      waitress$start()

      # do stuff
      Sys.sleep(3)
      tmp <- runif(100)

      # hide when done
      waitress$close()

      return(tmp)
    })

    output$plot <- renderPlot({
      hist(data())
    })

  })
}

# App #
ui <- fluidPage(
  mod_ui("test_ui")
)

server <- function(input, output, session) {
  mod_server("test_ui")
}

shinyApp(ui = ui, server = server)

The waitress does not appear in that case. I have tried with ns("#btn") and ns("btn") but without success. I feel like something is messing with the selector (for the javascript behind) and that it is lost somewhere with the modular approach.

Any thoughts? Thanks !

PaulJonasJost commented 1 month ago

Hey @fricour,

just another user here, but I recently tried paste0("#", ns("btn")), which worked for me, you could try that.