Reputation: 79
how can a person reset the actionButton back to being selected = character(0) rather than one of the radio buttons that they have available,
any idea?
Upvotes: 1
Views: 2580
Reputation: 978
you can use shinyjs package for this,Thanks to Dean Attali for this wonderful package.
This is an example:
if (interactive()) {
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
div(
id = "form",
textInput("name", "Name"),
radioButtons("gender", "Gender", c("Male", "Female"))
),
actionButton("resetAll", "Reset all"),
actionButton("resetName", "Reset name"),
actionButton("resetGender", "Reset Gender")
),
server = function(input, output) {
observeEvent(input$resetName, {
reset("name")
})
observeEvent(input$resetGender, {
reset("gender")
})
observeEvent(input$resetAll, {
reset("form")
})
}
)
}
You can read about this in here
Upvotes: 3
Reputation: 229
Are you trying to use an action button to reset a radio button? Try using updateRadioButton
(https://shiny.rstudio.com/reference/shiny/0.14/updateRadioButtons.html). Here is an example:
library("shiny")
ui <- fluidPage(
sidebarPanel(
radioButtons("radio_buttons", "Radio buttons", list("option 1", "option 2"),
selected = character(0)),
actionButton("reset_button", "Reset button")
)
)
server <- function(input, output, session) {
observeEvent(input$reset_button, {
updateRadioButtons(session, "radio_buttons", "Radio buttons", list("option 1", "option 2"),
selected = character(0)
)
})
}
shinyApp(ui = ui, server = server)
Upvotes: 2
Reputation: 452
To my knowledge it is impossible to reset an actionButton in shiny
.
You can consider incrementing a reactiveValue
each time the button is clicked, and then checking if this value is odd or even to perform a given action.
Upvotes: 1