1
votes

Getting started with shiny, I try to learn how to use the Shiny Module design pattern. As the most simple example, I want to display a dataset without any further interaction.

I wish to organise the UI in tabPanels of a navbarPage. Each panel is independent from each other, (except that all panels use a global database connection objects, but this does not bother me now).

Here is the code for the DT GUI element:

library(shiny)
library(DT)

tabTable <- function(id) {
    ns <- shiny::NS(id)
    tabPanel(
        "Table",
        shiny::dataTableOutput(ns("table"))
    )
}

This is the server logic. I want it to draw the data table.

srvTable <- function(id, dat) shiny::moduleServer(id,
    function(input, output, session) {
        output$table <- shiny::renderDataTable({DT::datatable(dat)})
    }
)

Now here is the definition of the ui and the server:

ui <- shiny::navbarPage(title="Test",
    tabTable(id="iris"),
    shiny::tabPanel(title="Scatter")
)

server <- function(input, output, session) {
    srvTable(id="iris", dat=iris)
    session$onSessionEnded(stopApp)
}

shiny::shinyApp(ui, server)

This app starts, it displays the navigation bar, but it does not show the dataset. Any hint where I put the command for that? The problem for me is that there is no obvious condition that the server module needs to react to. What is to do in this case?

1

1 Answers

1
votes

Try this

library(shiny)
library(DT)

tabTable <- function(id) {
  ns <- shiny::NS(id)
  tabPanel(
    "Table",
    DTOutput(ns("table"))
  )
}

srvTable <- function(id, dat) { shiny::moduleServer(id,
                                                  function(input, output, session) {
                                                    output$table <- renderDT({DT::datatable(dat)})
                                                  }
)}

ui <- shiny::navbarPage(title="Test",
                        tabTable(id="iris"),
                        shiny::tabPanel(title="Scatter")
)

server <- function(input, output, session) {
  srvTable(id="iris", dat=iris)
  session$onSessionEnded(stopApp)
}

shiny::shinyApp(ui, server)