Not able to get the data from module to Shiny App

Hi Good Morning
I am working with Shiny app and a module. Module gets the data and does certain data cleansing and does certain plot. upto that it is fine. but I would like to get that processed data from the module to Shiny app. I am not able to get that. Could someone help me. given below minimum reproducible code (thanks - code taken from: Chapter 19 Shiny modules | Mastering Shiny)

library(shiny)

histogramUI <- function(id) {
  ns <- NS(id)
  tagList(
    selectInput(ns("var"), "Variable", choices = names(mtcars)),
    plotOutput(ns("hist"))
  )
}

###############################################

histogramServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    data <- reactive(mtcars[[input$var]])
    output$hist <- renderPlot({
      hist(data(), breaks = 20, main = input$var,col='lightblue')
    })
  })
  return('mydata'=reactive(data()))
}

###############################################
ui <- fluidPage(
  histogramUI("hist1")
)
server <- function(input, output, session) {
  histogramServer("hist1")
}
shinyApp(ui, server)  
###############################################


library(shiny)

histogramUI <- function(id) {
  ns <- NS(id)
  tagList(
    selectInput(ns("var"), "Variable", choices = names(mtcars)),
    plotOutput(ns("hist"))
  )
}

###############################################

histogramServer <- function(id) {
  moduleServer(id, function(input, output, session) {
    data <- reactive(mtcars[[input$var]])
    output$hist <- renderPlot({
      hist(data(), breaks = 20, main = input$var, col = "lightblue")
    })

   #  return(list(mydata=reactive(data())))
    # pass it without unwrap is the same as unwrapping it and wrerapping like previous line
    return(list(mydata=data)) 
  })
}

###############################################
ui <- fluidPage(
  histogramUI("hist1"),
  verbatimTextOutput("res_out_1")
)
server <- function(input, output, session) {
  
  result_of_hist_1 <- histogramServer("hist1")

  output$res_out_1 <- renderText({
    rh1 <- req(result_of_hist_1)
    d <- req(rh1$mydata())
    paste0(capture.output(str(d)), collapse = "\n")
  })
}
shinyApp(ui, server)
###############################################
1 Like

Excellent, Thank you very much, it works perfectly in my application. In fact, the modules gets dataset as reactive value and does certain data cleansing processes. at the end the shiny app gets the processed / cleansed data from the module. Thanks a lot for your suggestion & support

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.