【问题标题】:Download formattable as excel R Shiny?下载 formattable 为 excel R Shiny?
【发布时间】:2020-03-27 22:18:42
【问题描述】:

这个用R shiny 下载对象真的让我很头疼,尽管它看起来太简单了,但我不知道如何继续下去。这个问题旨在将formattable 输出下载为excel 表,但我什至不确定它们是否兼容,或者图像可能会很好。 @ 987654322@ 在我编写代码时似乎需要做一些事情,但是当我设置它时,每个用于绘图的 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)

【问题讨论】:

    标签: r shiny download formattable


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 对于问题情况下的问题,我很抱歉,所以我没有真正考虑清楚。非常感谢您的完整回复,得到了我需要的东西,并且看起来它们确实匹配,因为您实际上可以完美地下载 Excel 文件。再次感谢!
    • 它在我的完整应用程序上给了我这个 (Warning: Error in UseMethod: no applicable method for 'as.htmlwidget' applied to an object of class "c('formattable_widget', 'htmlwidget')")。可能知道它的全部内容吗?
    猜你喜欢
    • 2017-03-24
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2016-01-29
    • 2015-01-16
    • 1970-01-01
    相关资源
    最近更新 更多