Wordcloud with Shiny

Codes look good, but I have no idea how to change terms based on the selected file. Should I create another server() function and a function for another variable?

library(shiny)
library(wordcloud)
library(tm)
library(stopwords)

ui <- fluidPage(
  titlePanel("WordCloud"),
  sidebarLayout(
    mainPanel(plotOutput(outputId = "wordcloud")),
    sidebarPanel(radioButtons(inputId = "variable",
                              label = "Choose a variable:",
                              choices = c("review_title" = "title",
                                          "review_content" = "content"),
                              selected = "title"),
                 sliderInput(inputId = "min.freq",
                             label = "Minimum Frequency:",
                             min = 1,  max = 100, value = 50),
                 sliderInput(inputId ="max.words",
                             label = "Maximum Number of Words:",
                             min = 1,  max = 100,  value = 50),
    )
  )
)

readData <- function() {
  data <- read.csv("amazon.csv")
  reviews <- data$review_title
  texts.corpus <- Corpus(VectorSource(reviews))
  texts.corpus <- tm_map(texts.corpus, content_transformer(tolower))
  texts.corpus <- tm_map(texts.corpus, removePunctuation)
  texts.corpus <- tm_map(texts.corpus, removeNumbers)
  texts.corpus <- tm_map(texts.corpus, removeWords, stopwords("en"))
  texts.corpus <- tm_map(texts.corpus, removeWords, c("can", "just", "amazon", "product", "overall"))
  tdm <- TermDocumentMatrix(texts.corpus)
  terms <- as.matrix(tdm)
  textCounts <- sort(rowSums(terms), decreasing = T)
  
  reviews2 <- data$review_content
  texts.corpus2 <- Corpus(VectorSource(reviews2))
  texts.corpus2 <- tm_map(texts.corpus2, content_transformer(tolower))
  texts.corpus2 <- tm_map(texts.corpus2, removePunctuation)
  texts.corpus2 <- tm_map(texts.corpus2, removeNumbers)
  texts.corpus2 <- tm_map(texts.corpus2, removeWords, stopwords("en"))
  texts.corpus2 <- tm_map(texts.corpus2, removeWords, c("can", "just", "use", "product", "order", "purchased", "overall"))
  tdm2 <- TermDocumentMatrix(texts.corpus2)
  terms2 <- as.matrix(tdm2)
  textCounts2 <- sort(rowSums(terms2), decreasing = T)
  return(c(textCounts, textCounts2))
}

server <- function(input, output) {
  df <- readData()
  output$wordcloud <- renderPlot({
    wordcloud(names(df), df, colors = brewer.pal(3, "Dark2"), random.order = F, min.freq = input$min.freq, max.words = input$max.words)
  })
}

shinyApp(ui = ui, server = server)

Advice : study mastering shiny; and understand reactivity.
Chapter 3 Basic reactivity | Mastering Shiny (mastering-shiny.org)

Despite updating my codes, I still struggle with this. In trying to run my code, the Wordcloud terms do not appear. Do you know what I'm doing wrong?

readData <- function() {
  data <- read.csv("amazon.csv")
  reviews <- data$review_title
  texts.corpus <- Corpus(VectorSource(reviews))
  texts.corpus <- tm_map(texts.corpus, content_transformer(tolower))
  texts.corpus <- tm_map(texts.corpus, removePunctuation)
  texts.corpus <- tm_map(texts.corpus, removeNumbers)
  texts.corpus <- tm_map(texts.corpus, removeWords, stopwords("en"))
  texts.corpus <- tm_map(texts.corpus, removeWords, c("can", "just", "amazon", "product", "overall"))
  tdm <- TermDocumentMatrix(texts.corpus)
  terms <- as.matrix(tdm)
  textCounts <- sort(rowSums(terms), decreasing = T)
  return(textCounts)
}

readData2 <- function() {
  data <- read.csv("amazon.csv")
  reviews <- data$review_content
  texts.corpus <- Corpus(VectorSource(reviews))
  texts.corpus <- tm_map(texts.corpus, content_transformer(tolower))
  texts.corpus <- tm_map(texts.corpus, removePunctuation)
  texts.corpus <- tm_map(texts.corpus, removeNumbers)
  texts.corpus <- tm_map(texts.corpus, removeWords, stopwords("en"))
  texts.corpus <- tm_map(texts.corpus, removeWords, c("can", "just", "use", "product", "order", "purchased", "overall"))
  tdm <- TermDocumentMatrix(texts.corpus)
  terms <- as.matrix(tdm)
  textCounts <- sort(rowSums(terms), decreasing = T)
  return(textCounts)
}

server <- function(input, output) {
  df_source <- reactive({
    if (input$variable == "title"){
      df <- readData()
      output$wordcloud <- renderPlot({
        wordcloud(names(df), df, colors = brewer.pal(3, "Dark2"), random.order = F, min.freq = input$min.freq, max.words = input$max.words)
      })
    } else {
      df <- readData2()
      output$wordcloud <- renderPlot({
        wordcloud(names(df), df, colors = brewer.pal(3, "Dark2"), random.order = F, min.freq = input$min.freq, max.words = input$max.words)
      })
    }
    return(df)
  })
}

you have output$ <- render*() definitions within Reactives ...
unless you are a a seasoned expert and are knowingly breaking the rules you should never do such a thing. This is not taught in that textbook and there wont be examples of that in the text book.
You need to think of constructing chains of reactivity. this depends on that because this mentions that. the definitions of this and that are seperate and dont overlap.

Your example code needs a lot of work to fix it up, and thats why I'm not immediately pulling it up and addressing it; also because its no fun to attempt such a thing without example data.
I dont have amazon.csv so it would take a substantial effort to make the code run at all, before looking to improve it.

My first time using shiny to create a word cloud, and I understand the idea of reactivity, but the terms won't appear even after trying to fix my codes with your help

server <- function(input, output) {
  df_source <- reactive({
    if (input$variable == "title"){
      df <- readData()
      return(df)
    } else {
      df <- readData2()
      return(df)
    }})
  df <- df_source
  output$wordcloud <- renderPlot({
    wordcloud(names(df), df, colors = brewer.pal(3, "Dark2"), random.order = F, min.freq = input$min.freq, max.words = input$max.words)
  })
}

Probably

server <- function(input, output) {
  df_source <- reactive({
    if (input$variable == "title"){
      df <- readData()
      return(df)
    } else {
      df <- readData2()
      return(df)
    }})
  
  output$wordcloud <- renderPlot({
df <- req(df_source())
    wordcloud(names(df), df, colors = brewer.pal(3, "Dark2"), random.order = F, min.freq = input$min.freq, max.words = input$max.words)
  })
}

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.