diff --git a/NAMESPACE b/NAMESPACE index 4211a69..12c60d6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,8 @@ importFrom(grDevices,dev.off) importFrom(grDevices,png) importFrom(htmlwidgets,getDependency) importFrom(methods,is) +importFrom(methods,new) +importFrom(methods,setRefClass) importFrom(miniUI,gadgetTitleBar) importFrom(miniUI,miniContentPanel) importFrom(miniUI,miniPage) diff --git a/R/mwServer.R b/R/mwServer.R deleted file mode 100644 index 60a3217..0000000 --- a/R/mwServer.R +++ /dev/null @@ -1,79 +0,0 @@ -#' Private function that returns a shiny server function to use in manipulateWidget -#' -#' @param .expr see manipulateWidget -#' @param controls Object returned by function preprocessControls -#' @param widgets A list of the widgets to show, in their initial state -#' @param renderFunction Function to use to render the widgets -#' @param .display see manipulateWidget -#' @param .compareLayout see manipulateWidget -#' @param .updateBtn see manipulateWidget -#' -#' @return A server function that can be used in runGadget. -#' -#' @noRd -#' -mwServer <- function(.expr, controls, widgets, - renderFunction, - .updateBtn, .return, nrow, ncol, useCombineWidgets) { - - - function(input, output, session) { - message("Click on the 'OK' button to return to the R session.") - # Ensure that initial values of select inputs with multiple = TRUE are in - # same order than the user asked. - selectInputList <- subset(controls$desc, type == "select" & multiple) - for (i in seq_len(nrow(selectInputList))) { - shiny::updateSelectInput( - session, - selectInputList$name[i], - selected = selectInputList$initValues[[i]] - ) - } - - updateModule <- function(i) { - # Initialize the widgets with their first evaluation - if (useCombineWidgets) widgets[[i]] <- combineWidgets(widgets[[i]]) - output[[paste0("output", i)]] <- renderFunction(widgets[[i]]) - - desc <- subset(controls$desc, mod %in% c(0, i)) - - # Set the reactive environment of the modules. envs[[i]] is a reactive - # value containing the module environment. - moduleEnv <- reactive({ - input$.update - - for (j in seq_len(nrow(desc))) { - if (.updateBtn) v <- isolate(input[[desc$inputId[j]]]) - else v <- input[[desc$inputId[j]]] - assign(desc$name[j], v, envir = desc$env[[j]]) - } - controls$env$ind[[i]] - }) - - # Update inputs and widget of the module - observe({ - showHideControls(desc, session, moduleEnv()) - - # Skip first evaluation, since widgets have already been rendered with - # initial parameters - if (get(".initial", envir = moduleEnv())) { - assign(".initial", FALSE, envir = moduleEnv()) - assign(".session", session, envir = moduleEnv()) - } else { - desc <<- updateControls(desc, session, moduleEnv()) - res <- eval(.expr, envir = moduleEnv()) - if (useCombineWidgets) res <- combineWidgets(res) - if (is(res, "htmlwidget")) { - output[[paste0("output", i)]] <- renderFunction(res) - } - } - }) - } - - for (i in seq_len(controls$nmod)) { - updateModule(i) - } - - observeEvent(input$done, onDone(.expr, controls, .return, nrow, ncol)) - } -} diff --git a/R/mwServer_helpers.R b/R/mwServer_helpers.R index e3f7c52..1347cbd 100644 --- a/R/mwServer_helpers.R +++ b/R/mwServer_helpers.R @@ -1,90 +1,3 @@ -#' Dynamically show/hide controls in the UI -#' -#' @param .display expression that evaluates to a named list of boolean -#' @param desc subset of controls$desc containing only shared inputs and inputs -#' for the current module -#' @param session shiny session -#' @param env module environment -#' -#' @noRd -showHideControls <- function(desc, session, env) { - displayBool <- lapply(desc$display, eval, envir = env) - for (i in seq_along(displayBool)) { - if (is.logical(displayBool[[i]])) { - shiny::updateCheckboxInput( - session, - inputId = paste0(desc$inputId[i], "_visible"), - value = displayBool[[i]] - ) - } - } -} - -#' Dynamically set input parameters like choices, minimal or maximal values, etc. -#' -#' @param .updateInputs expression that evaluate to a named list of lists -#' @inheritParams showHideControls -#' -#' @return data.frame 'desc' with updated column params -#' @noRd -updateControls <- function(desc, session, env) { - - for (i in seq_len(nrow(desc))) { - newParams <- evalParams(desc$params[[i]], env) - - args <- list(session = session, inputId = desc$inputId[i]) - updateRequired <- FALSE - - for (p in setdiff(names(newParams), c("value", "label"))) { - if (identical(newParams[[p]], desc$currentParams[[i]][[p]])) { - next - } - - updateRequired <- TRUE - args[[p]] <- newParams[[p]] - - # Special case: update value of select input when choices are modified - if (p == "choices" & desc$type[i] == "select") { - actualSelection <- get(desc$name[i], envir = env) - if (desc$multiple[[i]]) { - args$selected <- intersect(actualSelection, newParams[[p]]) - } else { - if (actualSelection %in% newParams[[p]]) { - args$selected <- actualSelection - } - } - } - - desc$currentParams[[i]][[p]] <- newParams[[p]] - } - - if (updateRequired) { - updateInputFun <- getUpdateInputFun(desc$type[i]) - do.call(updateInputFun, args) - } - } - - desc -} - -#' Private function that returns the function to use to update some type of inputs -#' @noRd -getUpdateInputFun <- function(type) { - switch( - type, - slider = shiny::updateSliderInput, - text = shiny::updateTextInput, - numeric = shiny::updateNumericInput, - password = shiny::updateTextInput, - select = shiny::updateSelectInput, - checkbox = shiny::updateCheckboxInput, - radio = shiny::updateRadioButtons, - date = shiny::updateDateInput, - dateRange = shiny::updateDateRangeInput, - checkboxGroup = shiny::updateCheckboxGroupInput - ) -} - #' Function called when user clicks on the "Done" button. It stops the shiny #' gadget and returns the final htmlwidget #' diff --git a/README.Rmd b/README.Rmd index a3197ba..0ddde97 100644 --- a/README.Rmd +++ b/README.Rmd @@ -95,14 +95,15 @@ myPlotFun <- function(distribution, range, title) { value = randomFun(n = diff(range) + 1) ) combineWidgets( - ncol = 2, colsize = c(2, 1), + ncol = 1, rowsize = c(2, 1), dygraph(myData, main = title), combineWidgets( + ncol = 2, plot_ly(x = myData$value, type = "histogram"), paste( - "The graph on the left represents a random time series generated using a ", + "The graph above represents a random time series generated using a ", distribution, "distribution function.
", - "The chart above represents the empirical distribution of the generated values." + "The chart on the left represents the empirical distribution of the generated values." ) ) ) diff --git a/man/mwCheckbox.Rd b/man/mwCheckbox.Rd index 9da8537..846cd75 100644 --- a/man/mwCheckbox.Rd +++ b/man/mwCheckbox.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwCheckbox} \alias{mwCheckbox} \title{Add a checkbox to a manipulateWidget gadget} diff --git a/man/mwCheckboxGroup.Rd b/man/mwCheckboxGroup.Rd index 4a32251..ae7fc93 100644 --- a/man/mwCheckboxGroup.Rd +++ b/man/mwCheckboxGroup.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwCheckboxGroup} \alias{mwCheckboxGroup} \title{Add a group of checkboxes to a manipulateWidget gadget} diff --git a/man/mwDate.Rd b/man/mwDate.Rd index 84c2c4e..58125b0 100644 --- a/man/mwDate.Rd +++ b/man/mwDate.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwDate} \alias{mwDate} \title{Add a date picker to a manipulateWidget gadget} diff --git a/man/mwDateRange.Rd b/man/mwDateRange.Rd index 93c0673..f99f315 100644 --- a/man/mwDateRange.Rd +++ b/man/mwDateRange.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwDateRange} \alias{mwDateRange} \title{Add a date range picker to a manipulateWidget gadget} diff --git a/man/mwGroup.Rd b/man/mwGroup.Rd index 3c6ee1c..0e3cde3 100644 --- a/man/mwGroup.Rd +++ b/man/mwGroup.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwGroup} \alias{mwGroup} \title{Group inputs in a collapsible box} diff --git a/man/mwNumeric.Rd b/man/mwNumeric.Rd index 7a7fee5..0db2de9 100644 --- a/man/mwNumeric.Rd +++ b/man/mwNumeric.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwNumeric} \alias{mwNumeric} \title{Add a numeric input to a manipulateWidget gadget} diff --git a/man/mwPassword.Rd b/man/mwPassword.Rd index 4d19461..567938f 100644 --- a/man/mwPassword.Rd +++ b/man/mwPassword.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwPassword} \alias{mwPassword} \title{Add a password to a manipulateWidget gadget} diff --git a/man/mwRadio.Rd b/man/mwRadio.Rd index 2f9972e..de9e7f6 100644 --- a/man/mwRadio.Rd +++ b/man/mwRadio.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwRadio} \alias{mwRadio} \title{Add radio buttons to a manipulateWidget gadget} diff --git a/man/mwSelect.Rd b/man/mwSelect.Rd index 7d3a197..9287a25 100644 --- a/man/mwSelect.Rd +++ b/man/mwSelect.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwSelect} \alias{mwSelect} \title{Add a Select list input to a manipulateWidget gadget} diff --git a/man/mwSlider.Rd b/man/mwSlider.Rd index fe74512..ace6622 100644 --- a/man/mwSlider.Rd +++ b/man/mwSlider.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwSlider} \alias{mwSlider} \title{Add a Slider to a manipulateWidget gadget} diff --git a/man/mwText.Rd b/man/mwText.Rd index c739398..a405fd7 100644 --- a/man/mwText.Rd +++ b/man/mwText.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/controls.R +% Please edit documentation in R/inputs.R \name{mwText} \alias{mwText} \title{Add a text input to a manipulateWidget gadget}