. Trying to create a shiny app that will generate different word clouds according to the variable selections made by users. So far, I have been able to produce the clouds, but it is the findAssocs()
part that is giving problems - only returning $word
and numeric(0)
.
#> **Warning:** Error in findAssocs: object 'dtm' not found.
I tried it without the filters and got meaningful outputs for findAssocs()
.
Would be very grateful for some help.
Here is the reprex-..
Agegroup <- c("A","B","D","C","E","B","A","B","D","E")
Region <- c("N","S","E","W","W","N","S","E","S","E")
Word <- c("raining cats and dogs", "rabbit out of a hat", "cats with nine lives", "a bear hug",
"elephant in the room", "white elephant", "dogs bark, cats meow",
"a life worth living", "hello", "gold fish")
Word2 <- c("raining cats and dogs", "rabbit out of a hat", "cats with nine lives", "a bear hug",
"elephant in the room", "white elephant", "dogs bark, cats meow",
"a life worth living", "gold fish", "hello")
Data <- data.frame(Agegroup,Region,Word, Word2, stringsAsFactors=FALSE)
ui <- fluidPage(
titlePanel("Big and small pets"),
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "source",
label = "Select Question",
choices = c("Why are you satisfied or not satisfied with service?" = "satisfy",
"Reasons for recommending or not recommending business" = "recommend")),
selectInput("region",
"Select region:",
choices = c("total", "N", "S", "E", "W"),
selected = "total"),
selectInput("group",
"Select age group:",
choices = c("total", "A","B","C","D","E"),
selected = "total"),
),
mainPanel(
wordcloud2Output("cloud"),verbatimTextOutput("heading2")
)
)
)
server <- function(input, output) {
output$cloud <- renderWordcloud2({
Data <- Data%>%
dplyr::select(Region, Word, Word2, Agegroup)
if(input$region == "total"){
Data <- Data
}
else if(input$region != "total"){
Data <- Data%>%
subset(Region == input$region)
}
if(input$group == "total"){
Data <- Data
}
else if(input$group != "total"){
Data <- Data%>%
subset(Agegroup == input$group)
}
if (input$source == "satisfy"){
text <- Data%>%
select(Word)}
else if (input$source == "recommend"){
text <- Data%>%
select(Word2)}
docs <- Corpus(VectorSource(text))
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/")
docs <- tm_map(docs, toSpace, "@")
docs <- tm_map(docs, toSpace, "\\|")
docs <- tm_map(docs, content_transformer(tolower))
docs <- tm_map(docs, removeNumbers)
docs <- tm_map(docs, removeWords, stopwords("english"))
docs <- tm_map(docs, removeWords, c("blabla1", "blabla2"))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, stripWhitespace)
docs <- tm_map(docs, stemDocument)
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m), decreasing=TRUE)
d <- data.frame(word = names(v), freq=v)
set.seed(1234)
isolate({
wordcloud2(data = d, size = 0.5, shape = "circle")
})
})
output$heading2 <- renderPrint({
findAssocs(dtm, "cat", corlimit = 0.3)
})
}
shinyApp(ui = ui, server = server)