|
| 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 | +reset.event.handlers = function(app = get.app()) { |
| 13 | + app$values=list() |
| 14 | +} |
| 15 | + |
| 16 | +add.handlers.to.session = function(handlers, session.env=app$session.env, app=get.app()) { |
| 17 | + for (el in handlers) { |
| 18 | + call = el$call |
| 19 | + eval(call, session.env) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +add.handler = function(id, call, type="unknown", app = get.app()) { |
| 24 | + if (app$is.running) { |
| 25 | + eval(app$handler.env, call) |
| 26 | + } else { |
| 27 | + app$handlers[[length(app$handlers)+1]] = list(id=id, call=call, type=type) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +add.change.handler = function(id, fun,...,app=get.app(), on.create=FALSE) { |
| 32 | + fun = substitute(fun) |
| 33 | + # Create dynamic observer |
| 34 | + args = list(...) |
| 35 | + ca = substitute(env=list(s_id=id, s_fun=fun,s_args=args, s_on.create=on.create), |
| 36 | + observe({ |
| 37 | + display("called event handler for ",s_id) |
| 38 | + input[[s_id]] |
| 39 | + if (has.widget.value.changed(s_id, input[[s_id]], on.create=s_on.create)) { |
| 40 | + display("run event handler for ",s_id) |
| 41 | + do.call(s_fun, c(list(id=s_id, value=input[[s_id]], session=session),s_args)) |
| 42 | + } |
| 43 | + }) |
| 44 | + ) |
| 45 | + add.handler(id=id,call=ca,type="change",app=app) |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +add.button.handler = function(id, fun,..., app = get.app()) { |
| 51 | + |
| 52 | + fun = substitute(fun) |
| 53 | + args = list(...) |
| 54 | + |
| 55 | + ca = substitute(env=list(s_id=id, s_fun=fun,s_args=args), |
| 56 | + observe({ |
| 57 | + if (has.button.counter.increased(s_id, input[[s_id]])) { |
| 58 | + display(s_id, " has been clicked...") |
| 59 | + do.call(s_fun, c(list(id=s_id, value=input[[s_id]], session=session),s_args)) |
| 60 | + } |
| 61 | + }) |
| 62 | + ) |
| 63 | + add.handler(id=id,call=ca,type="button",app=app) |
| 64 | +} |
| 65 | + |
| 66 | +has.widget.value.changed = function(id, new.value,on.create=FALSE, app = get.app()) { |
| 67 | + restore.point("has.widget.value.changed") |
| 68 | + if (!id %in% names(app$values)) { |
| 69 | + app$values[[id]] = new.value |
| 70 | + changed = on.create |
| 71 | + } else { |
| 72 | + changed = !identical(app$values[[id]],new.value) |
| 73 | + if (changed) { |
| 74 | + app$values[[id]] = new.value |
| 75 | + } |
| 76 | + } |
| 77 | + return(changed) |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +has.button.counter.increased = function(id, counter, app=get.app()) { |
| 82 | + restore.point("has.widget.counter.increased") |
| 83 | + if (isTRUE(counter == 0) | is.null(counter) | isTRUE(counter<=app$values[[id]])) { |
| 84 | + app$values[[id]] = counter |
| 85 | + cat("\nno counter increase: ", id, " ",counter) |
| 86 | + return(FALSE) |
| 87 | + } |
| 88 | + app$values[[id]] = counter |
| 89 | + cat("\ncounter has increased: ", id, " ",counter) |
| 90 | + return(TRUE) |
| 91 | +} |
0 commit comments