Download handler and reactable

Hello guys,
Please I am trying to add a download button to my shiny app, so that a user can download the data in the table. Here is my code:

if(interactive()){

library(shiny)

library(shinycustomloader)

library(shinythemes)

library(SentimentAnalysis)

library(textclean)

library(reactable)  

 

ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("cerulean"),

               windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,

               tabPanel(strong("Opinion Miner"),

                          sidebarLayout(

                          sidebarPanel(width=3,

                          img(src="logo.jpg",height=130,width=150),

                          h4("Enter your texts in these fields"),

                          actionButton("clear","Clear Fields"),br(),br(),

                          textAreaInput("text","Text Field 1",value="It is a beautiful day"),

                          textAreaInput("texts","Text Field 2",value="I am happy to be here"),

                          textAreaInput("word","Text Field 3",value="Let's have some fun"),

                          textAreaInput("words","Text Field 4",value="It has been a bad day"),

                          textAreaInput("wordy","Text Field 5",value="I dislike clowns"),

                          actionButton("run","Run Analysis")

                        ),mainPanel(h4("The Opinion Miner is a tool for conducting sentiment analysis. It is useful for generating insights from product reviews as well as social media posts."),withLoader(reactableOutput("table"),loader="dnaspin"),downloadButton("download","Download Table"),

                                    selectInput("choice","Select Sentiment Score to Plot",choices=c("QDAP","LoughranM","HarvardIV")),selectInput("color","Select Color",choices=c("Blue","Red","Green","Yellow","Purple")),

                                    withLoader(plotOutput("plot",height=400,width=400),loader="dnaspin"),withLoader(plotOutput("graph",height=400,width=400),loader="dnaspin")))),

               tabPanel(strong("Financial Ratios Calculator")),

               navbarMenu(strong("More"),

               tabPanel(strong("Graphs and Charts")),

               tabPanel(strong("Tables")))

)

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

    observeEvent(input$clear,{

      updateTextAreaInput(session,"text",value="")

      updateTextAreaInput(session,"texts",value="")

      updateTextAreaInput(session,"word",value="")

      updateTextAreaInput(session,"words",value="")

      updateTextAreaInput(session,"wordy",value="")

    })

  output$download<-downloadHandler(

    filename=function(){

      paste("data.table",".csv",sep="")

    },

    content=function(file){

      write.csv(data.table,file="Table.csv")

    }

  )

  output$table<-renderReactable({

    tables<-data.frame(QDAP,LoughranM,HarvardIV)

    data.table<-reactable(tables,searchable=TRUE,selection="multiple",bordered=TRUE,defaultColDef=colDef(

      align="center",

      headerStyle=list(background="#5dade2"),

      style=function(value){

        if(value>0){color<-"#27ae60"}

        else if(value<0){color<-"#e74c3c"}

        else{color<-"#5dade2"}

        list(color=color,fontWeight="bold")

      }),

      highlight=TRUE)

  })

  output$plot<-renderPlot({

    data<-switch(input$choice,

        "QDAP"=QDAP,

        "LoughranM"=LoughranM,

        "HarvardIV"=HarvardIV)

    color<-switch(input$color,

                  "Blue"="#5dade2",

                  "Red"="#e74c3c",

                  "Green"="#1abc9c",

                  "Yellow"="#f7dc6f",

                  "Purple"="#a569bd")

    barplot(data,col=color,border="white",xlab="Texts",ylab="Sentiment Scores",main="Bar Plot of Sentiment Scores")

  })

  output$graph<-renderPlot({

    data<-switch(input$choice,

                 "QDAP"=QDAP,

                 "LoughranM"=LoughranM,

                 "HarvardIV"=HarvardIV)

    plotSentiment(data)

  })

    Analyze<-reactive(

    analyzeSentiment(

    replace_symbol(

    replace_number(

      replace_ordinal(

            doc<-c(input$text,input$texts,input$word,input$words,input$wordy))))))

  isolate_Analyze<-isolate(Analyze())

  QDAP<-isolate_Analyze$SentimentQDAP

  LoughranM<-isolate_Analyze$SentimentLM

  HarvardIV<-isolate_Analyze$SentimentGI

}

}

 shinyApp(ui=ui,server=server)

However, when I run the app, I get an error message that "data.table" does not exist. Please, I would really appreciate anybody's help. Thanks guys, you're the best.

Hi @iFeanyi. The problem was due to data.table just exist within the renderReactable. You have to make a public variable as in my code I made a reactiveValues named vals to store the table. Another problem is reactable is a html object, so you cannot export to csv. I stored the tables instead of data.table. And in downloadHandler, the content function should use the file argument.

library(shiny)

library(shinycustomloader)

library(shinythemes)

library(SentimentAnalysis)

library(textclean)

library(reactable)  

ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("cerulean"),
               
               windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,
               
               tabPanel(strong("Opinion Miner"),
                        
                        sidebarLayout(
                          
                          sidebarPanel(width=3,
                                       
                                       img(src="logo.jpg",height=130,width=150),
                                       
                                       h4("Enter your texts in these fields"),
                                       
                                       actionButton("clear","Clear Fields"),br(),br(),
                                       
                                       textAreaInput("text","Text Field 1",value="It is a beautiful day"),
                                       
                                       textAreaInput("texts","Text Field 2",value="I am happy to be here"),
                                       
                                       textAreaInput("word","Text Field 3",value="Let's have some fun"),
                                       
                                       textAreaInput("words","Text Field 4",value="It has been a bad day"),
                                       
                                       textAreaInput("wordy","Text Field 5",value="I dislike clowns"),
                                       
                                       actionButton("run","Run Analysis")
                                       
                          ),mainPanel(h4("The Opinion Miner is a tool for conducting sentiment analysis. It is useful for generating insights from product reviews as well as social media posts."),withLoader(reactableOutput("table"),loader="dnaspin"),downloadButton("download","Download Table"),
                                      
                                      selectInput("choice","Select Sentiment Score to Plot",choices=c("QDAP","LoughranM","HarvardIV")),selectInput("color","Select Color",choices=c("Blue","Red","Green","Yellow","Purple")),
                                      
                                      withLoader(plotOutput("plot",height=400,width=400),loader="dnaspin"),withLoader(plotOutput("graph",height=400,width=400),loader="dnaspin")))),
               
               tabPanel(strong("Financial Ratios Calculator")),
               
               navbarMenu(strong("More"),
                          
                          tabPanel(strong("Graphs and Charts")),
                          
                          tabPanel(strong("Tables")))
               
)

server<-function(input,output,session){
  vals <- reactiveValues()
  
  observeEvent(input$clear,{
    
    updateTextAreaInput(session,"text",value="")
    
    updateTextAreaInput(session,"texts",value="")
    
    updateTextAreaInput(session,"word",value="")
    
    updateTextAreaInput(session,"words",value="")
    
    updateTextAreaInput(session,"wordy",value="")
    
  })
  
  output$download<-downloadHandler(
    
    filename=function(){
      
      paste("data.table",".csv",sep="")
      
    },
    
    content=function(file){
      write.csv(vals$dt,file=file)
    }
    
  )
  
  output$table<-renderReactable({
    
    tables<-data.frame(QDAP,LoughranM,HarvardIV)
    vals$dt <- tables
    data.table <- reactable(tables,searchable=TRUE,selection="multiple",bordered=TRUE,defaultColDef=colDef(
      
      align="center",
      
      headerStyle=list(background="#5dade2"),
      
      style=function(value){
        
        if(value>0){color<-"#27ae60"}
        
        else if(value<0){color<-"#e74c3c"}
        
        else{color<-"#5dade2"}
        
        list(color=color,fontWeight="bold")
        
      }),
      
      highlight=TRUE)
    
  })
  
  output$plot<-renderPlot({
    
    data<-switch(input$choice,
                 
                 "QDAP"=QDAP,
                 
                 "LoughranM"=LoughranM,
                 
                 "HarvardIV"=HarvardIV)
    
    color<-switch(input$color,
                  
                  "Blue"="#5dade2",
                  
                  "Red"="#e74c3c",
                  
                  "Green"="#1abc9c",
                  
                  "Yellow"="#f7dc6f",
                  
                  "Purple"="#a569bd")
    
    barplot(data,col=color,border="white",xlab="Texts",ylab="Sentiment Scores",main="Bar Plot of Sentiment Scores")
    
  })
  
  output$graph<-renderPlot({
    
    data<-switch(input$choice,
                 
                 "QDAP"=QDAP,
                 
                 "LoughranM"=LoughranM,
                 
                 "HarvardIV"=HarvardIV)
    
    plotSentiment(data)
    
  })
  
  Analyze<-reactive(
    
    analyzeSentiment(
      
      replace_symbol(
        
        replace_number(
          
          replace_ordinal(
            
            doc<-c(input$text,input$texts,input$word,input$words,input$wordy))))))
  
  isolate_Analyze<-isolate(Analyze())
  
  QDAP<-isolate_Analyze$SentimentQDAP
  
  LoughranM<-isolate_Analyze$SentimentLM
  
  HarvardIV<-isolate_Analyze$SentimentGI
  
}



shinyApp(ui=ui,server=server)

Thanks a lot @raytong. I made my input variable into a reactive function, and that solved the problem.

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