0

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 downloaded. I made a minimal example below just to show the idea. I don't know how to adjust it on my server for this to work correctly.

Thank you!

library(shiny)

ui <- fluidPage(

    titlePanel("Old Faithful Geyser Data"),
    sidebarLayout(
        sidebarPanel(

            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30),
            downloadLink("standarddatabase", h4("Download the standard base")),
              ),

        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')
    })
}

shinyApp(ui = ui, server = server)

Antonio
  • 1,091
  • 7
  • 24
  • Hi, would you like to know how to generate the Excel file, or is this file already available in a fixed shareable link? If the latter, you can see the answer in https://stackoverflow.com/questions/42047422/create-url-hyperlink-in-r-shiny/42048943 – hamagust Jun 18 '20 at 20:50
  • Thanks friend for talking about this alternative, but I really need the other way. – Antonio Jun 18 '20 at 22:44
  • @Jose please see my solution below and upvote and accept it if it does what you desire. – hello_friend Jun 19 '20 at 02:36
  • Hi friend, thanks for the reply. Very good, just two quick things, in case you can help: I would like it to be downloaded in xlsx format. The other is that I would like to leave the Download Standard Database that you made similar to the "Example data File" on this site: https://daattali.com/shiny/ddpcr/. It's possible?. Thank you again! – Antonio Jun 19 '20 at 02:57
  • @Jose I have adjusted it to download to .xlsx format (see below). Be careful as your JVM can fill up and you may need to write a jvm garbage collection function and call it to clear some JVM memory otherwise you may have an error on export. I am not entirely sure what you are after from that Shiny example. Could you please try and explain what you want in more detail? Don't forget to upvote and accept my answer if it is doing what you desire. – hello_friend Jun 19 '20 at 03:06
  • It's OK now! I will use the example you did for my case, if I can't, I will return. Thank you so! – Antonio Jun 19 '20 at 03:20

1 Answers1

2
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)
hello_friend
  • 5,682
  • 1
  • 11
  • 15
  • Hello hello_friend, how are you? Can you see this question of my brother?https://stackoverflow.com/questions/65207873/evaluation-metrics-for-hierarchical-cluster-in-r – Antonio Dec 09 '20 at 01:34