Skip to content

Commit

Permalink
draft of simpler API for dynamic input update (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisGuillem committed Mar 10, 2017
1 parent 0506349 commit 0e9346e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
47 changes: 36 additions & 11 deletions R/controls.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#Copyright © 2016 RTE Réseau de transport d’électricité

# Private function used to create input generator functions.
#' Private function used to create input generator functions.
#' @param type character string indicating the type of input
#' @param inputFunction input generator function
#' @param params A named list containing at least elements "value" and "label".
#' Other elements need to be expressions that give the value of a given
#' parameter. Use prepareParams() to construct this argument.
#' @param valueVar name of the parameter containing the value in inputFunction.
#' only useful if this parameter is not named "value"
#' @param .display expression that evaluates to TRUE or FALSE
#'
#' @return a function (params) -> html. The function has some attributes:
#' - params: parameters entered by the user
#' - valueVar: names of arguments containing value of the input
#' - type: character string containing the input type
#' - display: expression that evaluates to TRUE or FALSE
#' @noRd
mwControlFactory <- function(type, inputFunction, params, valueVar = NULL, .display = NULL) {

res <- function(params) {
Expand All @@ -10,17 +25,27 @@ mwControlFactory <- function(type, inputFunction, params, valueVar = NULL, .disp
for (i in 1:length(valueVar)) params[[valueVar[i]]] <- params$value[[i]]
}
params$value <- NULL
} else {
valueVar <- "value"
}
if (is.null(params$label)) params$label <- params$inputId
do.call(inputFunction, params)
}

attr(res, "params") <- params
attr(res, "valueVar") <- valueVar
attr(res, "type") <- type
attr(res, "display") <- lazyeval::expr_find(.display)
res
}

prepareParams <- function(value, label, ...) {
params <- lapply(lazy_dots(..., .follow_symbols = TRUE), function(x) x$expr)
params$value <- value
params$label <- label
params
}

#' Add a Slider to a manipulateWidget gadget
#'
#' @param min
Expand Down Expand Up @@ -70,7 +95,7 @@ mwSlider <- function(min, max, value, label = NULL, ..., .display = TRUE) {
mwControlFactory(
"slider",
function(...) {tags$div(style = "padding:0 5px;", shiny::sliderInput(...))},
list(min = min, max = max, value = value, label = label, ...),
prepareParams(min = min, max = max, value = value, label = label, ...),
.display = .display
)
}
Expand Down Expand Up @@ -102,7 +127,7 @@ mwSlider <- function(min, max, value, label = NULL, ..., .display = TRUE) {
mwText <- function(value = "", label = NULL, ..., .display = TRUE) {
mwControlFactory(
"text", shiny::textInput,
list(value = value, label = label, ...),
prepareParams(value = value, label = label, ...),
.display = .display
)
}
Expand Down Expand Up @@ -135,7 +160,7 @@ mwText <- function(value = "", label = NULL, ..., .display = TRUE) {
mwNumeric <- function(value, label = NULL, ..., .display = TRUE) {
mwControlFactory(
"numeric", shiny::numericInput,
list(value = value, label = label, ...),
prepareParams(value = value, label = label, ...),
.display = .display
)
}
Expand Down Expand Up @@ -172,7 +197,7 @@ mwNumeric <- function(value, label = NULL, ..., .display = TRUE) {
mwPassword <- function(value = "", label = NULL, ..., .display = TRUE) {
mwControlFactory(
"password", shiny::passwordInput,
list(value = value, label = label, ...),
prepareParams(value = value, label = label, ...),
.display = .display
)
}
Expand Down Expand Up @@ -226,7 +251,7 @@ mwSelect <- function(choices = value, value = NULL, label = NULL, ...,
multiple = FALSE, .display = TRUE) {
mwControlFactory(
"select", shiny::selectInput,
list(choices = choices, value = value, label = label, ..., multiple = multiple),
prepareParams(choices = choices, value = value, label = label, ..., multiple = multiple),
valueVar = "selected",
.display = .display
)
Expand Down Expand Up @@ -261,7 +286,7 @@ mwSelect <- function(choices = value, value = NULL, label = NULL, ...,
mwCheckbox <- function(value = FALSE, label = NULL, ..., .display = TRUE) {
mwControlFactory(
"checkbox", shiny::checkboxInput,
list(value = value, label = label, ...),
prepareParams(value = value, label = label, ...),
.display = .display
)
}
Expand Down Expand Up @@ -298,7 +323,7 @@ mwCheckbox <- function(value = FALSE, label = NULL, ..., .display = TRUE) {
mwRadio <- function(choices, value = NULL, label = NULL, ..., .display = TRUE) {
mwControlFactory(
"radio", shiny::radioButtons,
list(choices = choices, value = value, label = label, ...),
prepareParams(choices = choices, value = value, label = label, ...),
valueVar = "selected",
.display = .display
)
Expand Down Expand Up @@ -331,7 +356,7 @@ mwRadio <- function(choices, value = NULL, label = NULL, ..., .display = TRUE) {
mwDate <- function(value = NULL, label = NULL, ..., .display = TRUE) {
mwControlFactory(
"date", shiny::dateInput,
list(value = value, label = label, ...),
prepareParams(value = value, label = label, ...),
.display = .display
)
}
Expand Down Expand Up @@ -365,7 +390,7 @@ mwDateRange <- function(value = c(Sys.Date(), Sys.Date() + 1), label = NULL, ...
.display = TRUE) {
mwControlFactory(
"dateRange", shiny::dateRangeInput,
list(value = value, label = label, ...),
prepareParams(value = value, label = label, ...),
valueVar = c("start", "end"),
.display = .display
)
Expand Down Expand Up @@ -404,7 +429,7 @@ mwDateRange <- function(value = c(Sys.Date(), Sys.Date() + 1), label = NULL, ...
mwCheckboxGroup <- function(choices, value = c(), label = NULL, ..., .display = TRUE) {
mwControlFactory(
"checkboxGroup", shiny::checkboxGroupInput,
list(choices = choices, value = value, label = label, ...),
prepareParams(choices = choices, value = value, label = label, ...),
valueVar = "selected",
.display = .display
)
Expand Down
2 changes: 1 addition & 1 deletion R/controlsUtils.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ getControlDesc <- function(controls) {
initValues <<- append(initValues, value)
types <<- append(types, attr(x, "type"))
groupLevel <<- append(groupLevel, level)
m <- if (is.null(attr(x, "params")$multiple)) NA else attr(x, "params")$multiple
m <- if (is.null(attr(x, "params")$multiple)) NA else eval(attr(x, "params")$multiple)
multiple <<- append(multiple, m)

# Label of the control
Expand Down
12 changes: 8 additions & 4 deletions R/preprocessControls.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
preprocessControls <- function(controls, compare = NULL, update = NULL, env) {
# Initialize object returned by the function
res <- list(
inputs = data.frame(),
desc = data.frame(),
env = list(
shared = new.env(parent = env),
ind = list()
Expand Down Expand Up @@ -161,13 +161,19 @@ preprocessControls <- function(controls, compare = NULL, update = NULL, env) {
res
}

evalParams <- function(params, env) {
lapply(params, function(x) {
tryCatch(eval(x, envir = env), silent = TRUE, error = function(e) {NULL})
})
}

getInitValue <- function(desc) {
type <- desc$type
value <- desc$initValue
params <- desc$params
lapply(seq_along(type), function(i) {
v <- value[[i]]
p <- params[[i]]
p <- evalParams(params[[i]], desc$env[[i]])

if (type[i] == "slider") {
v[v < p$min] <- p$min
Expand Down Expand Up @@ -223,15 +229,13 @@ getInitValue <- function(desc) {
setValueAndParams <- function(controls, desc) {
name <- desc$inputId
initValue <- desc$initValue
params <- desc$params

setValueAndParamsIter <- function(x) {
for (n in names(x)) {
if (is.list(x[[n]])) {
x[[n]] <- setValueAndParamsIter(x[[n]])
} else {
i <- which(name == n)
attr(x[[n]], "params") <- params[[i]]
attr(x[[n]], "params")$value <- initValue[[i]]
}
}
Expand Down

0 comments on commit 0e9346e

Please sign in to comment.