Skip to content

Commit

Permalink
manipulateWidget now returns an uninitialized controller in non inter…
Browse files Browse the repository at this point in the history
…active situations
  • Loading branch information
FrancoisGuillem committed Aug 4, 2017
1 parent c0fe531 commit 9e1b7c5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 4 additions & 0 deletions R/controller.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ MWController <- setRefClass(

init = function() {
if (!initialized) {
initialized <<- TRUE
inputList$init()
updateCharts()
if (is.null(renderFunc) || is.null(outputFunc) || is.null(useCombineWidgets)) {
Expand Down Expand Up @@ -112,6 +113,7 @@ MWController <- setRefClass(
"Update the value of a variable for a given chart."
oldValue <- getValue(name, chartId)
newValue <- inputList$setValue(name, value, chartId)
if (!initialized) return()
if (autoUpdate && !isTRUE(all.equal(oldValue, newValue))) {
if (inputList$isShared(name)) updateCharts()
else updateChart(chartId)
Expand All @@ -121,6 +123,7 @@ MWController <- setRefClass(
setValueById = function(id, value) {
oldValue <- getValueById(id)
newValue <- inputList$setValue(inputId = id, value = value)
if (!initialized) return()
if (autoUpdate && !isTRUE(all.equal(oldValue, newValue))) {
if (grepl("^shared_", id)) updateCharts()
else {
Expand Down Expand Up @@ -268,5 +271,6 @@ cloneEnv <- function(env, parentEnv = parent.env(env)) {
#'
#' @export
knit_print.MWController <- function(x, ...) {
x$init()
knitr::knit_print(x$returnCharts(), ...)
}
12 changes: 8 additions & 4 deletions R/manipulate_widget.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ manipulateWidget <- function(.expr, ..., .updateBtn = FALSE, .saveBtn = TRUE,
controller <- MWController(.expr, inputs, autoUpdate = !.updateBtn,
nrow = dims$nrow, ncol = dims$ncol,
returnFunc = .return)
controller$init()

mwModuleInput <- controller$getModuleUI(gadget = !isRuntimeShiny, saveBtn = .saveBtn)
mwModule <- controller$getModuleServer()

if (.runApp & interactive()) {
# We are in an interactive session so we start a shiny gadget
controller$init()
mwModuleInput <- controller$getModuleUI(gadget = TRUE, saveBtn = .saveBtn)
mwModule <- controller$getModuleServer()

.viewer <- switch(
.viewer,
pane = shiny::paneViewer(),
Expand All @@ -279,6 +279,10 @@ manipulateWidget <- function(.expr, ..., .updateBtn = FALSE, .saveBtn = TRUE,
shiny::runGadget(ui, server, viewer = .viewer)
} else if (.runApp & isRuntimeShiny) {
# We are in Rmarkdown document with shiny runtime. So we start a shiny app
controller$init()
mwModuleInput <- controller$getModuleUI(gadget = FALSE, saveBtn = .saveBtn)
mwModule <- controller$getModuleServer()

ui <- mwModuleInput("ui", height = "100%")
server <- function(input, output, session, ...) {
controller <- shiny::callModule(mwModule, "ui")
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-controller.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,19 @@ describe("MWController", {
expect_is(server, "function")
expect_equal(names(formals(server)), c("input", "output", "session", "..."))
})

it("does not update values or create charts until it is initialized", {
inputs <- initInputs(list(x = mwSlider(0, 10, 5), y = mwSlider(x, 10, 0)))
expr <- expression(paste(x, y))
controller <- MWController(expr, inputs)
expect_length(controller$charts, 0)
expect_equal(controller$getValue("y"), 0)
controller$setValue("x", 3)
expect_length(controller$charts, 0)
expect_equal(controller$getValue("y"), 0)
controller$init()
expect_length(controller$charts, 1)
expect_equal(controller$charts[[1]]$widgets[[1]], "3 3")
expect_equal(controller$getValue("y"), 3)
})
})
18 changes: 18 additions & 0 deletions tests/testthat/test-manipulate_widget.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
context("manipulateWidget")

describe("manipulateWidget", {
it("returns an uninitialized MWController in a non interactive situation", {
c <- manipulateWidget(
paste(a, b),
a = mwSelect(c("a", "b", "c")),
b = mwText("test"),
.compare = "a"
)
expect_true(!c$initialized)
})

it("creates two charts when .compare is a character vector", {
c <- manipulateWidget(
paste(a, b),
a = mwSelect(c("a", "b", "c")),
b = mwText("test"),
.compare = "a"
)
c$init()
expect_equal(c$ncharts, 2)
expect_equal(c$getValue("a", 1), "a")
expect_equal(c$getValue("a", 2), "a")
Expand All @@ -20,6 +31,7 @@ describe("manipulateWidget", {
b = mwText("test"),
.compare = list(a = NULL)
)
c$init()
expect_equal(c$ncharts, 2)
expect_equal(c$getValue("a", 1), "a")
expect_equal(c$getValue("a", 2), "a")
Expand All @@ -32,6 +44,7 @@ describe("manipulateWidget", {
b = mwText("test"),
.compare = list(a = list("a", "b"))
)
c$init()
expect_equal(c$ncharts, 2)
expect_equal(c$getValue("a", 1), "a")
expect_equal(c$getValue("a", 2), "b")
Expand All @@ -47,6 +60,7 @@ describe("manipulateWidget", {
.compare = list(a = list("a", "b", "c")),
.compareOpts = compareOptions(ncharts = 3)
)
c$init()
expect_equal(c$ncharts, 3)
expect_equal(c$getValue("a", 1), "a")
expect_equal(c$getValue("a", 2), "b")
Expand All @@ -62,6 +76,7 @@ describe("manipulateWidget", {
x = mwSlider(0, 10, 5),
y = mwSlider(0, x, 4)
)
c$init()
expect_equal(c$getParams("y")$max, 5)
c$setValue("x", 3)
expect_equal(c$getParams("y")$max, 3)
Expand All @@ -74,6 +89,7 @@ describe("manipulateWidget", {
x = mwSlider(0, 10, 0),
y = mwSlider(0, 10, 0, .display = x < 5)
)
c$init()
expect_true(c$isVisible("y"))
c$setValue("x", 6)
expect_true(!c$isVisible("y"))
Expand All @@ -86,6 +102,7 @@ describe("manipulateWidget", {
x2 = mwSharedValue(x * 2),
y = mwSlider(0, x2, 0)
)
c$init()
expect_equal(c$getParams("y")$max, 10)
expect_equal(c$charts[[1]]$widgets[[1]], 10)
c$setValue("x", 8)
Expand All @@ -103,6 +120,7 @@ describe("manipulateWidget", {
x3 = mwSharedValue(x + x2),
y = mwSlider(0, x2, 0)
)
c$init()
expect_equal(c$getParams("y")$max, 1)
expect_equal(c$charts[[1]]$widgets[[1]], 1)
c$setValue("x2", 8)
Expand Down

0 comments on commit 9e1b7c5

Please sign in to comment.