为什么我的反应值没有在每个会话唯一的服务器函数中初始化?

Why aren't my reactive values initialised in the server function unique to each session?

我正在将我的应用程序部署到 shinyapps,当我打开多个 windows 时,我注意到一些奇怪的行为。我渲染了一个数据 table,当我更新 window 上的过滤器时,我的 table 只更新最后打开的 window。

阅读范围文档后,我已将我的反应值移动到服务器函数中。

app.R

source("helpers/load_data.R")

server <- function(input, output, session) {

source("helpers/load_session_data.R")

output$risk_table <- renderDataTable({
DT::datatable(riskData$data
              rownames = FALSE)
})

observeEvent(input$get_filtered_data, {
    # UpdateTable function takes my table_csv and filters by the date, and updates the riskData reactive value
    UpdateTable(table, input$date)
}

UpdateTable <- function(table, date) {
#... filter stuff
riskData$data <- filtered_table
}
}

load_session_data.R

#table is a data.frame loaded globally outside of the ui and server functions.
riskData <- reactiveValues(data = table_csv) 

我认为在服务器函数中加载我的反应值意味着每个会话都会有它自己的反应值?我希望能够在不同的会话中独立过滤 table。

你需要做两件事

1) 在源调用期间设置local=T

source("helpers/load_data.R", local=T)
source("helpers/load_session_data.R", local=T)

"If you use the default value of local = FALSE, then the file will be sourced in the global environment."

Source

2) 将您的源调用置于服务器函数下,否则即使启用 local=T 调用也会跨会话共享

server <- function(input, output, session) {
source("helpers/load_data.R", local=T)
source("helpers/load_session_data.R", local=
...
}

闪亮文档中的一个非常好的示例..(代码可以找到 here

# Objects in this file are shared across all sessions in the same R
# process
source('all_sessions.R', local = TRUE)

server <- function(input, output, session) {
  # Objects in this file are defined in each session
  source('each_session.R', local = TRUE)

  output$text <- renderText({
    # Objects in this file are defined each time this function is called
    source('each_call.R', local = TRUE)

    # ...
  })
}