r - 根据用户对 radioButtons() 的选择,使用 downloadHandler() 动态下载闪亮的文件
问题描述
我有一个闪亮的应用程序,它根据用户输入创建一个数据框。我想制作一个动态下载按钮,该按钮需要用户选择(radioButton
)来下载动态制作的数据框。数据帧作为列表从函数返回
当我为两个不同的下载按钮创建功能时,下载工作正常
library(shiny)
library(DT)
temp_func <- function(){
x <- mtcars
y = x[,1]
return(list(complete_df = as.data.frame(x), column1 = as.data.frame(y)))
}
# UI
ui <- shinyUI({
fluidPage(
actionButton("fetch", label = "Fetch data first"),
mainPanel(DT::dataTableOutput("table")),
downloadButton("down_all", "Download all"),
downloadButton("down_c1", "Download c1")
)})
# Server
server <- Main_Server <- function(input,output,session){
# Reactive Values
values <- reactiveValues(table = NULL)
# fetchEvent (Consider temp_func() is fetching data from website)
observeEvent(input$fetch, {values$table <- temp_func()})
# Rendering table for display
output$table <- renderDT({datatable(values$table$complete_df)})
# Download 1
output$down_all <- downloadHandler(
filename = function() { paste("All columns","csv", sep=".")},
content = function(file) {write.csv(values$table$complete_df, file)})
# Download 2
output$down_c1 <- downloadHandler(
filename = function() { paste("Columns1","csv", sep=".")},
content = function(file) {write.csv(values$table$column1, file)})
}
# Run-app
shinyApp(ui, server)
一旦我合并这两个函数并input$choice
从单选按钮传递,我会得到一个空文件
library(shiny)
library(DT)
temp_func <- function(){
x <- mtcars
y = x[,1]
return(list(complete_df = as.data.frame(x), column1 = as.data.frame(y)))
}
# UI
ui <- shinyUI({
fluidPage(
actionButton("fetch", label = "Fetch data first"),
mainPanel(DT::dataTableOutput("table")),
radioButtons("rd", c("Select"), choices = c("All Columns" = "complete_df","Column 1" = "column1"),
selected = "complete_df"),
downloadButton("down", "Download")
)})
# Server
server <- Main_Server <- function(input,output,session){
# Reactive Values
values <- reactiveValues(table = NULL)
# fetchEvent (Consider temp_func() is fetching data from website)
observeEvent(input$fetch, {values$table <- temp_func()})
# Rendering table for display
output$table <- renderDT({datatable(values$table$complete_df)})
# Combined Download
output$down <- downloadHandler(
filename = function() { paste("File","csv", sep=".")},
content = function(file) {write.csv(values$table$input$rd, file)})
}
# Run-app
shinyApp(ui, server)
考虑 temp_func() 正在从其他网站获取数据
解决方案
尝试使用:
# Combined Download
output$down <- downloadHandler(
filename = function() { paste("File","csv", sep=".")},
content = function(file) {write.csv(values$table[[input$rd]], file)})
您使用的语法返回NULL
,因为values$table
没有input
字段。
使用更新的语法,下载的文件不再是空的。
推荐阅读
- javascript - 是否可以从单独的 html 页面单击 html 页面上的按钮?
- sql - SQL 错误信息 where 附近的语法错误
- batch-file - 如何批量设置两个变量之间的随机数?
- python - 当我写入与以前删除的文件具有相同文件名的文件时,scipy.io.wavfile.write 不起作用
- html - 为什么这个 CSS 代码让我的下拉菜单只显示 3 中的一项?
- javascript - 仅使用 CSS 单击时更改不同按钮的背景和字体颜色。我试过 :focus 但只适用于 Chrome
- mercurial - 如何在 tortoisehg 中变基?
- javascript - 如果找不到 ID (document.getElementById),希望 JS 继续
- angular - 从控制台触发 router.navigate
- batch-file - 如何修复批量无法正确加载网站链接?