JohnCoene / waiter

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

Using waiter for other render elements than plot? #66

Closed shahreyar-abeer closed 4 years ago

shahreyar-abeer commented 4 years ago

Hi John,

Nice work you have going on there. Truly appreciate it.

I was thinking, can we use {waiter} for other elements other than renderPlot(). So for example, I have am trying to do the following.


library(shiny)
library(waiter)

ui <- fluidPage(
  use_waiter(),
  actionButton(
    inputId = "go",
    label = "GO"
  ),
  uiOutput("text")
)

server <- function(input, output) {
  w = Waiter$new(id = "text")

  some_text <- reactive({
    w$show()
    Sys.sleep(3)
    h3("{waiter} is polished!")
  })

  observeEvent(input$go, {
    output$text <- renderUI({
      some_text()
    })
  })
}

if (interactive()) 
  shinyApp(ui, server)

I see that this doesn't work.

JohnCoene commented 4 years ago

Thank you.

Yes you can use {waiter} on any render* and it should work, the reason it does not show in your case is for two reasons related to the fact that internally waiter checks that the element you want to overly is 50 pixels.

When waiter is first triggered uiOutput("text") is just empty and has a height of 0px (so even without waiter sanitising it still would not show), the second time it runs the uiOutput("text") is populated with the <h3> but is still not over 50 pixels.

Simplifying your code somewhat we can force the height to be over 50 pixels:

library(shiny)
library(waiter)

ui <- fluidPage(
  use_waiter(),
  actionButton(
    inputId = "go",
    label = "GO"
  ),
  uiOutput("text",  style="height:51px")
)

server <- function(input, output) {
  w <- Waiter$new(id = "text")

  observeEvent(input$go, {
    output$text <- renderUI({
      w$show()
      Sys.sleep(3)
      h3("{waiter} is polished!")
    })
  })
}

shinyApp(ui, server)
JohnCoene commented 4 years ago

Let me know if this answers your question

shahreyar-abeer commented 4 years ago

Alhamdulillah.

This answers my question exactly to the point. I wonder though, why the choice of 50 pixels?

JohnCoene commented 4 years ago

Because 1) otherwise it looks really odd 2) most spinners are around 48px high.

shahreyar-abeer commented 4 years ago

I see. Thanks for that.

Keep up the great work.