Skip to content

Instantly share code, notes, and snippets.

@Quiri
Created September 9, 2014 10:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quiri/95587c804e1fa737c61a to your computer and use it in GitHub Desktop.
Save Quiri/95587c804e1fa737c61a to your computer and use it in GitHub Desktop.
Shiny disable checkbox minimal example
library(shiny)
shinyServer(function(input, output, session) {
observe({
if(input$tv == "All, but C") {
updateCheckboxGroupInput(session, "check2", label = NULL, choices = NULL,
selected = c("A", "B", "D", "E"))
}
if(input$tv == "Only C or D") {
updateCheckboxGroupInput(session, "check2", label = NULL, choices = NULL,
selected = c("C", "D"))
}
if(input$tv == "All") {
updateCheckboxGroupInput(session, "check2", label = NULL, choices = NULL,
selected = c("A", "B","C", "D", "E"))
}
})
output$values <- renderPrint({
list(radio = input$radio, checkbox = input$check)
})
output$values2 <- renderPrint({
list(radio = input$tv, checkbox = input$check2)
})
})
library(shiny)
shinyUI(
fluidPage(
tagList(
tags$head(
tags$script(type="text/javascript", src = "disable.js")
)
),
sidebarLayout(
sidebarPanel(
radioButtons("radio",
h5("Uncheck all:"),
c("All", "Custom"),
inline = T
),
checkboxGroupInput("check",
"",
c(1,2,3,4,5),
inline = T
),
radioButtons("tv",
h5("Uncheck custom:"),
c("All", "All, but C" ,"Only C or D"),
inline = T
),
checkboxGroupInput("check2",
"",
c("A","B","C","D","E"),
c("A","B","C","D","E"),
inline = T
)
),
mainPanel(
h5("Uncheck all"),
verbatimTextOutput('values'),
h5("Uncheck custom:"),
verbatimTextOutput('values2')
)
)
)
)
$(document).ready(function(){
disableButtons(1, 5, "check", true); //Disable all
disableButtons(1, 5, "check2", true); //Disable all
$('.radio').click(function() {
//Uncheck all
if($('#radio1').prop('checked')) {
disableButtons(1, 5, "check", true);
} else {
disableButtons(1, 5, "check", false);
}
//Uncheck custom
if($('#tv1').prop('checked')){
disableButtons(1, 5, "check2", true);
}
if($('#tv2').prop('checked')){
disableButtons(1, 5, "check2", false); //Enable all
disableButtons(3, 3, "check2", true); //Disable C
}
if($('#tv3').prop('checked')){
disableButtons(1, 5, "check2", true); //Disable all
disableButtons(3, 4, "check2", false); //Enable C, D
}
})
})
var disableButtons = function(from, to, id, dis) {
for(i = from; i<= to; i++) {
$('#' + id + "" + i).prop('disabled', dis);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment