Link excel database with downloadLink in Shiny

后端 未结 1 1908
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 10:43

Could you help me link an excel database to my downloadLink? Therefore, whenever I click on \"Download the standard base\" in shiny, an excel database is automatically downloade

1条回答
  •  孤城傲影
    2021-01-23 11:17

    library(shiny)
    #install.packages("xlsx", dependencies = TRUE)
    library(xlsx)
    ui <- fluidPage(
    
        titlePanel("Old Faithful Geyser Data"),
        sidebarLayout(
            sidebarPanel(
    
                sliderInput("bins",
                            "Number of bins:",
                            min = 1,
                            max = 50,
                            value = 30),
                downloadButton("downloadData", "Download Standard Database"),
            ),
    
            mainPanel(
                plotOutput("distPlot")
            )
        )
    )
    
    server <- function(input, output) {
    
        output$distPlot <- renderPlot({
            x    <- faithful[, 2]
            bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
            hist(x, breaks = bins, col = 'darkgray', border = 'white')
        })
    
    
        # Reactive value for selected dataset ----
        datasetInput <- reactive({
            switch(faithful,
                   "eruptions" = eruptions,
                   "waiting" = waiting)
        })
    
        # Table of selected dataset ----
        output$table <- renderTable({
            faithful
        })
    
        # Downloadable csv of selected dataset ----
        output$downloadData <- downloadHandler(
            filename = function() {
                paste0(deparse(substitute(faithful)), ".xlsx")
            },
            content = function(file) {
                write.xlsx(as.data.frame(faithful), file, 
                           sheetName = deparse(substitute(faithful)),
                           row.names = FALSE)
            }
        )
    }
    
    shinyApp(ui = ui, server = server)
    

    0 讨论(0)
提交回复
热议问题