英文:
Shiny - Updating global variable and seeing the result in current session
问题
我正在使用全局变量,这些变量在时间X后更新。我遇到的问题是,它更新了全局变量,但当前会话没有相应地更新,然而,任何新打开的会话都使用了更新后的全局变量。
问题:如何使当前会话使用更新后的全局变量? 我认为将其包装在一个响应式中会起作用,但实际情况并非如此。
代码:
library(shiny)
library(shinydashboard)
####/GLOBAL/####
num <- 4
####/UI/####
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
  verbatimTextOutput("test")
)
ui <- dashboardPage(header, sidebar, body)
####/SERVER/####
server <- function(input, output, session) {
  
  data <- reactive({num})
  output$test <- renderText({ data() })
  
  observe({
    invalidateLater(0.5*60*1000,session)
    
    num <<- sample(1:1000,1,replace=T)
  })
  
}
shinyApp(ui, server)
如果您等待30秒以上,然后打开一个新的会话,您会看到数字已经从4更改,但原始会话仍然显示4。它们应该显示相同的数字。
英文:
I am working with global variables that update after time X. This issue I am coming across is it updates the global variable but the current session doesn't update accordingly, however, any new session open uses the updated global variable.
Question: how do I get the current session to use the updated global variable? I thought wrapping it in a reactive would work but it doesn't.
Code:
library(shiny)
library(shinydashboard)
####/GLOBAL/####
num <- 4
####/UI/####
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
  verbatimTextOutput("test")
)
ui <- dashboardPage(header, sidebar, body)
####/SERVER/####
server <- function(input, output, session) {
  
  data <- reactive({num})
  output$test <- renderText({ data() })
  
  observe({
    invalidateLater(0.5*60*1000,session)
    
    num <<- sample(1:1000,1,replace=T)
  })
  
}
shinyApp(ui, server)
If you wait 30+ seconds and then open up a new session you will see that the number has changed from 4 but the original session still shows 4. They should be showing the same number.
答案1
得分: 1
已解决!我意识到我需要将其包装在reactiveValues中,而不是reactive。我还将更新的值更改为数据框而不是单个数字,因为这符合我的实际仪表板的需求。
library(shiny)
library(shinydashboard)
####/全局/####
dataset <- data.frame(ColA = c("dogs", "cats", "birds"), ColB = c(10, 2, 2), stringsAsFactors = FALSE)
####/UI/####
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
  box(width = 3, tableOutput("test"))
)
ui <- dashboardPage(header, sidebar, body)
####/服务器/####
server <- function(input, output, session) {
  values <- reactiveValues(n = dataset)
  data <- reactive({values$n})
  output$test <- renderTable({ data() })
  observe({
    invalidateLater(0.5*60*1000, session)
    new1 <- sample(1:10, 1, replace = TRUE)
    new2 <- sample(1:10, 1, replace = TRUE)
    new3 <- sample(1:10, 1, replace = TRUE)
    print(new1)
    print(new2)
    print(new3)
    dat <- data.frame(ColA = c("dogs", "cats", "birds"), ColB = c(new1, new2, new3), stringsAsFactors = FALSE)
    values$n <- dat
    dataset <<- dat
  })
}
shinyApp(ui, server)
英文:
Solved! Realized I needed to wrap it in a reactiveValues versus reactive. I also made the updating a value a dataframe versus a single number because that fits my real dashboard's problem.
library(shiny)
library(shinydashboard)
####/GLOBAL/####
dataset <- data.frame(ColA = c("dogs", "cats", "birds"), ColB = c(10, 2, 2), stringsAsFactors = FALSE)
####/UI/####
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
  box(width = 3, tableOutput("test"))
)
ui <- dashboardPage(header, sidebar, body)
####/SERVER/####
server <- function(input, output, session) {
  values <- reactiveValues(n = dataset)
  data <- reactive({values$n})
  output$test <- renderTable({ data() })
  observe({
    invalidateLater(0.5*60*1000,session)
    new1 <- sample(1:10,1,replace=T)
    new2 <- sample(1:10,1,replace=T)
    new3 <- sample(1:10,1,replace=T)
    
    print(new1)
    print(new2)
    print(new3)
    
    dat <- data.frame(ColA = c("dogs", "cats", "birds"), ColB = c(new1, new2, new3), stringsAsFactors = FALSE)
    
    values$n <- dat
    dataset <<- dat
  })
  
}
shinyApp(ui, server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论