Reputation: 177
I found out that is possible to add additional arguments to an actionButton. Now what i would like to know how to retrieve this additional argument inside an observeEvent.
library(shiny)
ui <- fluidPage(
actionButton("goButton", "Go", value = 10)
)
server <- function(input, output, session) {
observeEvent(input$goButton,{
# print value argument of the action Button - should in that case always print 10
})
}
shinyApp(ui, server)
Many thanks!
Upvotes: 1
Views: 367
Reputation: 8557
Apparently, value
is not very useful. Here's a discussion about this. This is the answer of Joe Cheng to the following remark:
I still don't quite understand the resistance to not being able to reset to zero - it doesn't seem to inherently promote any bad coding practices
It absolutely, positively does promote bad coding practices. Just look at all of the code examples in this thread (through no fault of the users, of course--we just haven't done a good enough job with education). The action button being clicked should be thought of as a discrete event, not a value. The special 0 value is just a workaround to prevent the code from thinking that the button is clicked at startup. It's the fact that a number has changed that is the relevant signal here, not the value that the number happens to be.
If you want to count the number of times this button is clicked, here's an example of application:
library(shiny)
library(shinyWidgets)
ui <- basicPage(
actionButton("click", "Click"),
verbatimTextOutput("number", placeholder = TRUE)
)
server <- function(input, output) {
count <- reactiveValues(value = 0)
observeEvent(input$click, {
count$value <- count$value + 1
output$number <- renderText(count$value)
})
}
shinyApp(ui = ui, server = server)
Upvotes: 1