download user uploaded csv file

.I am attempting to download a file after I upload it via fileInput and apply a user defined function via actionButton. When I run the app I get the error: Error in is.data.frame(x): object 'file1' not found. I think the code below works as a good reprex.

library(shiny)
library(DT)
ibrary(shinyWidgets)
library(plyr)
library(dplyr)
library(readxl)

cleanup <- function(x){
  x <- as.character(x) # convert to character
  x <- tolower(x) # make all lowercase
  x <- trimws(x, "both") # trim white space
  return(x)
}

ui <- fluidPage(
  h2("App"),
  fileInput("file1", "Upload file for cleaning", accept = c("xls", "csv"), multiple = F),
  actionButton(inputId = "clean", label = "Clean Data"),
  downloadButton("download1", "Download file1"),
  pickerInput(width = "75%",
          inputId = "pick_col1",
          label = "Select columns to display",
          choices = colnames(file1),
          selected = colnames(file1),
          options = list(
            `actions-box` = T,
            `selected-text-format` = paste("count > ", length(colnames(file1)) - 1),
            `count-selected-text` = "Alle",
            liveSearch = T,
            liveSearchPlaceholder = T
          ),
          
          multiple = T),
  DT::dataTableOutput("mytable")
)

load_path <- function(path) {
    req(input$file)
    ext <- tools::file_ext(path)

  if (ext == "csv"){
  read.csv(path, header = T)
} else if (ext == "xls" || ext == "xlsx"){
  read_excel(path)
} else{
  stop("Unknown extension: '.", ext, "'")
}

}

server <- function(input, output, session){

file1 <- reactive(load_path(input$selection$datapath[[1]]))


  eventReactive(input$clean, {
 output$mytable <- DT::renderDataTable({
  data.frame(lapply(select(file1(), input$pick_col1), cleanup))
})

})

  output$download <- downloadHandler(
filename = function(){
  paste0(tools::file_path_sans_ext(input$filename), ".csv")
  
},

content = function(file){
  write.csv(data(), file)
}

)

}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.