From 7d55959bd11be2de4dd016cf1c2c75c05c1e43d5 Mon Sep 17 00:00:00 2001 From: Benoit Thieurmel Date: Mon, 30 Oct 2017 15:06:12 +0100 Subject: [PATCH] add .updateInitBtn argument --- R/controller.R | 14 +++++++------- R/manipulate_widget.R | 4 +++- man/MWController-class.Rd | 2 +- man/manipulateWidget.Rd | 9 ++++++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/R/controller.R b/R/controller.R index b91ca63..b1d6917 100644 --- a/R/controller.R +++ b/R/controller.R @@ -41,7 +41,7 @@ #' @field nrow Number of rows. #' @field ncol Number of columns. #' @field autoUpdate Boolean indicating if charts should be automatically -#' updated when a value changes +#' updated when a value changes. list with \code{value} and \code{initBtn} (not autoUpdate, if want first charts on init) #' #' @export MWController <- setRefClass( @@ -51,7 +51,7 @@ MWController <- setRefClass( "returnFunc", "initialized"), methods = list( - initialize = function(expr, inputs, autoUpdate = TRUE, nrow = NULL, + initialize = function(expr, inputs, autoUpdate = list(value = TRUE, initBtn = FALSE), nrow = NULL, ncol = NULL, returnFunc = function(widget, envs) {widget}) { expr <<- expr inputList <<- inputs$inputList @@ -119,7 +119,7 @@ MWController <- setRefClass( oldValue <- getValue(name, chartId) newValue <- inputList$setValue(name, value, chartId, reactive = reactive) if (!initialized) return() - if (autoUpdate && !identical(oldValue, newValue)) { + if (autoUpdate$value && !identical(oldValue, newValue)) { if (inputList$isShared(name)) updateCharts() else updateChart(chartId) } @@ -129,7 +129,7 @@ MWController <- setRefClass( oldValue <- getValueById(id) newValue <- inputList$setValue(inputId = id, value = value) if (!initialized) return() - if (autoUpdate && !identical(oldValue, newValue)) { + if (autoUpdate$value && !identical(oldValue, newValue)) { if (grepl("^shared_", id)) updateCharts() else { chartId <- get(".id", envir = inputList$inputs[[id]]$env) @@ -220,7 +220,7 @@ MWController <- setRefClass( function(ns, okBtn = gadget, width = "100%", height = "400px") { #ns <- shiny::NS(id) mwUI(ns, uiSpec, nrow, ncol, outputFunc, - okBtn = okBtn, updateBtn = !autoUpdate, saveBtn = saveBtn, + okBtn = okBtn, updateBtn = !autoUpdate$value, saveBtn = saveBtn, areaBtns = length(uiSpec$inputs$ind) > 1, border = addBorder, width = width, height = height) } @@ -248,7 +248,7 @@ MWController <- setRefClass( input$updateHTML(session) } }) - if (autoUpdate) renderShinyOutputs() + if (autoUpdate$value) renderShinyOutputs() }, error = function(e) {catIfDebug("Initialization error"); print(e)}) }, @@ -283,7 +283,7 @@ MWController <- setRefClass( } }) - observeEvent(input$.update, controller$updateCharts()) + observeEvent(input$.update, controller$updateCharts(), ignoreNULL = !autoUpdate$initBtn) observeEvent(input$done, onDone(controller)) output$save <- shiny::downloadHandler( diff --git a/R/manipulate_widget.R b/R/manipulate_widget.R index 0ff8dde..7a7e7c5 100644 --- a/R/manipulate_widget.R +++ b/R/manipulate_widget.R @@ -26,6 +26,7 @@ #' \code{TRUE}, then the graphic is updated only when the user clicks on the #' update button. #' @param .saveBtn Should an save button be added to the controls ? +#' @param .updateBtnInit In case of update button. Do you want to render graphics on init ? #' @param .viewer Controls where the gadget should be displayed. \code{"pane"} #' corresponds to the Rstudio viewer, \code{"window"} to a dialog window, and #' \code{"browser"} to an external web browser. @@ -221,6 +222,7 @@ #' @export #' manipulateWidget <- function(.expr, ..., .updateBtn = FALSE, .saveBtn = TRUE, + .updateBtnInit = FALSE, .viewer = c("pane", "window", "browser"), .compare = NULL, .compareOpts = compareOptions(), @@ -254,7 +256,7 @@ manipulateWidget <- function(.expr, ..., .updateBtn = FALSE, .saveBtn = TRUE, inputs <- initInputs(list(...), env = .env, compare = .compare, ncharts = .compareOpts$ncharts) # Initialize controller - controller <- MWController(.expr, inputs, autoUpdate = !.updateBtn, + controller <- MWController(.expr, inputs, autoUpdate = list(value = !.updateBtn, initBtn = .updateBtnInit), nrow = dims$nrow, ncol = dims$ncol, returnFunc = .return) diff --git a/man/MWController-class.Rd b/man/MWController-class.Rd index dc3a175..ade349d 100644 --- a/man/MWController-class.Rd +++ b/man/MWController-class.Rd @@ -21,7 +21,7 @@ desire to create automatic tests for applications created with \item{\code{ncol}}{Number of columns.} \item{\code{autoUpdate}}{Boolean indicating if charts should be automatically -updated when a value changes} +updated when a value changes. list with \code{value} and \code{initBtn} (not autoUpdate, if want first charts on init)} }} \section{Methods}{ diff --git a/man/manipulateWidget.Rd b/man/manipulateWidget.Rd index 6c2fefa..0a8a211 100644 --- a/man/manipulateWidget.Rd +++ b/man/manipulateWidget.Rd @@ -5,9 +5,10 @@ \title{Add Controls to Interactive Plots} \usage{ manipulateWidget(.expr, ..., .updateBtn = FALSE, .saveBtn = TRUE, - .viewer = c("pane", "window", "browser"), .compare = NULL, - .compareOpts = compareOptions(), .return = function(widget, envs) { - widget }, .width = NULL, .height = NULL, .runApp = TRUE) + .updateBtnInit = FALSE, .viewer = c("pane", "window", "browser"), + .compare = NULL, .compareOpts = compareOptions(), + .return = function(widget, envs) { widget }, .width = NULL, + .height = NULL, .runApp = TRUE) } \arguments{ \item{.expr}{expression to evaluate that returns an interactive plot of class @@ -28,6 +29,8 @@ update button.} \item{.saveBtn}{Should an save button be added to the controls ?} +\item{.updateBtnInit}{In case of update button. Do you want to render graphics on init ?} + \item{.viewer}{Controls where the gadget should be displayed. \code{"pane"} corresponds to the Rstudio viewer, \code{"window"} to a dialog window, and \code{"browser"} to an external web browser.}