Appendix A — Shiny example

Wait a minute or two for it to load in your browser…

Here’s a dataset for you to try the app with:

#| standalone: true
#| viewerHeight: 500
#| components: [viewer]
webr::install("DT")

library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("Upload CSV and Display as DT Table"),

  sidebarLayout(
    sidebarPanel(
      fileInput("file", "Choose CSV File",
                accept = c(
                  "text/csv",
                  "text/comma-separated-values,text/plain",
                  ".csv")
      ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),

    mainPanel(
      DTOutput("table")
    )
  )
)
server <- function(input, output) {
  output$table <- renderDT({
    inFile <- input$file

    if (is.null(inFile)) {
      return(NULL)
    }

    # Read the uploaded CSV
    data <- read.csv(inFile$datapath, header = input$header)

    # Return the datatable
    datatable(data, options = list(pageLength = 25))
  })
}
shinyApp(ui, server)

Click here to go back to the previous code book section.