Brilliant module that invokes a reactive dataset in the parent Brilliant server

I'm going to port some old Shiny applications to use Shiny Modules, but have run into difficulties trying to override my reactive expressions.

According to the documentation :

The goal is not to block the interaction of modules with them, but rather, to make these interactions explicit. If the module must use a reactive expression, accept the reactive expression as a function parameter.

I have active reactive expressions that import data from an API, etc. that I would like to pass but cannot find the syntax. If I change this example of the Shiny module below below, I can move on to the same problem.

Can someone change the following so that you can pass reactive car_data() data to the module? I tried almost every combination of isolate and car_data/car_data() , which I can think of and is stunned :)

I would prefer not to require the data to be called inside the module itself, since in my case I am trying to generalize the ETL function applicable to many data sets.

 library(shiny) library(ggplot2) linkedScatterUI <- function(id) { ns <- NS(id) fluidRow( column(6, plotOutput(ns("plot1"), brush = ns("brush"))), column(6, plotOutput(ns("plot2"), brush = ns("brush"))) ) } linkedScatter <- function(input, output, session, data, left, right) { # Yields the data frame with an additional column "selected_" # that indicates whether that observation is brushed dataWithSelection <- reactive({ brushedPoints(data(), input$brush, allRows = TRUE) }) output$plot1 <- renderPlot({ scatterPlot(dataWithSelection(), left()) }) output$plot2 <- renderPlot({ scatterPlot(dataWithSelection(), right()) }) return(dataWithSelection) } scatterPlot <- function(data, cols) { ggplot(data, aes_string(x = cols[1], y = cols[2])) + geom_point(aes(color = selected_)) + scale_color_manual(values = c("black", "#66D65C"), guide = FALSE) } ui <- fixedPage( h2("Module example"), linkedScatterUI("scatters"), textOutput("summary") ) server <- function(input, output, session) { ### My modification ### making the reactive outside of module call car_data <- reactive({ mpg }) ## This doesn't work ## What is the syntax for being able to call car_data()? df <- callModule(linkedScatter, "scatters", car_data(), left = reactive(c("cty", "hwy")), right = reactive(c("drv", "hwy")) ) output$summary <- renderText({ sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_))) }) } shinyApp(ui, server) 
+7
module r shiny shinyapps
source share
2 answers

Drop the parsers after car_data:

 df <- callModule(linkedScatter, "scatters", car_data, left = reactive(c("cty", "hwy")), right = reactive(c("drv", "hwy")) ) 

The module seems to want β€œunresolved” reagents. The brackets "solve" them.

+4
source share

If you want to pass input that is not part of the module, just wrap it around reactive() , as described in the tutorial .

If the module must access an input that is not part of the module, the containing application must pass the input value wrapped in a reactive (that is, reactive (...)): callModule(myModule, "myModule1", reactive(input$checkbox1))

Update: As another answer correctly and Joe Cheng said, the correct way to pass a reactive expression without parentheses ()

callModule(linkedScatter, "scatters", car_data)

One option is also to modulate the API input function, so you do not need to define the reactive expression of external modules. An example of modular input can be found from this. Below is your code with the correct answer.

 library(shiny) library(ggplot2) linkedScatterUI <- function(id) { ns <- NS(id) fluidRow( column(6, plotOutput(ns("plot1"), brush = ns("brush"))), column(6, plotOutput(ns("plot2"), brush = ns("brush"))) ) } linkedScatter <- function(input, output, session, data, left, right) { # Yields the data frame with an additional column "selected_" # that indicates whether that observation is brushed dataWithSelection <- reactive({ brushedPoints(data(), input$brush, allRows = TRUE) }) output$plot1 <- renderPlot({ scatterPlot(dataWithSelection(), left()) }) output$plot2 <- renderPlot({ scatterPlot(dataWithSelection(), right()) }) return(dataWithSelection) } scatterPlot <- function(data, cols) { ggplot(data, aes_string(x = cols[1], y = cols[2])) + geom_point(aes(color = selected_)) + scale_color_manual(values = c("black", "#66D65C"), guide = FALSE) } ui <- fixedPage( h2("Module example"), linkedScatterUI("scatters"), textOutput("summary") ) server <- function(input, output, session) { data(mpg) ### My modification ### making the reactive outside of module call car_data <- reactive({ mpg }) ## This doesn't work ## What is the syntax for being able to call car_data()? df <- callModule(linkedScatter, "scatters", reactive(car_data), left = reactive(c("cty", "hwy")), right = reactive(c("drv", "hwy")) ) output$summary <- renderText({ sprintf("%d observation(s) selected", nrow(dplyr::filter(df(), selected_))) }) } shinyApp(ui, server) 
+3
source share

All Articles