|
| 1 | +# Manually trigger shiny events when some value changes |
| 2 | +# While this is also the key idea of reactive programming |
| 3 | +# Shiny (at least in current versions as of October 2014) |
| 4 | +# tends to trigger events too often. |
| 5 | + |
| 6 | +# What we need: |
| 7 | +# 1. Call a function or render output when a button is clicked |
| 8 | +# 2. Call a function or render output when an input value has changed |
| 9 | +# 3. Update input values when an input value or variable has changed without triggering further events |
| 10 | + |
| 11 | + |
| 12 | +resetEventHandlers = function(app = getApp()) { |
| 13 | + app$values=list() |
| 14 | +} |
| 15 | + |
| 16 | +addEventHandlersToSession = function(handlers, session.env=app$session.env, app=getApp()) { |
| 17 | + for (el in handlers) { |
| 18 | + call = el$call |
| 19 | + eval(call, session.env) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +addEventHandlerToApp = function(id, call, type="unknown", app = getApp(),session.env=app$session.env) { |
| 24 | + n = length(app$handlers)+1 |
| 25 | + app$handlers[[n]] = list(id=id, call=call, type=type) |
| 26 | + names(app$handlers)[n] <- id |
| 27 | + if (app$is.running) { |
| 28 | + eval(call,app$session.env) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#' Add an handler to an input that is called when the input value changes |
| 33 | +#' |
| 34 | +#' @param id name of the input element |
| 35 | +#' @param fun function that will be called if the input value changes. The function will be called with the arguments: 'id', 'value' and 'session'. One can assign the same handler functions to several input elements. |
| 36 | +addChangeHandler = function(id, fun,...,app=getApp(), on.create=FALSE) { |
| 37 | + #browser() |
| 38 | + fun = substitute(fun) |
| 39 | + # Create dynamic observer |
| 40 | + args = list(...) |
| 41 | + ca = substitute(env=list(s_id=id, s_fun=fun,s_args=args, s_on.create=on.create), |
| 42 | + observe({ |
| 43 | + display("called event handler for ",s_id) |
| 44 | + input[[s_id]] |
| 45 | + if (hasWidgetValueChanged(s_id, input[[s_id]], on.create=s_on.create)) { |
| 46 | + display("run event handler for ",s_id) |
| 47 | + do.call(s_fun, c(list(id=s_id, value=input[[s_id]], session=session),s_args)) |
| 48 | + } |
| 49 | + }) |
| 50 | + ) |
| 51 | + addEventHandlerToApp(id=id,call=ca,type="change",app=app) |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +#' Add an handler to a button |
| 56 | +#' |
| 57 | +#' @param id name of the button |
| 58 | +#' @param fun function that will be called if button is pressed. The function will be called with the arguments: 'id', 'value' and 'session'. One can assign the same handler functions to several buttons. |
| 59 | +addButtonHandler = function(id, fun,..., app = getApp()) { |
| 60 | + |
| 61 | + if (app$verbose) |
| 62 | + display("\naddButtonHandler('",id,'",...)') |
| 63 | + |
| 64 | + fun = substitute(fun) |
| 65 | + args = list(...) |
| 66 | + |
| 67 | + ca = substitute(env=list(s_id=id, s_fun=fun,s_args=args), |
| 68 | + observe({ |
| 69 | + if (hasButtonCounterIncreased(s_id, input[[s_id]])) { |
| 70 | + display(s_id, " has been clicked...") |
| 71 | + do.call(s_fun, c(list(id=s_id, value=input[[s_id]], session=session),s_args)) |
| 72 | + } |
| 73 | + }) |
| 74 | + ) |
| 75 | + addEventHandlerToApp(id=id,call=ca,type="button",app=app) |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +#' Checks whether the value of an input item has been changed (internal function) |
| 80 | +hasWidgetValueChanged = function(id, new.value,on.create=FALSE, app = getApp()) { |
| 81 | + restore.point("hasWidgetValueChanged") |
| 82 | + if (!id %in% names(app$values)) { |
| 83 | + app$values[[id]] = new.value |
| 84 | + changed = on.create |
| 85 | + } else { |
| 86 | + changed = !identical(app$values[[id]],new.value) |
| 87 | + if (changed) { |
| 88 | + app$values[[id]] = new.value |
| 89 | + } |
| 90 | + } |
| 91 | + return(changed) |
| 92 | +} |
| 93 | + |
| 94 | +#' Checks whether a button has been pressed again (internal function) |
| 95 | +hasButtonCounterIncreased = function(id, counter, app=getApp()) { |
| 96 | + restore.point("hasButtonCounterIncreased") |
| 97 | + if (isTRUE(counter == 0) | is.null(counter) | isTRUE(counter<=app$values[[id]])) { |
| 98 | + app$values[[id]] = counter |
| 99 | + cat("\nno counter increase: ", id, " ",counter) |
| 100 | + return(FALSE) |
| 101 | + } |
| 102 | + app$values[[id]] = counter |
| 103 | + cat("\ncounter has increased: ", id, " ",counter) |
| 104 | + return(TRUE) |
| 105 | +} |
0 commit comments