Set the numbers of words displayed in wordcloud in shiny app
I have the shiny
app below in which I want to set the count of words displayed in the wordcloud. Everytime the word is 1 no word is displayed in the wordcloud while 1 word would normally be displayed.
if(require(shiny)){
library(wordcloud2)
wordcloud2(data = demoFreq)
# Global variables can go here
n <- 1
# Define the UI
ui <- bootstrapPage(
sliderInput("max",
"Maximum Number of Words:",
min = 1, max = nrow(demoFreq), value =nrow(demoFreq),step=1 ),
wordcloud2Output('wordcloud2')
)
# Define the server code
server <- function(input, output) {
output$wordcloud2 <- renderWordcloud2({
# wordcloud2(demoFreqC, size=input$size)
wordcloud2(demoFreq[1:input$max,])
})
}
# Return a Shiny app object
# Sys.setlocale("LC_CTYPE","chs") #if you use Chinese character
## Do not Run!
shinyApp(ui = ui, server = server)
}
Here is a workaround to show only one word:
if(require(shiny)) {
library(wordcloud2)
wordcloud2(data = demoFreq)
# Global variables can go here
n <- 1
# Define the UI
ui <- bootstrapPage(
sliderInput(
"max",
"Maximum Number of Words:",
min = 1,
max = nrow(demoFreq),
value = nrow(demoFreq),
step = 1
),
uiOutput("wordcloud2ortext")
# wordcloud2Output('wordcloud2')
)
# Define the server code
server <- function(input, output) {
output$wordcloud2 <- renderWordcloud2({
# wordcloud2(demoFreqC, size=input$size)
wordcloud2(demoFreq[1:input$max, ])
})
output$wordcloud2ortext <- renderUI({
if (input$max == 1) {
wordcloud2(rbind(c(word = NA, freq = 0L), demoFreq[1:input$max, ]))
}
else{
wordcloud2(demoFreq[1:input$max, ])
}
})
}
# Return a Shiny app object
# Sys.setlocale("LC_CTYPE","chs") #if you use Chinese character
## Do not Run!
shinyApp(ui = ui, server = server)
}