0

I have a shiny app that takes a module to do the multiple graph and I want to display these graph in multiple different places in the main Shiny UI. For example the below code has one moduleserver function that generate plot1 and plot2 which is also mention in the module ui part. But I dont know how I will call plot1 and plot2 separately.

  • This is module code:
mod_dt_UI <- function(id) {
  ns <- NS(id)
  tagList(
    uiOutput(ns("plot1")),
    uiOutput(ns("plot2")),
    uiOutput(ns("plot3"))
  )
}

mod_dt_Server <- function(id, dsin) {
  moduleServer(
    id,
    function(input, output, session) {

      output$plot1<- renderPlotly({
            Some graph code. 
       })
      output$plot2<- renderPlotly({
            Some graph code. 
       })
      output$plot3<- renderPlotly({
            Some graph code. 
       })
     }
   )
}
  • This is the main App UI and server
ui <- fluidPage(
  mod_dt_UI("plot1")  #These plot will be displayed in different pages in the SHiny app. 
  mod_dt_UI("plot2") #These plot will be displayed in different pages in the SHiny app. 
  mod_dt_UI("plot3")  #These plot will be displayed in different pages in the SHiny app. 
)

# Define the server function
server <- function(input, output, session) {
  # Call the module and assign its output to the placeholder
    mod_dt_Server(id="plot1", dsin=df)
}

Tried to access using mod_dt_UI("plot1") but not able to get what I want. All plots together.

L--
  • 565
  • 1
  • 12
  • Return the required plots from the module server function. Assign the return value to an object in app's main server function and then reference them where you need. Without working code, that's likely to be all we can tell you. See [here](https://stackoverflow.com/questions/70265644/best-practices-for-returning-a-server-side-generated-value-from-a-shiny-module) for an example of how to do this. – Limey Mar 26 '23 at 07:55

1 Answers1

0
  1. Specify a unique ID for each plot with the id argument of renderPlotly

  2. In the mod_dt_UI function change uiOutput to plotOutput, this will create a placeholder for each plot with the same ID from renderPlotly.

Here is an example with the mtcars dataset:

library(shiny)
library(plotly)

# Module UI function
mod_dt_UI <- function(id) {
  ns <- NS(id)
  tagList(
    plotlyOutput(ns("plot1")),
    plotlyOutput(ns("plot2")),
    plotlyOutput(ns("plot3"))
  )
}

# Module server function
mod_dt_Server <- function(id, dsin) {
  moduleServer(
    id,
    function(input, output, session) {
      
      output$plot1 <- renderPlotly({
        # Plot 1
        dsin %>%
          group_by(am) %>%
          summarise(mean_mpg = mean(mpg)) %>%
          plot_ly(x = ~am, y = ~mean_mpg, type = "bar")
      })
      
      output$plot2 <- renderPlotly({
        # Plot 2
        dsin %>%
          group_by(am) %>%
          summarise(mean_hp = mean(hp)) %>%
          plot_ly(x = ~am, y = ~mean_hp, type = "bar")
      })
      
      output$plot3 <- renderPlotly({
        # Plot 3
        dsin %>%
          group_by(am) %>%
          summarise(mean_mpg = mean(mpg), mean_hp = mean(hp)) %>%
          plot_ly(x = ~am, y = ~mean_mpg, type = "bar", name = "Mean MPG") 
      })
      
    }
  )
}


ui <- fluidPage(
  mod_dt_UI("plot1"),
  mod_dt_UI("plot2"),
  mod_dt_UI("plot3")
)


server <- function(input, output, session) {
  
  mod_dt_Server(id = "plot1", dsin = mtcars)
  mod_dt_Server(id = "plot2", dsin = mtcars)
  mod_dt_Server(id = "plot3", dsin = mtcars)
}

# Run the app
shinyApp(ui, server)

example output: enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66