user14543207
user14543207

Reputation:

R shiny not downloading anything

I am working on a data cleaning app. The app is supposed to take a file from the user clean it, and be able to download the cleaned data. For some reason my app will only download the html of the app and not a csv. Any Help would be awesome. Here is what I have so far. The interface is asking me to add more details but this is all I got

ui.R

library(shiny)
library(DT)
###Allows up to 60 mgs of data
options(shiny.maxRequestSize = 60*1024^2)

navbarPage(
  "Astute App",
  tabPanel(
    "Clean Data",
    fluidRow(
      fileInput('target_upload', 'Choose file to upload',
                accept = c(
                  'text/csv',
                  'text/comma-separated-values',
                  '.csv'
                )),
      radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=",",inline=TRUE),
      DT::dataTableOutput("sample_table")),
    downloadButton("cleaned_data", label = "Download Data"),
  ),
  tabPanel(
    "Time Series Analysis",
    fluidPage(
      
    )
  ),
  tabPanel(
    "Prediction",
    fluidPage(
      
    )
  ),
  collapsible = TRUE
)

The interface is asking me to add more details but this is all I got server.R

library(shiny)
library(DT)
###Allows up to 60 mgs of data
options(shiny.maxRequestSize = 60*1024^2)


function(input, output, session){
  
########Reads in uplaoded Data
  df_products_upload <- reactive({
    inFile <- input$target_upload
    if (is.null(inFile))
      return(NULL)
    df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
    return(df)
  })
  
######Clean the data#####
  Cleaned_data <- df
  
  download_cleaned_data <- downloadHandler(
    filename = Cleaned_data,
    content = function(file){
      file.copy("data/", file)}
    )
  
###Displays Uploaded data unclean
  output$sample_table<- DT::renderDataTable({
    df <- df_products_upload()
    DT::datatable(df)
  })
  
  
  
  
  
  
  
}

Not sure what else to add

Upvotes: 1

Views: 195

Answers (1)

Ash
Ash

Reputation: 1513

A couple of issues in the downloadHandler section. It should look like this:

output$cleaned_data <- downloadHandler(
    filename = "mydata.csv",
    content = function(file) {write.csv(df_products_upload(), file)}
)

Here is a minimal working example, showing uploading and downloading a file:

library(shiny)

ui <- fluidPage(
  
    #Select file
    fileInput('myfileinput', 'Choose file to upload'),
    
    #Download file
    downloadButton("myfiledownload", label = "Download file"),
    
    #Data table of loaded file
    tableOutput("mydatatable")
    
)

server <- function(input, output, session) {
  
    #Create a reactive dataframe to hold our loaded file
    df_fileuploaded <- reactive({
    
        #If no file selected set the dataframe to null and exit
        if (is.null(input$myfileinput)) return(NULL)
        
        #Load the selected file
        return(read.csv(input$myfileinput$datapath))
    
    })
    
    #Download handler
    output$myfiledownload <- downloadHandler(
    
        filename = "mydata.csv",
        content = function(file) {write.csv(df_fileuploaded(), file)}
    
    )
    
    #Data table
    output$mydatatable <- renderTable({
        
        df_fileuploaded()
    
    })
    
}

shinyApp(ui, server)

Here is your full app with those changes:

ui.r

library(shiny)
library(DT)
###Allows up to 60 mgs of data
options(shiny.maxRequestSize = 60*1024^2)

navbarPage(
    "Astute App",
    tabPanel(
        "Clean Data",
        fluidRow(
            fileInput('target_upload', 'Choose file to upload',
                      accept = c(
                          'text/csv',
                          'text/comma-separated-values',
                          '.csv'
                      )),
            radioButtons("separator","Separator: ",choices = c(";",",",":"), selected=",",inline=TRUE),
            DT::dataTableOutput("sample_table")),
        downloadButton("cleaned_data", label = "Download Data"),
    ),
    tabPanel(
        "Time Series Analysis",
        fluidPage(
            
        )
    ),
    tabPanel(
        "Prediction",
        fluidPage(
            
        )
    ),
    collapsible = TRUE
)

server.r

library(shiny)
library(DT)
###Allows up to 60 mgs of data
options(shiny.maxRequestSize = 60*1024^2)


function(input, output, session){
    
    ########Reads in uplaoded Data
    df_products_upload <- reactive({
        inFile <- input$target_upload
        if (is.null(inFile))
            return(NULL)
        df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
        return(df)
    })
    
output$cleaned_data <- downloadHandler(
    filename = "mydata.csv",
    content = function(file) {write.csv(df_products_upload(), file)}
)
    
    ###Displays Uploaded data unclean
    output$sample_table<- DT::renderDataTable({
        df <- df_products_upload()
        DT::datatable(df)
    })
    
}

Upvotes: 2

Related Questions