R Shiny: file download at the click of a button

I know that there are quite a few materials that are already available on the Internet to answer my question, but none of them seem to work for me. I suppose because I'm not very good at brilliant reactive programming.

So, I want to create an interface that allows the user to select a file using fileInput and upload it only when the "Download" button is clicked. I tried several solutions from different forums, but no one worked. The following is my last attempt:

 #ui.R library(shiny) shinyUI(pageWithSidebar( headerPanel(""), sidebarPanel( fileInput("in_file", "Input file:", accept=c("txt/csv", "text/comma-separated-values,text/plain", ".csv")), checkboxInput(inputId="is_header", label="Does the input file have column names?", value=TRUE), actionButton("upload_data", "Upload Data"), ), mainPanel( tabsetPanel( tabPanel("Original Data", tableOutput("orig_data")) ) ) )) #server.R library(shiny) shinyServer(function(input, output, session) { ra_dec_data <- reactive({ if(input$upload_data==0) return(NULL) return(isolate({ head(read_data(input$in_file$datapath, input$in_file$is_header), 50) })) }) output$orig_data <- renderTable({ ra_dec_data() }) }) 

The problem that I am facing is that the file is downloaded immediately after selecting it, and the "Download" button does not respond.

I assume that what I did does not make sense, so please accept my apologies for this horribly. Any help could be helpful. Thank you

+6
source share
1 answer

fileInput Download the file directly, so I suggest you create your own "fileInput".

Here's how I proceed:

Server.R

 library(shiny) shinyServer(function(input, output, session) { observe({ if (input$browse == 0) return() updateTextInput(session, "path", value = file.choose()) }) contentInput <- reactive({ if(input$upload == 0) return() isolate({ writeLines(paste(readLines(input$path), collapse = "\n")) }) }) output$content <- renderPrint({ contentInput() }) }) 

Ui.r

 library(shiny) shinyUI(pageWithSidebar( headerPanel("Example"), sidebarPanel( textInput("path", "File:"), actionButton("browse", "Browse"), tags$br(), actionButton("upload", "Upload Data") ), mainPanel( verbatimTextOutput('content') ) )) 

In Server.R, we first update the text input value each time we click the Browse action button.

"contentInput" is a reactive function, it will be reinstalled when the input values ​​(contained in the function body) are changed, "enter $ upload" here, and not when the "input $ path" has changed, because we isolate it, If we isolate If you don’t isolate the part containing the "input path $ path", the "contentInput" will be reinstalled every time we view a new file, and then the download button will be useless here.

And then we return the result of "contentInput" to "output $ content".

I hope for this help.

EDIT ##:

I realized that if you deselect the file, it will lead to an error and glitter of the application, then you should use this function by Henrik Bengtson ( https://stat.ethz.ch/pipermail/r-help/2007-June/133564.html ):

 file.choose2 <- function(...) { pathname <- NULL; tryCatch({ pathname <- file.choose(); }, error = function(ex) { }) pathname; } 
+5
source

All Articles