【问题标题】:Link excel database with downloadLink in Shiny在 Shiny 中使用 downloadLink 链接 excel 数据库
【发布时间】:2020-06-19 11:04:34
【问题描述】:

您能帮我将一个 Excel 数据库链接到我的下载链接吗?因此,每当我在闪亮中单击“下载标准库”时,就会自动下载一个 excel 数据库。我在下面做了一个最小的例子来展示这个想法。我不知道如何在我的服务器上调整它以使其正常工作。

谢谢!

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)

【问题讨论】:

  • 您好,您想知道如何生成 Excel 文件,或者该文件是否已在固定的可共享链接中提供?如果是后者,你可以在stackoverflow.com/questions/42047422/…看到答案
  • 感谢朋友谈论这个替代方案,但我真的需要另一种方式。
  • @Jose 请在下面查看我的解决方案,如果它符合您的要求,请投票并接受它。
  • 朋友您好,感谢您的回复。非常好,只需两件快速的事情,以防您能提供帮助:我希望以 xlsx 格式下载它。另一个是我想留下您制作的类似于此站点上的“示例数据文件”的下载标准数据库:daattali.com/shiny/ddpcr。这是可能的?。再次感谢您!
  • @Jose 我已将其调整为下载为 .xlsx 格式(见下文)。请小心,因为您的 JVM 可能会填满,您可能需要编写一个 jvm 垃圾收集函数并调用它来清除一些 JVM 内存,否则您可能会在导出时出错。我不完全确定你从那个闪亮的例子中得到了什么。您能否尝试更详细地解释您想要什么?如果它正在做你想做的事情,请不要忘记投票并接受我的回答。

标签: r shiny


【解决方案1】:
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)

【讨论】:

猜你喜欢
  • 2018-07-24
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 2016-06-26
  • 2018-10-23
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多