gpt4 book ai didi

r - 多个用户改变 R Shiny 的 react 值

转载 作者:行者123 更新时间:2023-12-02 20:02:47 25 4
gpt4 key购买 nike

同一应用的多个用户是否可以更改同一组 react 值?

这个问题( handling multiple users simulaneously in an R Shiny app )表明不同 session 中的多个用户可以对相同的值进行更改(通过在 server() 之外声明它并使用 <<- 而不是 <- )但这只是为了简单旧值/变量。对于无功值来说这可能吗?

理想情况下,我希望用户 A 所做的更改立即反射(reflect)在用户 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)

基本上,我使用一个全局变量(正如您和帖子所建议的那样),然后使用 reactivePoll 函数将其挂回服务器以使外部依赖项具有反应性。

关于r - 多个用户改变 R Shiny 的 react 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com