shinydashboard icon indicating copy to clipboard operation
shinydashboard copied to clipboard

Error expected an object with class 'shiny.tag'

Open KafeelBasha opened this issue 7 years ago • 5 comments

Below is my code for the server part which returns error as shown in the screenshot. image

library(shinydashboard)
library(dplyr)
library(shiny)
library(ggplot2)
server <- shinyServer(function(input,output,session){
  
  # read file
  values <- reactive({read.csv(input$file$datapath,na.strings=c(" ","","  ","   ")) })
  
  
  # observeEvent for input$file
  observeEvent(input$file, {
    
    # render tables at file upload
    output$data.frame <- renderTable(values())
    
    # update selectInput#Question
    updateSelectInput(
      session, 
      inputId = "Question",
      choices=names(values()),    
      selected=names(values()[,1])
    )
    
  })
  
  # observeEvent for input$Go
  observeEvent(input$Go, {
    # temp <- values([-input$Delete, ]    # commented for now
    # values <- temp                      # commented for now
    
    # commented for now
    library(tm)
    demo=Corpus(VectorSource(values()[,input$Question]))
    demo=tm_map(demo,content_transformer(tolower))
    demo=tm_map(demo,removePunctuation)
    df<- data.frame(text=unlist(sapply(demo,`[`)),stringsAsFactors=F)
    df$text=ifelse(df$text=="poor"|df$text=="fair","BAD",ifelse(df$text=="excellent"|df$text=="good","GOOD",NA))
    
    output$pos <- renderValueBox({
      
      # evaluate data: count, summarize, etc.
      positive<- df%>%
        filter(text=="GOOD") %>%count()    
      
      # make value box
      valueBox(positive,"Positive Response")
    })
    
    output$neg <- renderValueBox({
      
      # evaluate data: count, summarize, etc.
      negative<- df%>%
        filter(text=="BAD") %>%count()     
      
      # make value box
      valueBox(negative,"Negative Response")
    })
    
    output$nrow <- renderValueBox({
      
      #Number of records
      records<- nrow(df)
      # make value box
      valueBox(records,"Total number of responses")
    })
    
    output$bar <- renderPlot({
      
      #Bar chart
     ggplot(data=df,aes(text,fill=text))+geom_bar(aes(y = (..count..)/sum(..count..)))+geom_text(aes(y = ((..count..)/sum(..count..)), label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25)+xlab("Selected Feedback")+ylab("Response Proportion")
    })
    
    output$JigID<-renderTable({
      #Students who have given bad feedback
      values()[which(df[,"text"]=="BAD"),"sis_id"]
    })
    
  })
  
  observeEvent(input$file,{
    
    updateSelectInput(
      session, 
      inputId = "feeds",
      choices=names(values()),    
      selected=names(values()[,1])
    )})
  observeEvent(input$Go, {
  #Text analytics
  library(tm)
  feeds.corpus<-Corpus(VectorSource(values()[,input$feeds]))
  
  #Data Transformations -Cleaning
  feeds.corpus<-tm_map(feeds.corpus,tolower) #Converting to lower case
  feeds.corpus<-tm_map(feeds.corpus,stripWhitespace) #Removing extra white space
  feeds.corpus<-tm_map(feeds.corpus,removePunctuation) #Removing punctuations
  feeds.corpus<-tm_map(feeds.corpus,removeNumbers) #Removing numbers
  my_stopwords<-c(stopwords('english')) #Can add more words apart from standard list
  feeds.corpus<-tm_map(feeds.corpus,removeWords,my_stopwords)
  
  pos=c("good","very","best","satisfied","happy","excellent","nice","awesome","really","appreciate","easy","easier","precise","understand")
  neg=c("missed","lost","late","don't","not","tougher","difficult","bad","doesn't","difficulty","concern","uptick")
  
  #Famous Jeffreybreen Algorithm to "Tag" sentiments to sentences
  
  score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
  {
    require(plyr)
    require(stringr)
    
    #we got a vector of sentences. plyr will handle a list
    #or a vector as an "l" for us
    #we want a simple array of scores back, so we use
    #"l" + "a" + "ply" = "laply":
    scores = laply(sentences, function(sentence, pos.words, neg.words) {
      
      #clean up sentences with R's regex-driven global substitute, gsub():
      sentence = gsub('[[:punct:]]', '', sentence) #removes punctuations
      sentence = gsub('[[:cntrl:]]', '', sentence) #removes control characters
      sentence = gsub('\\d+', '', sentence) #removes digits
      
      #and convert to lower case:
      sentence = tolower(sentence)
      
      #split sentences into words. str_split is in the stringr package
      word.list = str_split(sentence, '\\s+')
      
      #sometimes a list() is one level of hierarchy too much
      words = unlist(word.list)
      
      #compare our words to the dictionaries of positive & negative terms
      pos.matches = match(words, pos.words)
      neg.matches = match(words, neg.words)
      
      #match() returns the position of the matched term or NA
      #we just want a TRUE/FALSE:
      pos.matches = !is.na(pos.matches)
      neg.matches = !is.na(neg.matches)
      
      #and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
      score = sum(pos.matches) - sum(neg.matches)
      
      return(score)
    }, pos.words, neg.words, .progress=.progress )
    
    scores.df = data.frame(score=scores, text=sentences)
    return(scores.df)
  }
  feeds.score<-score.sentiment(values()[,input$feeds], pos, neg, .progress="text")
  
  feeds.score$text<-as.character(feeds.score$text)
  feeds.score$sentiment<-ifelse(feeds.score$score>0,"positive",
                                ifelse(feeds.score$score<0,"negative","neutral"))
  #Classified feeds
  output$class_pos=renderValueBox({
    feeds.score%>%filter(sentiment=="positive")%>%count()
  })
  output$class_neg=renderValueBox({
    feeds.score%>%filter(sentiment=="negative")%>%count()
  })
  output$class_neu=renderValueBox({
    feeds.score%>%filter(sentiment=="neutral")%>%count()
  })
  })
  })

Please help.

KafeelBasha avatar Aug 07 '18 09:08 KafeelBasha

It is more than likely your UI file, not your server file.

Check the inputs your providing into the UI functions and make sure they're correctly declared. See example below where by not including title= to the dashboardHeader function returns the same error you're experiencing.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader("Invalid Title"),
  # dashboardHeader(title="Valid Title"),
  dashboardSidebar(),
  dashboardBody()
)

server <- function(input, output) { }
shinyApp(ui, server)
> runApp('apppp.R')
[1] "Invalid Title"
Error in FUN(X[[i]], ...) : Expected an object with class 'shiny.tag'.

JesseVent avatar Aug 25 '18 12:08 JesseVent

was there a solution to this error?

sanjmeh avatar Apr 12 '21 09:04 sanjmeh

This code has certainly some issues:

output$class_neu=renderValueBox({
    feeds.score%>%filter(sentiment=="neutral")%>%count()
  })

However I'm also getting this error basically when I don't have data :

    output$foo<- renderValueBox({
        if(react() %>% nrow() < 1){
            return()
        }
        
        valueBox(react()$Perf[1] %>% percent(accuracy = 0.1),
                 color = "yellow")
    })

In this case NULL is returned and the error appears. Not sure how to best handle that.

homer3018 avatar Apr 13 '21 07:04 homer3018

I see this also when I have a trailing comma sometimes -- removing the comma usually fixes it

nlarusstone avatar Apr 22 '21 19:04 nlarusstone

您好,丁永兵已经收到您的邮件,谢谢。

YongbingDing avatar Jan 06 '23 12:01 YongbingDing