首页 > 解决方案 > 如何在我的情绪分析应用程序中显示缺失值

问题描述

拜托,我开发了一个闪亮的应用程序,可以分析文本中的情绪。但是,当我没有填写所有文本字段并运行分析时,我会收到一条错误消息。这是我的应用程序代码:

if(interactive()){
library(shiny)
library(shinycustomloader)
library(shinycssloaders)  
library(shinythemes)
library(SentimentAnalysis)
library(textclean)
library(reactable)
library(tm)  

options(spinner.color="#3498db",
        spinner.color.background="#ffffff",
        spinner.size=1)  

ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("cerulean"),
               windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,
               tabPanel(strong("Opinion Miner"),icon=icon("table"),
                          sidebarLayout(
                          sidebarPanel(width=3,
                          img(src="logo.jpg",height=130,width=150),
                          h4(strong("Enter your texts in these fields")),
                          actionButton("clear",strong("Clear Fields"),icon=icon("broom")),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 outing"),
                          textAreaInput("wordy","Text Field 5",value="I dislike clowns"),
                          actionButton("run",strong("Run Analysis"),icon=icon("caret-right")),br(),br(),h5(strong("The number of words entered into each text field:")),tableOutput("count")),
                          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."),withSpinner(reactableOutput("table"),type=1),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")),
                                    withSpinner(plotOutput("plot",height=400,width=400),type=1),withSpinner(plotOutput("graph",height=400,width=400),type=1)))),

               tabPanel(strong("Financial Ratios Calculator"),icon=icon("chart-bar")),
               tabPanel(strong("Send Us Your Feedback"),icon=icon("envelope")),
               navbarMenu(strong("More"),
               tabPanel(strong("Graphs and Charts"),icon=icon("chart-bar")),
               tabPanel(strong("Tables"),icon=icon("table")))

)
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)})


  Analyze<-reactive({
    round(analyzeSentiment(
      replace_symbol(
        replace_number(
          replace_ordinal(
            doc())))),1)})

  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
      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=FALSE,compact=TRUE,onClick="select")
      )
  })

  output$count<-renderTable({
        input$run
        isolate(data.frame(word.count()))
       })

  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) 

以下是我总是收到的错误消息:

Warning: Removed 1 rows containing missing values (geom_path).
Warning: Error in if: missing value where TRUE/FALSE needed
  115: <Anonymous> [C:/Users/Idiaye/Documents/Analytic/Analytic.R#94]
  113: callFunc
  112: FUN
  111: lapply
  110: FUN
  109: lapply
  108: reactable
   95: func [C:/Users/Idiaye/Documents/Analytic/Analytic.R#90]
   82: origRenderFunc
   81: output$table
    1: runApp

请,我将非常感谢您的帮助。谢谢各位,你们是最棒的。

标签: rshiny

解决方案


可能有一个更优雅的解决方案,但这将完成这项工作。我添加了一个条件语句来忽略空文本框。

完整代码:


if(interactive()){
  library(shiny)
  library(shinycustomloader)
  library(shinycssloaders)  
  library(shinythemes)
  library(SentimentAnalysis)
  library(textclean)
  library(reactable)
  library(tm)  

  options(spinner.color="#3498db",
          spinner.color.background="#ffffff",
          spinner.size=1)  

  ui<-navbarPage(strong("Mavis Analytic"),theme=shinytheme("cerulean"),
                 windowTitle="Mavis Analytic",fluid=TRUE,inverse=FALSE,
                 tabPanel(strong("Opinion Miner"),icon=icon("table"),
                          sidebarLayout(
                            sidebarPanel(width=3,
                                         img(src="logo.jpg",height=130,width=150),
                                         h4(strong("Enter your texts in these fields")),
                                         actionButton("clear",strong("Clear Fields"),icon=icon("broom")),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 outing"),
                                         textAreaInput("wordy","Text Field 5",value="I dislike clowns"),
                                         actionButton("run",strong("Run Analysis"),icon=icon("caret-right")),br(),br(),h5(strong("The number of words entered into each text field:")),tableOutput("count")),
                            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."),withSpinner(reactableOutput("table"),type=1),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")),
                                      withSpinner(plotOutput("plot",height=400,width=400),type=1),withSpinner(plotOutput("graph",height=400,width=400),type=1)))),

                 tabPanel(strong("Financial Ratios Calculator"),icon=icon("chart-bar")),
                 tabPanel(strong("Send Us Your Feedback"),icon=icon("envelope")),
                 navbarMenu(strong("More"),
                            tabPanel(strong("Graphs and Charts"),icon=icon("chart-bar")),
                            tabPanel(strong("Tables"),icon=icon("table")))

  )
  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(if(nchar(input$text)>0) {input$text},
                     if(nchar(input$texts)>0) {input$texts},
                     if(nchar(input$word)>0) {input$word},
                     if(nchar(input$words)>0) {input$words},
                     if(nchar(input$wordy)>0) {input$wordy}
                     )
      })


    Analyze<-reactive({
      round(analyzeSentiment(
        replace_symbol(
          replace_number(
            replace_ordinal(
              doc())))),1)})

    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
      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=FALSE,compact=TRUE,onClick="select")
      )
    })

    output$count<-renderTable({
      input$run
      isolate(data.frame(word.count()))
    })

    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) 

推荐阅读