在 shiny 中,如何使用一个无功值作为另一个值的输入
In shiny, how to use a reactive value as an input to another one
在我闪亮的应用程序 (server.r) 中,我想使用一个 renderText
值作为另一个值的输入。
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'
编辑:问题的简单版本
这是对我上面的评论进行扩展的 MRE。 “Last 运行”每两秒更新一次,而“Current time”每秒更新一次。请注意 rvals$lastrun
的基础值与其在 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)
在我闪亮的应用程序 (server.r) 中,我想使用一个 renderText
值作为另一个值的输入。
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'
编辑:问题的简单版本
这是对我上面的评论进行扩展的 MRE。 “Last 运行”每两秒更新一次,而“Current time”每秒更新一次。请注意 rvals$lastrun
的基础值与其在 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)