Last active
August 29, 2015 14:05
-
-
Save xydrolase/735422c65d639419ccb8 to your computer and use it in GitHub Desktop.
Shiny: conditionalDisabledPanel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' 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