Wordcloud in Shiny

Hi,

I hope you can help me out with my below request.

Thank you very much in advance for your time and help. Very much appreciated.

I’d like to generate my wordcloud in Shiny with a drop down menu where I can select the wordcloud I want to populate.

I have two wordclouds (“1”, “2”)

addresses <<- list("1" = "w1", "1 = "w2")

I have populate the below code which doesn’t let me populate the wordcloud and apply the drop down menu.

Shiny code

library(shiny)


ui <-  fluidPage(
 
  titlePanel("Wordcloud"),
  
  sidebarLayout(
   
    sidebarPanel(
      selectInput("selection", "Choose an address:",
                  choices = addresses),
      actionButton("update", "Change"),
      hr(),
      sliderInput("freq",
                  "Minimum Frequency:",
                  min = 1,  max = 50, value = 15),
      sliderInput("max",
                  "Maximum Number of Words:",
                  min = 1,  max = 300,  value = 100)
    ),
    
   
    mainPanel(
      plotOutput("plot")
    )
  )
)


``
server<- function(input, output, session) {
 
  terms <- reactive({
   
    input$update
    
    isolate({
      withProgress({
        setProgress(message = "Processing corpus...")
        input$selection
      })
    })
  })
  
  
  wordcloud_rep <- repeatable(wordcloud)
  
  output$plot <- renderPlot({
    v <- terms()
    wordcloud_rep(names(v), v, scale=c(4,0.5),
                  min.freq = input$freq, max.words=input$max,
                  colors=brewer.pal(8, "Dark2"))
  })
}

shinyApp(ui, server)

Thanks,
Juanma

Are you saying you have two variables, w1 and w2?

As you've no doubt noticed, input$selection just contains the string value that's in the selectInput, so in this case, "w1" or "w2". If you want to make these correspond to data you have on the server side, you need to look up the data somehow. Here's one possibility. Define this globally:

corpuses <- list(w1 = w1, w2 = w2)

And then in your terms reactive, you'd have code like this instead of input$selection:

corpus <- corpuses[[input$selection]]
if (is.null(corpus))
  stop("Invalid selection!")
corpus

Hope that helps.

1 Like