[英]Download formattable as excel R Shiny?
在 R Shiny 中下載對象確實讓我頭疼,即使它看起來太簡單了,但我不知道如何繼續下去。 這個問題是為了將可格式化的輸出下載為 excel 表,但我什至不確定它們是否兼容,或者圖像可能會很好。 在這里,我在編寫代碼時似乎需要做一些事情,但是在我設置時,每個用於繪圖的 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()
),因此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)
另請注意,如果您想在主面板中可視化 formattable,則應更改此部分
## 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.