diff --git a/.Rbuildignore b/.Rbuildignore index 165a9b9..de4340d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -8,3 +8,4 @@ README_files ^\.travis\.yml$ ^codecov\.yml$ newUI +^inst/examples diff --git a/R/controller.R b/R/controller.R index 869479d..d325477 100644 --- a/R/controller.R +++ b/R/controller.R @@ -92,6 +92,7 @@ MWController <- setRefClass( }, setShinySession = function(output, session) { + catIfDebug("Set shiny session") session <<- session shinyOutput <<- output inputList$session <<- session @@ -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]]) } @@ -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()) diff --git a/inst/examples/reactive_values.R b/inst/examples/reactive_values.R new file mode 100644 index 0000000..8c6b981 --- /dev/null +++ b/inst/examples/reactive_values.R @@ -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) + + + + + + diff --git a/inst/examples/two_modules_one_app.R b/inst/examples/two_modules_one_app.R new file mode 100644 index 0000000..3dd89f8 --- /dev/null +++ b/inst/examples/two_modules_one_app.R @@ -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)