JohnCoene / waiter

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

Waiter not working with reactivePoll at valueFunc #146

Closed HugoGit39 closed 7 months ago

HugoGit39 commented 7 months ago

Hi

I dont know wether this is a real bug, however i would like to show a wating screen when data is generated and renewed using reactivePoll...but this doesnt work. Any idea why?

# Load required libraries
library(shiny)
library(ggplot2)
library(waiter)

# Function to generate random data
generate_data <- function() {
  data <- data.frame(
    x = rnorm(50),
    y = rnorm(50)
  )
  return(data)
}

# UI
ui <- fluidPage(
  titlePanel("ReactivePoll Example"),
  plotOutput("scatterplot")
)

# Server
server <- function(input, output, session) {

  data <- reactivePoll(
    intervalMillis = 10000,
    session = session,
    checkFunc = function() {
      # Return a timestamp to trigger polling

      print(format(Sys.time(), "%M"))

    },
    valueFunc = function() {

      waiter_show()

      generate_data()

      waiter_hide()

    }

  )

  # Scatterplot
  output$scatterplot <- renderPlot({
    ggplot(data(), aes(x = x, y = y)) +
      geom_point() +
      labs(title = "Random Scatterplot")
  })
}

# Run the application
shinyApp(ui, server)
JohnCoene commented 7 months ago

It looks to me like generate_data runs instantaneously.

What are you trying to achieve?

HugoGit39 commented 7 months ago

Yeah correct, this is a simplified example.

My current app is huge where data is being loaded from Google Drive. This takes a couple of seconds where i would like to add a spinner over the whole app while its fetching the data via reactivePoll. Bascially like in the example, but the data isnt fetched what also happens here.

is waiter in general usable with a reactiveval?

JohnCoene commented 7 months ago

It's because your function ends with waiter_hide

library(shiny)
library(ggplot2)
library(waiter)

# Function to generate random data
generate_data <- function() {
  Sys.sleep(3)
  data <- data.frame(
    x = rnorm(50),
    y = rnorm(50)
  )
  return(data)
}

# UI
ui <- fluidPage(
  titlePanel("ReactivePoll Example"),
  plotOutput("scatterplot")
)

# Server
server <- function(input, output, session) {

  data <- reactivePoll(
    intervalMillis = 10000,
    session = session,
    checkFunc = function() {
      print(format(Sys.time(), "%M"))
    },
    valueFunc = function() {
      waiter_show()
      on.exit(waiter_hide())
      generate_data()
    }

  )

  # Scatterplot
  output$scatterplot <- renderPlot({
    ggplot(data(), aes(x = x, y = y)) +
      geom_point() +
      labs(title = "Random Scatterplot")
  })
}

# Run the application
shinyApp(ui, server)
HugoGit39 commented 7 months ago

Thx! i thought I already tried on.exit(waiter_hide()) but somehow I didnt do it properly!