下载格式为 excel R Shiny?

Download formattable as excel R Shiny?

这个在R shiny中下载对象真是让我头疼,虽然看起来太简单了但是我不知道如何继续下去。这个问题是为了下载 formattable output as an excel table, but I'm not even sure if they are compatible, or an image might do just fine. In here 它似乎是我编码时要做的事情,但是每个用于绘图的 dowloadButton 问题都将绘图输入与输出分开,正如我设置的那样,但它不起作用正如我所想的那样,我什至无法继续检查我的 dowloadButton 是否有效。任何想法或遵循的路径将不胜感激!

library(openxlsx)
library(shiny)
library(formattable)

ui <- fluidPage(

  fluidRow(
    sidebarPanel(
      hr(style="border-color: #606060;"),
      # Add bullets
      h3(HTML(paste0("<b>","Download","</b>"))),
      downloadButton(outputId = "table_dowload", 
                     label = "Download"),
      hr(style="border-color: #606060;"),
      width = 3
    ),
    mainPanel(
      br(),
      formattableOutput("info_company_table"),
      br()
    )
  )
)

server <- function(input, output, session) {

## Visualization input
table_input <- function(){

  bycompany <- structure(list(Parent = "Melissa", 
                              Active = 12681L, 
                              Claims = 16.22, 
                              Strength = 24.15, 
                              Backward = 6.37, 
                              Forward = 1.09), 
                         row.names = 1L, 
                         class = "data.frame")

  # Visualize top 10
  if(nrow(bycompany())>0) {
    t <- formattable(bycompany() %>%
                       arrange(desc(`Active`, )) %>%
                       slice(1:10),
                     align = "l",
                     list(
                       `Backward` = color_tile("white", "lightblue"),
                       `Forward` = color_tile("white", "lightblue"),
                       `Claims` = color_tile("white", "lightblue"),
                       `Strength` = color_tile("white", "lightblue"),
                       `Active` = color_tile("white", "lightblue")
                     ))
  } else {
    t <- formattable(data.frame())
  }

}

## Visualization 
output$table <- renderFormattable({

  table_input()

})

# DOWNLOAD

output$table_dowload <- downloadHandler(
  filename <- function(){
  paste("Table",
        Sys.Date(),
        "xlsx",
        sep = ".")
},
content = function(file) {
  write.xlsx(table_input(), file)
})

}
shinyApp(ui,server)

您的问题中包含多个问题。首先,让我们解决主面板中未显示的下载按钮和格式表。您的下载按钮不起作用,因为您在服务器端定义了数据框 bycompany 作为函数 (bycompany()) 因此 shiny 无法将 bycompany 识别为数据框。因此,为了让您的下载按钮起作用,将所有 bycompany() 更改为 bycompany.

(在 table_input 函数内)

所以你的代码看起来像这样,现在下载按钮起作用了:

library(openxlsx)
library(shiny)
library(formattable)
# I've also added dplyr for pipe operator
library(dplyr)
ui <- fluidPage(fluidRow(
  sidebarPanel(
    hr(style = "border-color: #606060;"),
    # Add bullets
    h3(HTML(paste0(
      "<b>", "Download", "</b>"
    ))),
    downloadButton(outputId = "table_dowload",
                   label = "Download"),
    hr(style = "border-color: #606060;"),
    width = 3
  ),
  mainPanel(br(),
            formattableOutput("info_company_table"),
            br())
))

server <- function(input, output, session) {
  ## Visualization input
  table_input <- function() {
    bycompany <- structure(
      list(
        Parent = "Melissa",
        Active = 12681L,
        Claims = 16.22,
        Strength = 24.15,
        Backward = 6.37,
        Forward = 1.09
      ),
      row.names = 1L,
      class = "data.frame"
    )

    # Visualize top 10
    if (nrow(bycompany) > 0) {
      t <- formattable(
        bycompany %>%
          arrange(desc(`Active`,)) %>%
          slice(1:10),
        align = "l",
        list(
          `Backward` = color_tile("white", "lightblue"),
          `Forward` = color_tile("white", "lightblue"),
          `Claims` = color_tile("white", "lightblue"),
          `Strength` = color_tile("white", "lightblue"),
          `Active` = color_tile("white", "lightblue")
        )
      )
    } else {
      t <- formattable(data.frame(t))
    }

  }

  ## Visualization
  output$table <- renderFormattable({
    table_input()

  })

  # DOWNLOAD

  output$table_dowload <- downloadHandler(
    filename <- function() {
      paste("Table",
            Sys.Date(),
            "xlsx",
            sep = ".")
    },
    content = function(file) {
      write.xlsx(table_input(), file)
    }
  )

}
shinyApp(ui, server)

另请注意,如果您想在主面板中可视化格式化表,则应更改此部分

 ## Visualization
  output$table <- renderFormattable({
    table_input()

  })

为此,您已将 UI 部分定义为 formattableOutput("info_company_table")

          ## Visualization
          output$info_company_table <- renderFormattable({
            table_input()

          })

关于格式化表(即导出的excel数据的格式)我只能找到这个link(没有解决方案) https://github.com/renkun-ken/formattable/issues/70