I have a shinyapp, in which a main object should be updated depending on the change other objects/inputs (buttons that perform other operations, whose result is not easily tracked, e.g. online data). That's why I had to use the input of buttons. Is there a way to update the main object without having to re-write the code for every button? In my example, I had to use observeEvent two times:
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)
You simply make an expression with both buttons, for example using c()
:
observeEvent(c(input$submit,input$change),{
data$data<-WorldPhones[,input$region]
})