2017-02-14 65 views
1

我有一個shinyapp,其中主對象應根據更改更新其他對象/輸入(執行其他操作的按鈕,其結果不容易跟蹤,例如,在線數據)。這就是爲什麼我不得不使用按鈕的輸入。有沒有辦法更新主對象,而無需重新編寫每個按鈕的代碼?在我的例子,我不得不用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) 

回答

0

你只是讓有兩個按鈕的表達,例如使用c()

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