Skip to content

Instantly share code, notes, and snippets.

@xiaodaigh
Last active August 18, 2022 17:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xiaodaigh/6810928 to your computer and use it in GitHub Desktop.
Save xiaodaigh/6810928 to your computer and use it in GitHub Desktop.
Shiny: Disable Button
library(shiny)
disableActionButton <- function(id,session) {
session$sendCustomMessage(type="jsCode",
list(code= paste("$('#",id,"').prop('disabled',true)"
,sep="")))
}
shinyServer(function(input, output,session) {
observe({
if(input$btn1 == 0) return()
disableActionButton("btn2",session)
})
})
library(shiny)
shinyUI(basicPage(
tags$head(tags$script(HTML('
Shiny.addCustomMessageHandler("jsCode",
function(message) {
console.log(message)
eval(message.code);
}
);
')))
,actionButton("btn1","Disable the other button")
,actionButton("btn2","Button")
)
)
@elahika
Copy link

elahika commented Aug 8, 2014

Hi

Thanks for your code. Is is possible to do the same thing for downloadButton? I'm not very familiar with javascript, but I see that downloadButton does not have an id.
Also, is it possible to add a pop-op message for explaining why this button is disabled?

@jiesong111
Copy link

Does anyone know how to do it? I have some questions, I want to disable certain select inputs, this example only works for actionbutton, can any one tell me how to disable select inputs?
Thanks very much.

@daattali
Copy link

Hi, you can disable select inputs (and almost any other input) using my new package shinyjs with a simple function call shinyjs::disable(id)
https://github.com/daattali/shinyjs

Example:

library(shiny)
library(shinyjs)
runApp(shinyApp(
  ui = fluidPage(
    useShinyjs(),
    selectInput("test", "Select once", letters),
    actionButton("submit", "Choose")
  ),
  server = function(input, output, session) {
    observeEvent(input$submit, {
      disable("test")
    })
  }
))

The reason disabling selectize inputs and some other inputs don't just work is because they use a special javascript library to construct the tag instead of plain HTML. shinyjs is meant to help shiny app developers do these kinds of small tasks with normal R code instead of fiddling with javsacript

@cutariechu
Copy link

Thank you Dean, that was very helpful

@tanthiamhuat
Copy link

can we do a similar one to enable a disabled button like below:
enableActionButton <- function(id,session) { session$sendCustomMessage(type="jsCode", list(code= paste("$('#",id,"').prop('enabled',true)" ,sep=""))) }

@vitallish
Copy link

Hi, unfortunately the last comment didn't work for me. It should be as below - the property to change is still the 'disabled' property

enableActionButton <- function(id,session) {
  session$sendCustomMessage(type="jsCode",
                             list(code= paste("$('#",id,"').prop('disabled',false)"
                                    ,sep="")))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment