首页 > 解决方案 > 让 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)

标签: rshiny

解决方案


您可以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)

当用户更改箱数时,所有用户的直方图都会更改,但滑块不会更改。

希望这可以帮助!!


推荐阅读