R Shiny:使用 reactiveTimer 时保存以前的反应值
R Shiny : Save previous reactive value while using reactiveTimer
这是一个玩具示例,其中我每秒绘制一个介于 1 和 10 之间的数字,并且我只想在刚刚绘制的实际数字与之前绘制的数字之间存在差异时显示这些数字等于 1。因此,如果开奖号码的顺序是:[5,7,8,2,3]。我想连续显示[8,3]。我想这全都与反应值和隔离功能有关,但我无法意识到我想要什么。我在评论中的失败尝试。
library(shiny)
ui <- shinyUI(fluidPage(
mainPanel(
textOutput("time"),
textOutput("num")
)
))
server <- function(input, output, session) {
autoInvalidate <- reactiveTimer(1000)
timestamp<-reactive({
autoInvalidate()
Sys.time()
})
random_num<-reactive({
autoInvalidate()
sample(x=seq(1,10),size = 1,replace = FALSE,pro)
})
# My attempt : obviously do not work
# values <- reactiveValues(old=1)
# observe({
# if (random_num()==values$old) {values$display=random_num()}
# else {values$old=random_num()}
# })
output$time <- renderText({paste("time :",timestamp())})
output$num <- renderText({paste("num :",random_num())})
#My attempt : obviously do not work
# output$num <- renderText({paste("num :",isolate(values$display))})
}
shinyApp(ui, server)
我知道有一个 和我的很像,但完全不同。
这是一个例子。
library(shiny)
old <- NULL
ui <- shinyUI(fluidPage(
mainPanel(
textOutput("time"),
textOutput("num"),
textOutput("num_raw")
)
))
server <- function(input, output, session) {
autoInvalidate <- reactiveTimer(1000)
timestamp<-reactive({
autoInvalidate()
Sys.time()
})
random_num<-reactive({
autoInvalidate()
r <- sample(x=seq(1,10),size = 1,replace = FALSE)
})
observe({
output$time <- renderText({paste("time :",timestamp())})
r <- random_num()
if (!is.null(old) && r - old == 1) {
output$num <- renderText({paste("num :",r)})
}
output$num_raw <- renderText({paste("raw num : ", r)})
old <<- r
})
}
shinyApp(ui, server)
这是一个玩具示例,其中我每秒绘制一个介于 1 和 10 之间的数字,并且我只想在刚刚绘制的实际数字与之前绘制的数字之间存在差异时显示这些数字等于 1。因此,如果开奖号码的顺序是:[5,7,8,2,3]。我想连续显示[8,3]。我想这全都与反应值和隔离功能有关,但我无法意识到我想要什么。我在评论中的失败尝试。
library(shiny)
ui <- shinyUI(fluidPage(
mainPanel(
textOutput("time"),
textOutput("num")
)
))
server <- function(input, output, session) {
autoInvalidate <- reactiveTimer(1000)
timestamp<-reactive({
autoInvalidate()
Sys.time()
})
random_num<-reactive({
autoInvalidate()
sample(x=seq(1,10),size = 1,replace = FALSE,pro)
})
# My attempt : obviously do not work
# values <- reactiveValues(old=1)
# observe({
# if (random_num()==values$old) {values$display=random_num()}
# else {values$old=random_num()}
# })
output$time <- renderText({paste("time :",timestamp())})
output$num <- renderText({paste("num :",random_num())})
#My attempt : obviously do not work
# output$num <- renderText({paste("num :",isolate(values$display))})
}
shinyApp(ui, server)
我知道有一个
这是一个例子。
library(shiny)
old <- NULL
ui <- shinyUI(fluidPage(
mainPanel(
textOutput("time"),
textOutput("num"),
textOutput("num_raw")
)
))
server <- function(input, output, session) {
autoInvalidate <- reactiveTimer(1000)
timestamp<-reactive({
autoInvalidate()
Sys.time()
})
random_num<-reactive({
autoInvalidate()
r <- sample(x=seq(1,10),size = 1,replace = FALSE)
})
observe({
output$time <- renderText({paste("time :",timestamp())})
r <- random_num()
if (!is.null(old) && r - old == 1) {
output$num <- renderText({paste("num :",r)})
}
output$num_raw <- renderText({paste("raw num : ", r)})
old <<- r
})
}
shinyApp(ui, server)