以excel R Shiny格式下载表格吗?

用R Shiny下载对象确实让我头疼,即使它看起来太简单了,但我不知道如何继续。这个问题是为了将formattable输出作为excel表下载,但是我什至不确定它们是否兼容,或者图像可能还不错。 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",`Claims` = color_tile("white",`Strength` = color_tile("white",`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)
z81452418 回答:以excel R Shiny格式下载表格吗?

您的问题中有多个问题。首先,让我们解决主面板中未显示的下载按钮和格式表。您的下载按钮不起作用,因为您在服务器端定义了数据框 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",`Claims` = color_tile("white",`Strength` = color_tile("white",`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数据的格式),我只能找到此链接(不会带来解决方案) https://github.com/renkun-ken/formattable/issues/70

本文链接:https://www.f2er.com/2565140.html

大家都在问