Git Product home page Git Product logo

Comments (5)

gadenbuie avatar gadenbuie commented on May 29, 2024

Hi @HugoGit39, thanks for the question! The main issue is that req() throws an error that's normally handled gracefully by observers and reactives but that checkFunc isn't designed to handle. You can read the reactive value in checkFunc, though, so this pattern works:

  USER_LOGGED_IN <- reactiveVal(FALSE)

  observeEvent(input$login, {
    message("User has logged in.")
    # Pretend that the user has now logged in
    USER_LOGGED_IN(TRUE)
  })

  # Function to poll for updated data every 30 seconds
  data_poll <- reactivePoll(
    intervalMillis = 5000,
    session = session,
    checkFunc = function() {
      if (!USER_LOGGED_IN()) {
        message("Not logged in yet.")
        return()
      }

      # Return a timestamp to trigger polling
      message("Data check: ", format(Sys.time(), "%H:%M:%S"))
      format(Sys.time(), "%H:%M")
    },
    valueFunc = function() {
      # Generate new data
      req(USER_LOGGED_IN())
      generate_data()
    }
  )

I've put this modification in a small shinylive app from your example.

from shiny.

HugoGit39 avatar HugoGit39 commented on May 29, 2024

@gadenbuie Thx!. But the checkFunc is running already from the start. Isnt there a way to start the checkFunc (or the reactivePoll function) when the user logs in?

from shiny.

gadenbuie avatar gadenbuie commented on May 29, 2024

@gadenbuie Thx!. But the checkFunc is running already from the start. Isnt there a way to start the checkFunc (or the reactivePoll function) when the user logs in?

No, the way that reactivePoll() works is that checkFunc should be a cheap and quick function that can be called repeatedly. Whenever its value changes, it gets a new value by calling valueFunc.

The idea in my updated example is to have checkFunc return immediately when the user isn't yet logged in. Because that's only checking an R object, it'll resolve immediately. So it's okay that it's running before the value of data_poll() is actually used.

from shiny.

HugoGit39 avatar HugoGit39 commented on May 29, 2024

Right, but it its still a nice feature to have reactivePoll start running after a certain period right? Does checkFunc takes a lot of computing power on the server-side?

from shiny.

gadenbuie avatar gadenbuie commented on May 29, 2024

Right, but it its still a nice feature to have reactivePoll start running after a certain period right? Does checkFunc takes a lot of computing power on the server-side?

No, the code you use for checkFunc should be as quick and simple as possible. The idea is that checkFunc should run extremely quickly and be okay for Shiny to run more often than necessary without slowing down your app.

from shiny.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.