Skip to content

Commit

Permalink
Module server function can now accept reactive values as arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisGuillem committed Aug 4, 2017
1 parent ab71d7c commit b86ec65
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ README_files
^\.travis\.yml$
^codecov\.yml$
newUI
^inst/examples
16 changes: 13 additions & 3 deletions R/controller.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ MWController <- setRefClass(
},

setShinySession = function(output, session) {
catIfDebug("Set shiny session")
session <<- session
shinyOutput <<- output
inputList$session <<- session
Expand Down Expand Up @@ -151,7 +152,8 @@ MWController <- setRefClass(

updateChart = function(chartId = 1) {
catIfDebug("Update chart", chartId)
charts[[chartId]] <<- eval(expr, envir = envs[[chartId]])
e <- new.env(parent = envs[[chartId]]) # User can set values in expr without messing environments
charts[[chartId]] <<- eval(expr, envir = e)
if (useCombineWidgets) {
charts[[chartId]] <<- combineWidgets(charts[[chartId]])
}
Expand Down Expand Up @@ -240,10 +242,18 @@ MWController <- setRefClass(
controller$setShinySession(output, session)
controller$renderShinyOutputs()

# message("Click on the 'OK' button to return to the R session.")
reactiveValueList <- list(...)
print(names(reactiveValueList))
observe({
for (n in names(reactiveValueList)) {
controller$setValue(n, reactiveValueList[[n]]())
}
})

lapply(names(controller$inputList$inputs), function(id) {
observe(controller$setValueById(id, value = input[[id]]))
if (controller$inputList$inputs[[id]]$type != "sharedValue") {
observe(controller$setValueById(id, value = input[[id]]))
}
})

observeEvent(input$.update, controller$updateCharts())
Expand Down
51 changes: 51 additions & 0 deletions inst/examples/reactive_values.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
mydata <- data.frame(
year = 2000+1:100,
series1 = rnorm(100),
series2 = rnorm(100),
series3 = rnorm(100)
)

c <- manipulateWidget(
{
print(title)
if (is.null(series)) series <- "series1"
if (is.null(title)) title <- ""
dygraph(mydata[range[1]:range[2] - 2000, c("year", series)], main = title)
},
range = mwSlider(2001, 2100, c(2001, 2100)),
series = mwSharedValue(),
title = mwSharedValue(), .runApp = FALSE
)$init()

mwModuleInput <- c$getModuleUI(gadget = FALSE, saveBtn = TRUE)
mwModule <- c$getModuleServer()

ui <- fillPage(
fillRow(
flex = c(NA, 1),
div(
textInput("title", label = "Title", value = "glop"),
selectInput("series", "series", choices = c("series1", "series2", "series3"))
),
mwModuleInput("ui")
#uiOutput("ui", container = function(...) tags$div(style="height:100%;", ...))
)
)

server <- function(input, output, session) {
#
# c$init()
# mwModuleInput <- c$getModuleUI(gadget = FALSE, saveBtn = TRUE)
# mwModule <- c$getModuleServer()
# id <- paste0("mwModule_", sample(1e9, 1))
# output$ui <- renderUI(mwModuleInput(id, height = "100%"))
callModule(mwModule, "ui", series = reactive(input$series), title = reactive(input$title))
}

shinyApp(ui, server)






62 changes: 62 additions & 0 deletions inst/examples/two_modules_one_app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
mydata <- data.frame(
year = 2000+1:100,
series1 = rnorm(100),
series2 = rnorm(100),
series3 = rnorm(100)
)

c <- manipulateWidget(
combineWidgets(dygraph(mydata[range[1]:range[2] - 2000, c("year", series)], main = title)),
range = mwSlider(2001, 2100, c(2001, 2100)),
series = mwSelect(c("series1", "series2", "series3")),
title = mwText("Fictive time series"),
.compare = c("title", "series"), .runApp = FALSE
)$init()

dt <- data.frame (
x = sort(runif(100)),
y = rnorm(100)
)

myPlot <- function(type, lwd) {
if (type == "points") {
plot_ly(dt, x= ~x, y = ~y, type = "scatter", mode = "markers")
} else {
plot_ly(dt, x= ~x, y = ~y, type = "scatter", mode = "lines", line = list(width = lwd))
}
}

c2 <- manipulateWidget(
combineWidgets(myPlot(type, lwd)),
type = mwSelect(c("points", "lines"), "points"),
lwd = mwSlider(1, 10, 1, .display = type == "lines"), .runApp = FALSE
)$init()

mwModuleInput <- c$getModuleUI(gadget = FALSE, saveBtn = TRUE)
mwModule <- c$getModuleServer()

mwModuleInput2 <- c2$getModuleUI(gadget = FALSE, saveBtn = TRUE)
mwModule2 <- c2$getModuleServer()

ui <- fillPage(
fillRow(
tags$div(mwModuleInput("pane1"), style = 'height:100%;'),
tags$div(mwModuleInput2("pane2"), style = 'height:100%;')
)
)

ui <- navbarPage("antaresViz",
tabPanel("prodStack",
tags$div(mwModuleInput("pane1"), style = 'height:800px;')
),
tabPanel("exchangesStack",
tags$div(mwModuleInput2("pane2"), style = 'height:800px;')
),
tabPanel("Table")
)
server <- function(input, output, session) {
callModule(mwModule, "pane1")
callModule(mwModule2, "pane2")
}

shinyApp(ui, server)

0 comments on commit b86ec65

Please sign in to comment.