Running the analysis

Please, I am trying to get my app to run a sentiment analysis of texts entered into the text fields after the text fields have been cleared, and display the scores in the table as well as in the graphs. But whenever I click the 'Run Analysis' button after clearing the initial texts and entering new ones, nothing happens. Here is my code below:

if(interactive()){

library(shiny)

library(shinycustomloader)

library(shinythemes)

library(SentimentAnalysis)

library(textclean)

library(reactable)

library(tm)  

 

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"),br(),br(), tableOutput("count"),tableOutput("freq")

                        ),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",strong("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="",placeholder="Enter new text")

      updateTextAreaInput(session,"texts",value="",placeholder="Enter new text")

      updateTextAreaInput(session,"word",value="",placeholder="Enter new text")

      updateTextAreaInput(session,"words",value="",placeholder="Enter new text")

      updateTextAreaInput(session,"wordy",value="",placeholder="Enter new text")

    })

  doc<-reactive({c(input$text,

                   input$texts,

                   input$word,

                   input$words,

                   input$wordy)})

  many<-reactive({termFreq(paste(

                          input$text,

                          input$texts,

                          input$word,

                          input$words,

                          input$wordy

  ))})

  Analyze<-reactive({

    analyzeSentiment(

      replace_symbol(

        replace_number(

          replace_ordinal(

            doc()))))})

  isolate_Analyze<-isolate(Analyze())

  QDAP<-isolate_Analyze$SentimentQDAP

  LoughranM<-isolate_Analyze$SentimentLM

  HarvardIV<-isolate_Analyze$SentimentGI

  word.count<-reactive({countWords(doc(),removeStopwords=FALSE)})

  tables<-reactive({

    data.frame(QDAP,LoughranM,HarvardIV)

  })

  isolate.tables<-isolate(tables())

  data<-reactive({switch(input$choice,

                         "QDAP"=isolate.tables$QDAP,

                         "LoughranM"=isolate.tables$LoughranM,

                         "HarvardIV"=isolate.tables$HarvardIV)})

  output$download<-downloadHandler(

    filename=function(){

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

    },

    content=function(file){

      write.csv(tables(),file)

    }

  )

  output$table<-renderReactable({

      input$run

      isolate(reactable(tables(),searchable=TRUE,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,outlined=TRUE,striped=TRUE,filterable=TRUE,compact=TRUE,onClick="select")

      )

  })

  output$count<-renderTable({

        input$run

        isolate(data.frame(word.count()))

       })

  output$freq<-renderTable({

        input$run

        isolate(as.matrix(many()))

      })

  output$plot<-renderPlot({

    color<-switch(input$color,

                  "Blue"="#5dade2",

                  "Red"="#e74c3c",

                  "Green"="#1abc9c",

                  "Yellow"="#f7dc6f",

                  "Purple"="#a569bd")

    input$run  

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

    )

  })

  output$graph<-renderPlot({

            input$run

            isolate(plotSentiment(data()))

      })

}

}

 shinyApp(ui=ui,server=server)

Please someone help me diagnose why my app is not running a new text sentiment analysis

It runs for me. What is the output supposed to be?

Hi @iFeanyi. Your code have misuse of isolate problem. If using isolate, the reactive trigger will stop, so no update. And many normal variable assigned problem, variables in shiny mostly reactive variables and must evaluate in observer, so aware of this concept. There are also logic problem in the style argument of the reactable table. You can use print to check the variable value in console, which can help you to make a good design of you conditional statement. Finally, improve more coding style which make debugging easier.

library(shiny)

library(shinycustomloader)

library(shinythemes)

library(SentimentAnalysis)

library(textclean)

library(reactable)

library(tm)



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"),
          br(),
          br(),
          tableOutput("count"),
          tableOutput("freq")
          
        ),
        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", strong("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 = "", placeholder = "Enter new text")
    
    updateTextAreaInput(session,
                        "texts",
                        value = "",
                        placeholder = "Enter new text")
    
    updateTextAreaInput(session, "word", value = "", placeholder = "Enter new text")
    
    updateTextAreaInput(session,
                        "words",
                        value = "",
                        placeholder = "Enter new text")
    
    updateTextAreaInput(session,
                        "wordy",
                        value = "",
                        placeholder = "Enter new text")
    
  })
  
  doc <- reactive({
    c(input$text,
      
      input$texts,
      
      input$word,
      
      input$words,
      
      input$wordy)
  })
  
  many <- reactive({
    termFreq(paste(
      input$text,
      
      input$texts,
      
      input$word,
      
      input$words,
      
      input$wordy
      
    ))
  })
  
  Analyze <- reactive({
    analyzeSentiment(replace_symbol(replace_number(replace_ordinal(doc(
    )))))
  })
  
  QDAP <- reactive(Analyze()$SentimentQDAP)
  
  LoughranM <- reactive(Analyze()$SentimentLM)

  HarvardIV <- reactive(Analyze()$SentimentGI)
  
  word.count <- reactive({
    countWords(doc(), removeStopwords = FALSE)
  })
  
  tables <- reactive({
    data.frame(QDAP(), LoughranM(), HarvardIV())
    
  })
  
  data <- reactive({
    switch(
      input$choice,
      
      "QDAP" = tables()$QDAP,
      
      "LoughranM" = tables()$LoughranM,
      
      "HarvardIV" = tables()$HarvardIV
    )
  })
  
  output$download <- downloadHandler(
    filename = function() {
      paste("table", ".csv", sep = "")
      
    },
    
    content = function(file) {
      write.csv(tables(), file)
      
    }
    
  )
  
  output$table <- renderReactable({
    input$run
    
    print(tables())
    
    isolate(
      reactable(
        tables(),
        searchable = TRUE,
        bordered = TRUE,
        defaultColDef = colDef(
          align = "center",
          
          headerStyle = list(background = "#5dade2"),
          
          style = function(value) {
            if(is.nan(value) || is.na(value)) {
              color <- "#5dade2"
            } else if (value > 0) {
              color <- "#27ae60"
            } else if (value < 0) {
              color <- "#e74c3c"
            } else{
              color <- "#5dade2"
            }
            
            list(color = color, fontWeight = "bold")
            
          }
        ),
        
        highlight = TRUE,
        outlined = TRUE,
        striped = TRUE,
        filterable = TRUE,
        compact = TRUE,
        onClick = "select"
      )
      
    )
    
  })
  
  output$count <- renderTable({
    input$run
    
    isolate(data.frame(word.count()))
    
  })
  
  output$freq <- renderTable({
    input$run
    
    isolate(as.matrix(many()))
    
  })
  
  output$plot <- renderPlot({
    color <- switch(
      input$color,
      
      "Blue" = "#5dade2",
      
      "Red" = "#e74c3c",
      
      "Green" = "#1abc9c",
      
      "Yellow" = "#f7dc6f",
      
      "Purple" = "#a569bd"
    )
    
    input$run
    
    isolate(
      barplot(
        data(),
        col = color,
        border = "white",
        xlab = "Texts",
        ylab = "Sentiment Scores",
        main = "Bar Plot of Sentiment Scores"
      )
      
    )
    
  })
  
  output$graph <- renderPlot({
    input$run
    
    isolate(plotSentiment(data()))
    
  })
  
}

}

shinyApp(ui = ui, server = server)

Thanks a lot @raytong. Your advice worked. My app now runs analysis.

A user is supposed to be able to clear those text fields and enter their own text. Then when the "Run Analysis" button is clicked, the app should compute new sentiment scores.

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