简体   繁体   中英

In shiny, how to use a reactive value as an input to another one

In my shiny app (server.r) I'd like to use a renderText value as input into another one.

autoinvalidate <- reactiveTimer(2000)

# this works
rvals <- reactiveValues()
rvals$lastrun <- renderText({
  autoinvalidate()
  suppressWarnings(readLines(logfile))
})

rvals$currenttime <- renderText({
  invalidateLater(1000, session)
  format(Sys.time())
})

# this doesn't work
rvals$timesincelastrun <- renderText({
  autoinvalidate()
  as.character(rvals$lastrun)
}
)
Warning: Error in as.vector: cannot coerce type 'closure' to vector of type 'character'

EDIT: simpler version of the question

Here's a MRE that expands on my comment above. "Last run" updates every two seconds, whereas "Current time" updates every second. Note the separation between the underlying value of rvals$lastrun and its representation in the UI.

library(shiny)

ui <- fluidPage(
        textOutput("currenttime"),
        textOutput("lastrun")
)

server <- function(input, output, session) {
    autoinvalidate <- reactiveTimer(2000)
    
    rvals <- reactiveValues(lastrun=NA)
    
    observe({
        autoinvalidate()
        # since logFile is undefined
        # suppressWarnings(readLines(logfile))
        rvals$lastrun = format(Sys.time())
    })
    
    output$currenttime <- renderText({
        invalidateLater(1000, session)
        paste0("Current time: ", format(Sys.time()))
    })
    
    output$lastrun <- renderText({
        paste0("Last run: ", rvals$lastrun)
    })
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM