I am trying to create a wordcloud shiny app after going through a DataCamp course. In the course they use a custom create_wordcloud() function to make the app. If someone has the code for it, it would make my life easier.
Anyways, I am trying to go about my own way since I don't have the custom function and will be using wordcloud2() function.
I am having trouble with using reactive functions to make the Shiny app. Essentially I am trying to make it so that the user can select the number of words and change the background in the wordcloud using the UI. For this to happen, I need to convert the text provided by the user a dataframe, order the df by word count, and then subset the df into what ever number the user selects in the app UI.
Here is my code:
library(shiny)
library(colourpicker)
library(tidyverse)
ui <- fluidPage(
h1("Word Cloud"),
sidebarLayout(
sidebarPanel(
# Add a textarea input
textAreaInput("text", "Enter text", rows = 7),
numericInput("num", "Maximum number of words", 25),
colourInput("col", "Background color", value = "white")
),
mainPanel(
wordcloud2Output("cloud")
)
)
)
server <- function(input, output) {
df_data <- reactive({
input$text %>%
term_stats(., drop_punct = TRUE, drop = stopwords_en) %>%
order_by(desc(count))
})
output$cloud <- renderWordcloud2({
# Use the textarea's value as the word cloud data source
wordcloud2(data = df_data()[1:input$num, ],
backgroundColor = input$col)
})
}
shinyApp(ui = ui, server = server)
The error that I get is:
Warning: Error in : Input must be a vector, not a function.
I really look forward to hearing answers from the community and improve my reactive programming skills!
Thank you!
Solution 1: Obtaining actual code
create_wordcloudfunction as follows:Solution 2: Provided by @Xiang in the comments section: