Shiny Module - Data Table doesn't show up

I'm starting to build out more complex Shiny apps and am trying to figure out modules to help with my workflow. I have build some smaller modules that work, but this current one has the page formatting I want, but the desired table doesn't appear when I run the module. What am I missing?


library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(nycflights13)

df <- nycflights13::flights


# Flight module ----
depart_ui <- function(id) {
  tagList (
    selectInput (NS(id, "depart"), "Departure", choices = c("All", sort(unique(df$origin))))
  )
  
}

dest_ui <- function(id) {
  tagList (
    selectInput (NS(id, "dest"), "Destination", choices = c("All", sort(unique(df$dest))))
  )
  
}

flight_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    # data <- reactive (df %>% 
    #                     select (year, month, day, origin, dest) %>% 
    #                     dplyr::filter (origin == input$depart) %>% 
    #                     dplyr::filter (origin == input$dest) 
    #                   )
    data <- reactive (df)
    output$flights <- DT::renderDataTable({
      data()
    })
  })
  
}

flight_app <- function() {
  ui <- fluidPage(
    fluidRow (
      HTML("<h2><center>Origins and Destinations</center></h2>")
    ),
    fluidRow(
      column(
        width = 6, 
        HTML("The following table shows the flight origins and destinations. ")
      ),
      column (
        depart_ui("depart"),
        width = 2
      ),
      column (
        dest_ui("dest"),
        width = 2
      )
    ),
    fluidRow(
      dataTableOutput("flights")
    )
  )
  
  server <- function(input, output, session) {
    flight_server("flights")
  }
  shinyApp(ui, server)  
}


flight_app ()

The problem seems to be that in the UI you are missing the namespace in the dataTableOutput. Furthermore dataTableOutput exists in package shiny and DT but only in the server you are explicitly using DT.
This is the updated code

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(nycflights13)
library(DT)

df <- nycflights13::flights


# Flight module ----
depart_ui <- function(id) {
    tagList (
        selectInput (NS(id, "depart"), "Departure", choices = c("All", sort(unique(df$origin))))
    )
    
}

dest_ui <- function(id) {
    tagList (
        selectInput (NS(id, "dest"), "Destination", choices = c("All", sort(unique(df$dest))))
    )
    
}

flight_server <- function(id) {
    moduleServer(id, function(input, output, session) {
        # data <- reactive (df %>% 
        #                     select (year, month, day, origin, dest) %>% 
        #                     dplyr::filter (origin == input$depart) %>% 
        #                     dplyr::filter (origin == input$dest) 
        #                   )
        data <- reactive (df)
        output$flights <- DT::renderDataTable({
            data()
        })
    })
    
}

flight_app <- function() {
    ui <- fluidPage(
        fluidRow (
            HTML("<h2><center>Origins and Destinations</center></h2>")
        ),
        fluidRow(
            column(
                width = 6, 
                HTML("The following table shows the flight origins and destinations. ")
            ),
            column (
                depart_ui("depart"),
                width = 2
            ),
            column (
                dest_ui("dest"),
                width = 2
            )
        ),
        fluidRow(
            DT::dataTableOutput(NS("flights", "flights"))
        )
    )
    
    server <- function(input, output, session) {
        flight_server("flights")
    }
    shinyApp(ui, server)  
}
flight_app ()

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.