使Shinyapp在会话之间使用新输入

时间:2018-07-12 08:37:06

标签: r shiny

我正在开源的闪亮服务器上使用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)

1 个答案:

答案 0 :(得分:0)

您可以在全局文件中启动reactiveValue并使用它代替原始输入

原则上,全局文件中初始化的所有对象都是通过会话共享的。

示例

** UI **

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)

当用户更改容器数时,直方图将为所有用户更改,但不更改滑块

希望这会有所帮助!!