Agree with @Dean Attali. But if it is absolutely needed (Not Recommended),
library(shiny)
ui <- fluidPage(
radioButtons("btn","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("clickMe","Click Me to Reset")
)
server <- function(input, output, session) {
observeEvent(input$clickMe,{
updateRadioButtons(session,"btn",choices = c("Choice 1","Choice s"),selected = character(0))
})
}
shinyApp(ui, server)
Consider using a checkbox gorup as an alternative?
Edit
library(shiny)
ui <- fluidPage(
radioButtons("btn","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("clickMe","Click Me to Reset"),
actionButton("showValue", "Show value")
)
server <- function(input, output, session) {
selected = reactiveVal(NULL)
observeEvent(input$clickMe,{
updateRadioButtons(session,"btn",choices = c("Choice 1","Choice s"),selected = character(0))
selected(NULL)
})
observeEvent(input$btn,{
selected(input$btn)
})
observeEvent(input$showValue, {
print(selected())
})
}
shinyApp(ui, server)
you can use a reactive val as a proxy. But we are getting into real hacky territory here.
Edit 2 Custom javascript solution
Here is the custom javascript solution discussed in the comments. In this solution if the user double clicks the radio button it gets unselected. You can double check the value though the button. Note you will not refer to input$btn
for the button value. A custom value is created with input$radio_click
.
library(shiny)
ui <- fluidPage(
tags$script(HTML("
window.onload = function(){
var btns = document.getElementById('btn');
btns_radio = btns.getElementsByTagName('input');
for(var i = 0; i < btns_radio.length; i++){
btns_radio[i].addEventListener('click',function(x){
Shiny.onInputChange('radio_click', this.value)
});
btns_radio[i].addEventListener('dblclick',function(x){
if(this.checked){
this.checked = false;
Shiny.onInputChange('radio_click', null)
}
})
}
}
")),
radioButtons("btn","Click Me",choices = c("Choice 1","Choice s"),selected = character(0)),
actionButton("showValue", "Show value")
)
server <- function(input, output, session) {
observeEvent(input$showValue, {
print(input$radio_click)
})
}
shinyApp(ui, server)