Debian Codegraph @

inst/examples/10_download/app.R

raw
1 library(shiny)
2
3 # Define UI for data download app ----
4 ui <- fluidPage(
5
6 # App title ----
7 titlePanel("Downloading Data"),
8
9 # Sidebar layout with input and output definitions ----
10 sidebarLayout(
11
12 # Sidebar panel for inputs ----
13 sidebarPanel(
14
15 # Input: Choose dataset ----
16 selectInput("dataset", "Choose a dataset:",
17 choices = c("rock", "pressure", "cars")),
18
19 # Button
20 downloadButton("downloadData", "Download")
21
22 ),
23
24 # Main panel for displaying outputs ----
25 mainPanel(
26
27 tableOutput("table")
28
29 )
30
31 )
32 )
33
34 # Define server logic to display and download selected file ----
35 server <- function(input, output) {
36
37 # Reactive value for selected dataset ----
38 datasetInput <- reactive({
39 switch(input$dataset,
40 "rock" = rock,
41 "pressure" = pressure,
42 "cars" = cars)
43 })
44
45 # Table of selected dataset ----
46 output$table <- renderTable({
47 datasetInput()
48 })
49
50 # Downloadable csv of selected dataset ----
51 output$downloadData <- downloadHandler(
52 filename = function() {
53 paste(input$dataset, ".csv", sep = "")
54 },
55 content = function(file) {
56 write.csv(datasetInput(), file, row.names = FALSE)
57 }
58 )
59
60 }
61
62 # Create Shiny app ----
63 shinyApp(ui, server)