闪亮的应用程序中未显示 wordcloud 时显示文本消息

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

在下面闪亮的应用程序中,我显示了一个词云和一个设置显示的词数的滑块。当此数字 =1 (input$max==1) 时,不会显示任何内容。所以我想用 renderUI() 替换这个词云,它会在 input$max==1 时显示一条消息,在其他情况下会显示词云。

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)
}

您只需要注释掉其他输出 (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)
}