如何将 table 的重新计算条件设置为 RShiny 应用程序中的 actionButton 单击

How to condition a recalculation of a table to an actionButton click in RShiny app

我有以下非常简单的应用程序

funSolver <- function(server,currencyList,tenorList,tailList) {

return(do.call(rbind,lapply(currencyList,function(ccy) {
    return(do.call(rbind,lapply(tenorList,function(tenor) {
        return(do.call(rbind,lapply(tailList,function(myTail) {
            a <- 0
            b <- 10
            d <- 25
            e <- 35
            return(data.frame(ccy=ccy,tenor=tenor,tail=myTail,a=a,b=b,d=d,e=e))
        })))
    })))
})))
}


ui <- fluidPage(

titlePanel("Carry Selector"),
sidebarPanel(
    selectInput(inputId = 'serverListInput',label = 'Server', choices=as.list(paste("adsg-",c("l01","ln01","l02","ln02"),sep="")),multiple = FALSE,selectize = TRUE),
    selectInput(inputId = 'currencyListInput',label = 'blabla', choices=list("blabla1","blabla2","blabla3"),multiple = TRUE,selectize = TRUE),
    fluidRow(
        column(6,selectInput(inputId = 'tenorListInput',label = 'Tenors', choices=as.list(c("All",paste(c(1,3,6),"m",sep=""),paste(c(seq(1,9),seq(10,30,5)),"y",sep=""))),multiple = TRUE,selectize = TRUE)),
        column(6,selectInput(inputId = 'tailListInput',label = 'Tails', choices=as.list(c("All",paste(c(1,2,3,5,7,10,15,20),"y",sep=""))),multiple = TRUE,selectize = TRUE))
    ),
    actionButton(inputId = 'launchCalcButton',label = 'Launch Calc')
),
mainPanel(
    fluidRow(
        column(12,dataTableOutput(outputId = 'table'))
    )
)
)

server <- function(input,output){
observeEvent(input$launchCalcButton, {  
    output$table <- renderDataTable({
        datatable(funSolver(input$serverListInput,input$currencyListInput,input$tenorListInput,input$tailListInput))
    })
})
}

app = shinyApp(ui,server)
runApp(app,port=3250,host='0.0.0.0')

您选择几个参数并单击按钮以显示 funSolver 函数生成的 table。我已将按钮调用包装到 observeEvent 中,因此仅当您单击时才会生成 table。我不明白为什么,如果您在第一次单击按钮后更改参数,即使我没有单击按钮,table 也会更新。

问题 1:这种行为是预期的吗?

Q2:如果是,如何在闪亮的应用程序中做我想做的事情(table仅在单击按钮时刷新,而不是在更新参数时刷新)?

感谢所有帮助

您在观察事件部分中添加了渲染函数,这意味着它会 运行 每当此事件中的依赖项发生变化时。查看 ?observeEvent 为您提供了一个非常好的方法来获得您想要的东西:

编辑

我试了一下 selectinput 和 button 选项,发现它实际上是由于 multiple=TRUE 参数(我相信)。出于某种原因,此参数否决了 observeEvent 中的输入按钮依赖性。但是下面带有 eventReactive 的版本确实有效:

server <- function(input,output){
  df = eventReactive(input$launchCalcButton, {  
       data.table(funSolver(input$serverListInput,input$currencyListInput,input$tenorListInput,input$tailListInput))
  })

  output$table <- renderDataTable({
    df()
  })
}