Two things:
render*
functions are inherently reactive, so they should not be nested within another. In this case, it is already able to react without adding anything.
Action buttons work by incrementing their value each time it is pressed. This means if a user clicks it faster than R can react, it might actually be incremented by 2 (unlikely but perhaps feasible). The act of the button's value changing (incrementing) is what triggers the reaction, so you need to know what the previous values were to know which one (assuming only one) was clicked.
Try the following:
server <- function(input, output, session) {
buttonstates <- reactiveValues(one=0, two=0)
output$mean <- renderPlotly({
req(input$button1, input$button2)
if (input$button1 != buttonstates$one) {
buttonstates$one <- input$button1
output_mean(out(), start_date(), geo(), language(), grupo(), TRUE, "button1")
} else if (input$button2 != buttonstates$two) {
buttonstates$two <- input$button2
output_mean(out(), start_date(), geo(), language(), grupo(), TRUE, "button2")
}
})
}
Another option would be to separate the button-determination into
its own reactive block. The advantage of this is that it simplifies
your renderPlotly
block:
server <- function(input, output, session) {
buttonstates <- reactiveValues(one=0) # don't need two=0
whichbutton <- reactive({
req(input$button1, input$button2)
if (input$button1 != buttonstates$one) "button1" else "button2"
})
output$mean <- renderPlotly({
req(whichbutton())
output_mean(out(), start_date(), geo(), language(), grupo(), TRUE, whichbutton())
})
}
(If you aren't familiar with shiny::req
, it prevents the block from firing if the req
uired variable is not "truthy", as defined by shiny::isTruthy
. It's a handy way to prevent premature-firing during shiny start and/or other issues.)