用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)