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)