Sentiment Analysis App

...Please, I am trying to develop an app that would have sentiment analysis as one of its functions. I want the text areas of the sentiment analysis part of the app to have default values, so that once the app is loaded, there are tables and graphs created from the default text values in the text areas, which have been analyzed using the sentiment analysis package. Then once the text fields are cleared, a user can then input his texts and run a sentiment analysis. I wrote the codes for the sentiment analysis, and tried to display results in a data table, using data.frame() function in the server. But I keep getting the following error message whenever I run the app:

Warning: Error in $.shinyoutput: Reading objects from shinyoutput object not allowed.
  50: stop
  49: $.shinyoutput
  47: server [C:/Users/Idiaye/Documents/Analytic.R#35]
Error in `$.shinyoutput`(output, table) : 
  Reading objects from shinyoutput object not allowed.

This is the code for the app:

if(interactive()){
library(shiny)
library(shinycustomloader)
library(shinythemes)
library(SentimentAnalysis)
library(textclean)
ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("spacelab"),
               windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,
               tabPanel(strong("Opinion Miner"),
                          sidebarLayout(
                          sidebarPanel(width=3,
                          h4("Enter your texts in these fields"),br(),
                          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 tough journey"),
                          textAreaInput("wordy","Text Field 5",value="I don't like clowns"),
                          actionButton("run","Run Analysis")
                        ),mainPanel(h4("A table of the sentiment scores across four dictionaries"),withLoader(tableOutput("table"),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$table<renderDataTable({
    data.frame(
    QDAP<-Analyze$SentimentQDAP,
    LoughranM<-Analyze$SentimentLM,
    Henry<-Analyze$SentimentHE,
    HarvardIV<-Analyze$SentimentGI
    )
   })
}
}
  Analyze<-replace_symbol(
           replace_number(
           analyzeSentiment(
           doc<-c("input$text","input$texts","input$word","input$words","input$wordy"))))

shinyApp(ui=ui,server=server)

I would really appreciate anybody's help. Thanks.

HI @iFeanyi

I think your pbrobleme is that you did not specify well your affectation operator here. You put < insead of <-.

output$table<renderDataTable({

Hope it help.

Thanks Rodrigue. I corrected that error, but I am getting a new one:

Listening on http://127.0.0.1:4185
Warning: Error in $: $ operator is invalid for atomic vectors
  97: data.frame
  96: renderDataTable [C:/Users/Idiaye/Documents/Analytic.R#37]
  95: func
  82: origRenderFunc
  81: output$table
   1: runApp

your objet Analyze come from replace_symbol. The ouptput of a replace_symbol is character with no colnames. But in your renderdataTable, you are calling a column from Analyze who does not exist ( Analyze$SentimentQDAP, ... ). That is the pb.

In addition, I think you must put Analyze<-replace_symbol(... inside the server function or on the top of your app.

1 Like
data.frame( QDAP<-Analyze$SentimentQDAP, LoughranM<-Analyze$SentimentLM, Henry<-Analyze$SentimentHE, HarvardIV<-Analyze$SentimentGI )

This is weird, if not outright incorrect, please try with = instead of <– when assigining within the data.frame function.

1 Like

Here is my code once again, with a little adjustments:

if(interactive()){
library(shiny)
library(shinycustomloader)
library(shinythemes)
library(SentimentAnalysis)
library(textclean)
 
ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("spacelab"),
               windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,
               tabPanel(strong("Opinion Miner"),
                          sidebarLayout(
                          sidebarPanel(width=3,
                          h4("Enter your texts in these fields"),br(),
                          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 tough journey"),
                          textAreaInput("wordy","Text Field 5",value="I don't like clowns"),
                          actionButton("run","Run Analysis")
                        ),mainPanel(h4("A table of the sentiment scores across four dictionaries"),withLoader(dataTableOutput("table"),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$table<-renderDataTable({
    data.frame(
    QDAP,LoughranM,Henry,HarvardIV)
    
   })
  Analyze<-analyzeSentiment(
    replace_symbol(
    replace_number(
      replace_ordinal(
            doc<-c(input$text,input$texts,input$word,input$words,input$wordy)))))
  QDAP<-Analyze$SentimentQDAP
  LoughranM<-Analyze$SentimentLM
  Henry<-Analyze$SentimentHE
  HarvardIV<-Analyze$SentimentGI
}
}
 
shinyApp(ui=ui,server=server)

@iFeanyi

First, you can't use an objet that you define after. I talking about QDAP, LoughranM, ...You must put you objet Analyze before the output$table.

Second, you use reactive object in Analyze without a reactive context. You must put a reactive context when calling analyzeSentiment and isolate the result.

This script must work:

if(interactive()){
  library(shiny)
  library(shinycustomloader)
  library(shinythemes)
  library(SentimentAnalysis)
  library(textclean)
  
  ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("spacelab"),
                 windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,
                 tabPanel(strong("Opinion Miner"),
                          sidebarLayout(
                            sidebarPanel(width=3,
                                         h4("Enter your texts in these fields"),br(),
                                         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 tough journey"),
                                         textAreaInput("wordy","Text Field 5",value="I don't like clowns"),
                                         actionButton("run","Run Analysis")
                            ),mainPanel(h4("A table of the sentiment scores across four dictionaries"),withLoader(dataTableOutput("table"),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="")
    })
   
    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
    Henry<-isolate_Analyze$SentimentHE
    HarvardIV<-isolate_Analyze$SentimentGI
    
    output$table<-renderDataTable({
      data.frame(
        QDAP,LoughranM,Henry,HarvardIV)

    })
  }
}

shinyApp(ui=ui,server=server)

I advise you to read the Rstudio doc about Reactivity. It would help you a lot.

What I am trying to create is a sentiment analysis app that when loaded, will display the scores and graphs of the default values in the text areas in the side panel. After the fields are cleared, a user can then input his own texts and then run an analysis.

Thanks again Rodrigue. But I am getting a new error message:

Listening on http://127.0.0.1:7259
ERROR: [uv_write] broken pipe
ERROR: [uv_write] broken pipe
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  67: stop
  66: .getReactiveEnvironment()$currentContext
  65: getCurrentContext
  64: .subset2(x, "impl")$get
  63: $.reactivevalues
  47: server [C:/Users/Idiaye/Documents/Analytic.R#41]
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Warning: Error in data.frame: object 'QDAP' not found
  97: data.frame
  96: renderDataTable [C:/Users/Idiaye/Documents/Analytic.R#37]
  95: func
  82: origRenderFunc
  81: output$table
   1: runApp

I can't reproduce your new bug. Did you use my script?

My ouptut

@iFeanyi did you solve your problem?

1 Like

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