Home > database >  Display a text message when wordcloud is not diplayed in shiny app
Display a text message when wordcloud is not diplayed in shiny app

Time:01-12

In the shiny app below I display a wordcloud and a slider that sets the number of words displayed. When this number =1 (input$max==1) nothing is displayed though. So I would like to replace this wordcloud I have now ith a renderUI() that will display a message when input$max==1 and in the other cases the wordcloud will 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 ),  
    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){
        "No words found"
      }
      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)
}

CodePudding user response:

You just need to comment out the other output (wordcloud2Output):

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) {
        strong("No words found")
      }
      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)
}
  •  Tags:  
  • Related