r - 让 Shinyapp 跨会话使用新输入
问题描述
我在开源闪亮服务器上使用闪亮应用程序在多个设备上显示仪表板。我想有机会从本地 PC 更改所有仪表板上的图。如果在任何会话中更改了输入,则所有会话都应将其绘图更新为该新输入。我该怎么做呢?我可以将输入保存在全局变量中吗?
library(shiny)
# 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 = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
observe({
invalidateLater(10000)
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
# draw the histogram with the specified number of bins
p<<- hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
output$distPlot <- renderPlot({print(p)})
}
# Run the application
shinyApp(ui = ui, server = server)
解决方案
您可以reactiveValue
在全局文件中启动一个并使用它来代替原始输入
原则上,在全局文件中启动的所有对象都通过会话共享。
例子
**用户界面**
library(shiny)
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
# Application title
headerPanel("It's Alive!"),
# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot", height=250)
)
))
**服务器 **
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
# Expression that generates a histogram. The expression is
# wrapped in a call to renderPlot to indicate that:
#
# 1) It is "reactive" and therefore should be automatically
# re-executed when inputs change
# 2) Its output type is a plot
observe({
RV$bins = input$bins
})
output$distPlot <- renderPlot({
x <- faithful[, 2] # Old Faithful Geyser data
bins <- seq(min(x), max(x), length.out = RV$bins + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
** 全球的 **
RV <- reactiveValues(bins = 10)
当用户更改箱数时,所有用户的直方图都会更改,但滑块不会更改。
希望这可以帮助!!
推荐阅读
- flutter - 更喜欢 const 字面量作为 @immutable 类的构造函数的参数
- javascript - 何时以及谁调用我们作为参数传递给 Promise 构造函数的匿名函数?
- python - 数据输入问题 #input() 没有足够的值在 Anaconda3 中解包
- ios - 我可以以某种方式转换 Binding
到字符串? - pdf - 拆分模式下的脚注
- python - 如何将图像从 (200*200) 调整为 (368*368)?
- javascript - 没有 Live 服务器,Howler 无法播放
- javascript - Django Rest 框架客户端身份验证
- java - 使用 zip4j 从字节数组输入中提取受密码保护的文件
- java - 在 Macbook 上运行 pyspark 时出现 Java 异常