i want keep reflashing until 10:05, after 10:05 got tplus0_dt , stop processing invalidaterlater().
before 10:00, tplus0_dt doesn't exist, need keep reflashing until 10:05. after 10:05, no need refalsh, , when tplus0_dt becomes lage invalidaterlater() effects displaying of table1, screen , plots go gray every 1 seconds looks page dead while updating occurs.
so how can stop processing invalidatelater() , keep showing data after 10:05? help! example code below.
require(shiny) require(data.table) app <- shinyapp( server = function(input, output, session){ get_tplus0_data <- reactive({ time <- substr(as.character(sys.time()), 12, 16) invalidatelater(1000) if(time >= "10:05"){ # tplus0_dt<-data.table(read.csv("/data/df_highest_after_signal.csv",header = t, sep = ",", stringsasfactors = f)) tplus0_dt<- data.table(a = c(1, 2, 3, 4), b = c(3, 4, 5, 8)) return(tplus0_dt) } }) output$table1 <- rendertable({get_tplus0_data()}) }, ui = fluidpage( tableoutput("table1") ) ) runapp(app)
how override function needs?
if enter invalidatelaternew
in console, code of function printed.
to overwrite function within package post help: override function imported in namespace
then have consider functions .getreactiveenvironment()
, timercallbacks()
not accessible outside namespace. can call them this: shiny:::.getreactiveenvironment()
bring together:
you add additional parameter (e.g. update
), enable stop invalidelater()
whenever want.
invalidatelaternew <- function (millis, session = getdefaultreactivedomain(), update = true) { if(update){ ctx <- shiny:::.getreactiveenvironment()$currentcontext() shiny:::timercallbacks$schedule(millis, function() { if (!is.null(session) && session$isclosed()) { return(invisible()) } ctx$invalidate() }) invisible() } } unlockbinding("invalidatelater", as.environment("package:shiny")) assign("invalidatelater", invalidatelaternew, "package:shiny")
example:
i used example given in ?invalidatelater
demonstrate effect: (invalidatelater
stop when input$n
is bigger 800. can adapt example time restriction). decided not use time restriction example wouldnt handy test ;)
ui <- fluidpage( sliderinput("n", "number of observations", 2, 1000, 500), plotoutput("plot") ) server <- function(input, output, session) { observe({ # re-execute reactive expression after 1000 milliseconds invalidatelater(1000, session, input$n < 800) # each time invalidated. # isolate() makes observer _not_ invalidated , re-executed # when input$n changes. print(paste("the value of input$n is", isolate(input$n))) }) # generate new histogram @ timed intervals, not when # input$n changes. output$plot <- renderplot({ # re-execute reactive expression after 2000 milliseconds invalidatelater(2000, session, input$n < 800) hist(rnorm(isolate(input$n))) }) } shinyapp(ui, server)
Comments
Post a Comment