我有一个 Shiny 的应用程序,其中主对象应根据其他对象/输入的变化进行更新(执行其他操作的按钮,其结果不易跟踪,例如在线数据)。这就是为什么我必须使用按钮输入。有没有一种方法可以更新主对象,而不必为每个按钮重新编写代码?在我的示例中,我必须使用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)
最佳答案
您只需使用两个按钮创建一个表达式,例如使用 c()
:
observeEvent(c(input$submit,input$change),{
data$data<-WorldPhones[,input$region]
})
关于r - Shiny 的对象对多个输入(按钮)的 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42235191/