同一应用程序的多个用户是否可以更改同一组反应值?
这个问题(在R Shiny应用程序中同时处理多个用户)表明,在不同会话中的多个用户可以对同一值进行更改(通过在外部声明server()并使用<<-代替<-),但这仅适用于简单的旧值/变量。电抗值可能吗?
理想情况下,我希望用户A所做的更改能够立即反映在用户B查看的某些输出中。
这是基于 RStudio 的默认单文件 Shiny 应用程序的最小工作示例:
library(shiny)
slidervalue <- 30
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = slidervalue)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
textOutput('txt')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
observe({
slidervalue <<- input$bins
})
reactive_slidervalue <- reactivePoll(100, session,
checkFunc = function() { slidervalue },
valueFunc = function() { slidervalue }
)
output$txt <- renderText(reactive_slidervalue())
observe({
updateSliderInput(session, 'bins', value = reactive_slidervalue())
})
output$distPlot <- renderPlot({
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = reactive_slidervalue() + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application
shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)
基本上,我正在使用一个全局变量(如您和帖子所建议的那样),然后通过使用该reactivePoll函数使外部依赖项具有反应性将其挂回服务器。