Shiny Module that calls a reactive data set in parent Shiny server?

1,163 views
Skip to first unread message

Mark Edmondson

unread,
Apr 18, 2016, 9:42:03 AM4/18/16
to Shiny - Web Framework for R
Hi there, I have cross posted this to http://stackoverflow.com/questions/36695577/shiny-module-that-calls-a-reactive-data-set-in-parent-shiny-server but just in case anyone here can help:

I'm looking to port some older Shiny apps to use Shiny Modules, but running into trouble trying to port over my reactive expressions.

According to the documentation:

The goal is not to prevent modules from interacting with their containing apps, but rather, to make these interactions explicit. If a module needs to use a reactive expression, take the reactive expression as a function parameter.

I have existing reactive expressions that import data from APIs etc. that I would like to pass in, but can't seem to find the syntax. If I modify the give Shiny module example below I can get to the same problem.

Could anyone modify the below so that you can pass in the car_data() reactive data into the module? I've tried just about every combination of isolate and car_data/car_data() I can think of and am stumped :)

I would prefer to not need to call the data within the module itself, as in my case I'm trying to generalise an ETL function applicable to lots of datasets.


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 modifcation ### 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)

Joe Cheng

unread,
Apr 18, 2016, 3:10:37 PM4/18/16
to Mark Edmondson, Shiny - Web Framework for R
Hi Mark, the code in linkedScatter itself is correct; but when calling callModule, you want to pass the reactive itself by name (car_data) without reading it (car_data()).

callModule(linkedScatter, "scatters", car_data)

This is similar to how you can pass a function by name to something like lapply:

lapply(letters, toupper) # works
lapply(letters, toupper()) # doesn't work

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/69adbc38-e79d-4183-bb17-4dafd1af5139%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mark Edmondson

unread,
Apr 18, 2016, 3:47:41 PM4/18/16
to Shiny - Web Framework for R, m...@markedmondson.me
Thanks Joe :)
Reply all
Reply to author
Forward
0 new messages