RShiny download data frame as html instead of csv

Hello,

I am trying to download a dtaframe as csv file but when I press download button, I come up with an html file. Could you please help me to download a csv file? My code is below

Thank you

ui:

  downloadButton(outputId = "clean_download", label = "Download cleaned data")

server:

output$clean_download <- downloadHandler(
  filename =  function() {
    paste('clean_data.csv', sep='')
  },
  # content is a function with argument file. content writes the plot to the device
  content = function(filename) {
   write.csv(as.data.frame(train_cleaned()), filename, row.names=FALSE)
    
  } 
)

I expect that happens when the above struggle.
What is train_cleaned() before you cast it to a data.frame ?
You can peek at your objects in the console, a tradition as old as programming :

  content = function(filename) {
print(head(as.data.frame(train_cleaned()))
   write.csv(as.data.frame(train_cleaned()), filename, row.names=FALSE)
  }

train_cleaned() is subset of a data frame and it's type is "list"

I tried using write.csv(train_cleaned) outside of shiny and it worked properly, however when I try to use it with downloadHandler, a HTML file is downloaded instead of csv file.

I would expect a subset from a data.frame to be a dataframe and not a list... what method of subsetting are you doing?

Did you add print head and monitor that in your console ? You haven't shared the console output for me to make an opinion on.

Another way to approach this is for you to make a reprex.

please have a look at this guide, to see how to create one:

subset command:
train_cleaned=reactive({subset(train_sub(),!(rownames(train_sub()) %in% train_outlier_rownames()$rownumber))})

generated data frame from head(as.data.frame(train_cleaned))

When I open the app in web browser instead of R Studio viewer, it worked properly. I could download the file in csv format . It is strange :slight_smile:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.