在Flexdashboard Shiny应用程序的下载处理程序中使用上载的文件名

时间:2018-09-07 20:20:20

标签: r shiny flexdashboard

我希望能够使用下载处理程序中上传到我闪亮的应用程序的文件的名称。我的用例是生成报告,并将原始文件名(上传的文件名)与其他文本连接起来。

我创建了一个MWE应用,您可以在其中上传CSV文件并下载相同的文件。下载的文件应具有相同的名称,并带有“ NEW”。

---
title: Test downloading file with correct name
runtime: shiny
output: 
  flexdashboard::flex_dashboard
---

# Sidebar {.sidebar}

```{r}

#############
## Upload  ##
#############
fileInput("file", "Upload CSV File",
          accept = c(
              "text/csv",
              "text/comma-separated-values,text/plain",
              ".csv"))

######################################
## Download file with the same name ##
######################################
downloadHandler(
    filename = paste("NEW", input$file$name),
    content = function(file) {
        write.csv(x = read.csv(input$file$datapath),
                   file = file, row.names = FALSE)
    },
    outputArgs = list(label = "Download Uploaded File"))

```

相反,下载的文件名为NEW_,但内容正确。

我发现我可以通过在文件名上添加观察语句来正确命名文件(至少有时是这样):

---
title: Test downloading file with correct name
runtime: shiny
output: 
  flexdashboard::flex_dashboard
---

# Sidebar {.sidebar}

```{r}

#############
## Upload  ##
#############
fileInput("file", "Upload CSV File",
          accept = c(
              "text/csv",
              "text/comma-separated-values,text/plain",
              ".csv"))

######################################
## Download file with the same name ##
######################################
downloadHandler(
    filename = paste("NEW", input$file$name),
    content = function(file) {
        write.csv(x = read.csv(input$file$datapath),
                   file = file, row.names = FALSE)
    },
    outputArgs = list(label = "Download Uploaded File"))

```
```{r, echo = FALSE}

## This doesn't produce output but is necessary to ensure that output files are
## named correctly
observe({
    show(input$file$name)
})

```

shiny::downloadHandler()的帮助文件说,可以在content函数中使用反应表达式,那么为什么第一个代码块不起作用?

1 个答案:

答案 0 :(得分:0)

我通过添加一个事件观察器来使其工作。我不确定这是否适用于您的情况,但可以产生理想的结果。我也直接在代码中留言最多

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Sidebar {.sidebar}

```{r echo=FALSE}

shinyApp(

  ui = fluidPage(
    fileInput("file", "Upload CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")),
    downloadButton("downloadData", label = "DL")
  ),

  server = function(input, output, session) {
    # Wait for input$file to change so we know there is data available
    observeEvent(input$file, {
      # store all the input$file objects on the_f
      the_f <- as.list(input$file)
      # Create the new file name for downloading
      new_name <- sprintf("NEW_%s.csv", the_f[['name']])
      # Go ahead and read in
      new_dat <- read.csv(the_f[['datapath']])
      # DL handler
      output$downloadData <- downloadHandler(
        filename = function(){
          # Just use the name from outside the handler we already created
          new_name
        },
          # Handle content as follows
        content = function(file){
          write.csv(new_dat, file = file, row.names = FALSE)
        }
      )
    })
  }
)
```

enter image description here