简体   繁体   English

在 shiny 应用程序中,如何让用户选择要使用 write.table 下载的文件名和目录

[英]In a shiny app how can I let the user choose the filename and directory to download with write.table

This is a follow-up question to this这是对此的后续问题

Now I somehow managed to download the reactive dataframe to my hard drive (!not server or working directory) and append each new entry as new line with write.table .现在我以某种方式设法将反应 dataframe 下载到我的硬盘驱动器(!不是服务器或工作目录)和 append 每个新条目作为write.table的新行。

Interestingly write.csv does not work because it does not allow append argument https://stat.ethz.ch/pipermail/r-help/2016-August/441011.html .有趣的是write.csv不起作用,因为它不允许append参数https://stat.ethz.ch/pipermail/r-help/2016-August/441011.html

With this minimal working app, I would like to know how I can get the user to choose a directory and a filname to download there.有了这个最小的工作应用程序,我想知道如何让用户选择一个目录和一个文件名来下载。 Now I have this absolut path: file = "C:/Users/yourname/Downloads/my_df.csv" which works.现在我有了这个绝对路径: file = "C:/Users/yourname/Downloads/my_df.csv"有效。 But I don't know if it will work for other user!但我不知道它是否适用于其他用户!

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  sidebarLayout(
    
    sidebarPanel(width = 4,
                 sliderInput("a", "A", min = 0, max = 3, value = 0, width = "250px"),
                 actionButton("submit", "Submit")
                 ),
    
    mainPanel(
      titlePanel("Sliders"),
      tableOutput("values")
    )
  )
)
server <- function(input, output, session) {
 
  sliderValues <- reactive({
    data.frame(Name = c("A"), Value = as.character(c(input$a)), stringsAsFactors = FALSE)
  })
  
  output$values <- renderTable({
    sliderValues()
  }) 
  
  # Save the values to a CSV file on the hard disk ----
  saveData <- reactive({write.table(sliderValues(), file = "C:/Users/yourname/Downloads/my_df.csv", col.names=!file.exists("C:/Users/yourname/Downloads/my_df.csv"), append = TRUE) })
  
  observeEvent(input$submit, {
    saveData()
  })
}
shinyApp(ui, server)

The requirement is that the user should see a modal dialog ui with the question "In which folder with which filename you want to download?".要求是用户应该看到一个模态对话框 ui,其中包含问题“您要下载哪个文件夹和哪个文件名?”。 Quasi like the things we do daily if we download from the inte.net.如果我们从 inte.net 下载,就像我们每天做的事情一样。

I now solved it this way:我现在这样解决了:

I realized that I have two options:我意识到我有两个选择:

  1. As suggested by @Stéphane Laurent using downloadhandler正如@Stéphane Laurent 使用downloadhandler所建议的
  2. Using DT::datatable()使用DT::datatable()

I have decided to use number 2. Many thanks to all of your inputs!我决定使用数字 2。非常感谢您的所有投入!

library(shiny)
library(shinyWidgets)
library(DT)

ui <- fluidPage(
  sliderInput("a", "A", min = 0, max = 3, value = 0, width = "250px"),
  titlePanel("Sliders"),
  dataTableOutput("sliderValues", width="450px")
)
server <- function(input, output, session) {
  
  sliderValues <- reactive({
    df <- data.frame(
      Name = "A", 
      Value = as.character(c(input$a)), 
      stringsAsFactors = FALSE) %>% 
      pivot_wider(names_from = Name, values_from = Value)
    
    return(df)
  })

  
  # Show the values in an HTML table in a wider format----
  output$sliderValues <- DT::renderDataTable({
    DT::datatable(sliderValues(),
                  extensions = c('Buttons'),
                  options = list(
                    dom = 'frtBip',
                    buttons = list(list(extend = 'excel', filename = paste0("myname-", Sys.Date())))
                  )
    )
  })

}
shinyApp(ui, server)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM