【发布时间】:2019-06-14 07:23:59
【问题描述】:
根据用户输入下载为 excel 或 csv。该代码仅适用于 radioButtons 中的预选值。如下所示,它适用于 csv,因为 selected = "csv"。如果更改为 xlsx,则仅适用于 xlsx。用户应该能够选择并且这两个选项都应该是可能的。
也许该值已被缓存,我需要以某种方式强制更新。
library(shiny)
ui <- fluidPage(
h4("Download data"),
wellPanel(
fluidRow(
column(4, radioButtons("dl_data_file_type", "Format",
choices = c(excel = "xlsx",
csv = "csv"),
selected = "csv")),
column(5),
column(3, downloadButton("dl_data_dwnld_bttn"))
)))
server <- function(input, output) {
output$dl_data_dwnld_bttn <- {
downloadHandler(
filename = stringr::str_c(Sys.Date(), " Palim.", input$dl_data_file_type),
content = function(file){
x <- iris
if ( input$dl_data_file_type == "xlsx") {
writexl::write_xlsx(x, file)}
else if ( input$dl_data_file_type == "csv") {
readr::write_csv(x, file)}
})}}
shinyApp(ui = ui, server = server)
错误是excel文件仍然以.csv结尾,无法用excel打开。
【问题讨论】:
-
你确定吗?我没有看到任何错误。
-
不,我没试过。我想我现在明白了。看我的回答。
标签: r shiny shiny-reactivity