0

I don't have much experience working on shiny. I am trying to download the data by using DownloadHandeler function. I get nothing when I hit the download button.

My code:

ui:   downloadButton('download',"Download the data"),


Server :

 output$tb_cc <- DT::renderDataTable({
    if (is.null(input$dataset_cc)) {
      return(NULL)
    }
    
    library(dplyr)
    library(tidyverse)
    library(tidytext)
    
    df <- filedata_cc()
    df$date <- df$Responsedate
    df$date <- as.Date(df$date, format = "%m/%d/%Y")
    df <-
      subset(df, df$date >= input$dateRange_cc[1] &
               df$date <= input$dateRange_cc[2])
    
    df <- subset(df, df$sentiment_segment == input$sentiment_cc)
    
    df <- subset(df, df$Type.1 == input$issuetype_cc)
    
    
    df<- df[c(3:10)]
    DT::datatable(df)
  })

  output$download <- downloadHandler(
    filename = function() { 'Sentiment_data.csv' }, 
    content = function(file) {
      write.csv(df(), file)
    }
  )

sample data : enter image description here

  • Hi, Could you edit your question to provide a minimal data set? Like they describe [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). That would make trouble-shooting your code much easier. – xilliam Jun 18 '21 at 10:32
  • This is how it looks like : Responsedate : 06/01/2021 Market: UK NPS : 10 Type.1 : a concat_comment: abhs sentiment_segment : positive – Data Solver Jun 18 '21 at 14:41
  • Could you tell us about 'filedata_cc()'? From which package does this function originate? – xilliam Jun 21 '21 at 22:25

1 Answers1

0

You need to have df() reactive object outside of renderDataTable. Something like this should work:

ui <- fluidPage(
   downloadButton('download',"Download the data")
)

server <- function(input, output) {
  df<-reactive({
    faithful
  })

  output$download <- downloadHandler(
      filename = function() { 'Sentiment_data.csv' },
      content = function(file) {
        write.csv(df(), file)
    }
  )
}

shinyApp(ui, server)

So in your case, instead of df<-reactive({ faithful }) in my example, you can write

df <- reactive({
    df<-filedata_cc()
    df$date <- df$Responsedate
    df$date <- as.Date(df$date, format = "%m/%d/%Y")
    df <-
      subset(df, df$date >= input$dateRange_cc[1] &
               df$date <= input$dateRange_cc[2])
    
    df <- subset(df, df$sentiment_segment == input$sentiment_cc)
    
    df <- subset(df, df$Type.1 == input$issuetype_cc)
    
    
    df<- df[c(3:10)]
    df
})
phago29
  • 142
  • 1
  • 9