【问题标题】:shiny object reactivity to several inputs (buttons)闪亮的对象对几个输入(按钮)的反应
【发布时间】:2017-07-03 06:38:02
【问题描述】:

我有一个闪亮的应用程序,其中一个主要对象应根据其他对象/输入的更改(执行其他操作的按钮,其结果不容易跟踪,例如在线数据)进行更新。这就是为什么我不得不使用按钮的输入。有没有办法更新主对象而不必为每个按钮重新编写代码?在我的示例中,我不得不使用两次 observeEvent:

library(datasets)
library(shiny)

ui<-fluidPage(    
  titlePanel("Telephones by region"),
  sidebarLayout(      
    sidebarPanel(
      selectInput("region", "Region:", 
                  choices=colnames(WorldPhones)),
      helpText("Data from AT&T (1961) The World's Telephones."),
      actionButton("submit", 
                   label = "submit"), # this also has other functions
      actionButton("change", 
                   label = "change")  # this also has other functions
    ),
    mainPanel(
      plotOutput("phonePlot")  
    )
  )
)
server<-function(input, output) {
data<-reactiveValues()

observeEvent(input$submit,{
  data$data<-WorldPhones[,input$region]
  })
observeEvent(input$change,{
  data$data<-WorldPhones[,input$region]
})
output$phonePlot <- renderPlot({
   if(!is.null(data$data))
    barplot(data$data*1000, 
            ylab="Number of Telephones",
            xlab="Year")
  })
}
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny observers reactive


    【解决方案1】:

    您只需使用两个按钮创建一个表达式,例如使用c()

    observeEvent(c(input$submit,input$change),{
      data$data<-WorldPhones[,input$region]
      })
    

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 2021-01-12
      • 2015-04-29
      • 2015-04-26
      • 2020-12-28
      相关资源
      最近更新 更多