Skip to content

Instantly share code, notes, and snippets.

@xydrolase
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save xydrolase/735422c65d639419ccb8 to your computer and use it in GitHub Desktop.

Select an option

Save xydrolase/735422c65d639419ccb8 to your computer and use it in GitHub Desktop.
Shiny: conditionalDisabledPanel
#' Conditional Disabled Panel
#'
#' Creates a panel whose child input elements are disabled or not,
#' depending on the value of a Javascript expression.
#'
#' See ?shiny:::conditionalPanel for the Javascript expression usage.
#'
#' @param condition A JavaScript expression that will be evaluated repeatedly to
#' determine whether the child input elements should be disabled.
#' @param ... Elements to include in the panel.
#'
conditionalDisabledPanel <- function(condition, ...) {
tagList(
# inject javascript only once
singleton(tags$head(HTML(
"
<script type='text/javascript'>
$(document).ready(function() {
var conds = $(document).find('[data-disabled-on-condition]');
// shown and hidden are Shiny events triggered after show/hide completion.
conds.on('shown', function() {
$(this).find(':input').prop('disabled', false);
})
.on('hidden', function() {
$(this).find(':input').prop('disabled', true);
//override visibility, toggle 'disabled' state instead
$(this).show();
});
});
</script>
"
))),
div('data-display-if'=condition, 'data-disabled-on-condition'=NA, ...)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment