Shiny – 更新全局变量并在当前会话中查看结果

huangapple go评论131阅读模式
英文:

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 &lt;- 4

####/UI/####
header &lt;- dashboardHeader()

sidebar &lt;- dashboardSidebar()

body &lt;- dashboardBody(
  verbatimTextOutput(&quot;test&quot;)
)

ui &lt;- dashboardPage(header, sidebar, body)

####/SERVER/####
server &lt;- function(input, output, session) {
  
  data &lt;- reactive({num})
  output$test &lt;- renderText({ data() })
  
  observe({
    invalidateLater(0.5*60*1000,session)
    
    num &lt;&lt;- 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 &lt;- data.frame(ColA = c(&quot;dogs&quot;, &quot;cats&quot;, &quot;birds&quot;), ColB = c(10, 2, 2), stringsAsFactors = FALSE)

####/UI/####
header &lt;- dashboardHeader()

sidebar &lt;- dashboardSidebar()

body &lt;- dashboardBody(
  box(width = 3, tableOutput(&quot;test&quot;))
)

ui &lt;- dashboardPage(header, sidebar, body)

####/SERVER/####
server &lt;- function(input, output, session) {

  values &lt;- reactiveValues(n = dataset)

  data &lt;- reactive({values$n})
  output$test &lt;- renderTable({ data() })

  observe({
    invalidateLater(0.5*60*1000,session)

    new1 &lt;- sample(1:10,1,replace=T)
    new2 &lt;- sample(1:10,1,replace=T)
    new3 &lt;- sample(1:10,1,replace=T)
    
    print(new1)
    print(new2)
    print(new3)
    
    dat &lt;- data.frame(ColA = c(&quot;dogs&quot;, &quot;cats&quot;, &quot;birds&quot;), ColB = c(new1, new2, new3), stringsAsFactors = FALSE)
    
    values$n &lt;- dat
    dataset &lt;&lt;- dat

  })
  
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2020年1月7日 02:29:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定