Debian Codegraph @

inst/examples-shiny/10_download/app.R

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