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)