Module in Shiny app do not return plot

Hi,
I'm trying to make my first Shiny app using modules.
I faced a problem with the plot generated by the module but returns only "blank space". The same code pasted directly to the ui/server works just fine. Could anyone help me find where my syntax wrong?

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(ggplot2)

#UI module that should create a fluidRow with my plot
mod_home_tab_ui <- function(id){
    ns <- NS(id)
    home_tab = fluidRow(
            shinydashboardPlus::box(
                title = "Motivation",
                icon = shiny::icon("question"),
                collapsible = TRUE,
                plotOutput(outputId = "pubs_pl")
            )
        )

    return(home_tab)
}
#Server module to render plot
mod_home_tab_server <- function(id){
    moduleServer( id, function(input, output, session){
        ns <- session$ns

        output$pubs_pl <- renderPlot({
            ggplot2::ggplot(data = data.frame(x = rnorm(10),
                                              y = rnorm(10)),
                            ggplot2::aes(x = x, y = y))+
                ggplot2::geom_point()})
    })
}
            

ui <- shinydashboardPlus::dashboardPage(

    # Application title
    header = shinydashboardPlus::dashboardHeader(),
    # Sidebar with a slider input for number of bins 
    sidebar = shinydashboardPlus::dashboardSidebar(),

    body = shinydashboard::dashboardBody(
#This does not work
     mod_home_tab_ui("home_tab_ui_1"),

#This works fine
        fluidRow(
            shinydashboardPlus::box(
                title = "Motivation",
                icon = shiny::icon("question"),
                collapsible = TRUE,
                plotOutput(outputId = "pubs_pl2")
            )
        )
    )
        


)

# Define server logic required to draw a histogram
server <- function(input, output) {
# This does not work  
    mod_home_tab_server("home_tab_ui_1")
#This works fine
    output$pubs_pl2 <- renderPlot({
        ggplot2::ggplot(data = data.frame(x = rnorm(10),
                                          y = rnorm(10)),
                        ggplot2::aes(x = x, y = y))+
            ggplot2::geom_point()})

}

# Run the application 
shinyApp(ui = ui, server = server)

Make this change and it should work:

mod_home_tab_ui <- function(id){
  ns <- NS(id)
  fluidRow(
    shinydashboardPlus::box(
      title = "Motivation",
      icon = shiny::icon("question"),
      collapsible = TRUE,
      plotOutput(ns(outputId = "pubs_pl")) # change this bit. ns defined above
    )
  )
}

You can define and return home_tab if you want.

1 Like

Thank you!

In the form you suggested it return error Error in ns(outputId = "pubs_pl") : unused argument (outputId = "pubs_pl"), but I found that it works without outputId = . So the final version will be:

mod_home_tab_ui <- function(id){
  ns <- NS(id)
  fluidRow(
    shinydashboardPlus::box(
      title = "Motivation",
      icon = shiny::icon("question"),
      collapsible = TRUE,
      plotOutput(ns("pubs_pl"))
    )
  )
}
1 Like

In the form you suggested it return error Error in ns(outputId = "pubs_pl") : unused argument (outputId = "pubs_pl") , but I found that it works without outputId = .

yes the more explicit way would be

 plotOutput(outputId = ns("pubs_pl"))
1 Like

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.