2
votes

I have created an app in R shiny with the following dataframe as input

    DF <-
           variable   estimatesum
 1              get   0.2468349025
 2              new   0.1676732365
 3              buy   0.1028425291
 4            first   0.0823390287
 5            cover   0.0408254469
 6           perfect  -0.0299153527

These are a set of words from a term document matrix. I have made a dataframe with a series of percentages associated with the same. next I am creating a ui and server to process a text to the server and yield a result

    ui<- fluidPage(
   textInput("message", label = "message",placeholder = 
          "'"),actionButton("do", "Click Me"),
    numericInput("size", label = "size", value = 
             c(10000000)),
   dateInput("Date",label = "Date"),
   mainPanel(verbatimTextOutput("Output1")))

Next I am creating a server script. On pressing the actionButton above, the data should be fed into the server and the output should be displayed in the main panel

   server<-function(input, output, session) {
     text2<-reactive({input$message
     mycorpus2<-Corpus(VectorSource(text2))
     mycorpus2<-tm_map(mycorpus2, tolower)
    mycorpus2<-tm_map(mycorpus2, removeNumbers)
    mycorpus2<-tm_map(mycorpus2, removeWords, c(stopwords("english")))
    dtm2<-TermDocumentMatrix(mycorpus2)
    m2<-as.matrix(dtm2)
    v2 <- sort(rowSums(m2),decreasing=TRUE)
    d2 <- data.frame(word = names(v2),freq=v2)
    outputtable<-DF4[DF4$variable%in%d2$word,]
    return(output)

     })
   output$Output1=renderPrint({sum(outputtable$estimatesum)})}


   shinyApp(ui, server)

I am getting the following error repeatedly.

 object 'outputtable' not found.

It seems the data from the input are not getting fed into the server. I am unable to identify the error and request some help here. Will be thankful. I am a newbie to r shiny

1
When showing including sample data, could you use something like dput(head(x,n=10)) or actual calls to data.frame or read.table? It is much faster (for us) to copy/paste something like that; further, some aspects of the data can be masked when given in this format. - r2evans
Apologies. Will bear that in mind - Raghavan vmvs

1 Answers

1
votes

Does this help?

server <- function(input, output) {
  text2 <- reactive({
    mycorpus2 <- Corpus(VectorSource(input$message))
    mycorpus2 <- tm_map(mycorpus2, tolower)
    mycorpus2 <- tm_map(mycorpus2, removeNumbers)
    mycorpus2 <- tm_map(mycorpus2, removeWords, c(stopwords("english")))
    dtm2 <- TermDocumentMatrix(mycorpus2)
    m2 <- as.matrix(dtm2)
    v2 <- sort(rowSums(m2), decreasing=TRUE)
    d2 <- data.frame(word = names(v2), freq = v2)
    outputtable <- DF[DF$variable %in% d2$word, ]
  })
  output$Output1 <- renderPrint({sum(text2()$estimatesum)})}

I have made slight changes in your server script:

  • In renderPrint you would need to call your reactive function i.e. text2()
  • In Corpus you would need to pass input$message


Update:

Complete code for OP's reference:

df <- structure(list(variable = structure(c(4L, 5L, 1L, 3L, 2L, 6L), .Label = c("buy", 
                                                                                "cover", "first", "get", "new", "perfect"), class = "factor"), 
                     estimatesum = c(0.2468349025, 0.1676732365, 0.1028425291, 
                                     0.0823390287, 0.0408254469, -0.0299153527)), .Names = c("variable", 
                                                                                             "estimatesum"), class = "data.frame", row.names = c("1", "2", 
                                                                                                                                                 "3", "4", "5", "6"))

ui<- fluidPage(
  textInput("message", label = "message",placeholder = 
              "'"),actionButton("do", "Click Me"),
  numericInput("size", label = "size", value = 
                 c(10000000)),
  dateInput("Date",label = "Date"),
  mainPanel(verbatimTextOutput("Output1")))

server <- function(input, output) {
  text2 <- reactive({
    mycorpus2 <- Corpus(VectorSource(input$message))
    mycorpus2 <- tm_map(mycorpus2, tolower)
    mycorpus2 <- tm_map(mycorpus2, removeNumbers)
    mycorpus2 <- tm_map(mycorpus2, removeWords, c(stopwords("english")))
    dtm2 <- TermDocumentMatrix(mycorpus2)
    m2 <- as.matrix(dtm2)
    v2 <- sort(rowSums(m2), decreasing=TRUE)
    d2 <- data.frame(word = names(v2), freq = v2)
    outputtable <- df[df$variable %in% d2$word, ]
  })
  output$Output1 <- renderPrint({sum(text2()$estimatesum)})
  }

shinyApp(ui, server)