在 shiny app 中设置 wordcloud 中显示的字数

Set the numbers of words displayed in wordcloud in shiny app

我有下面的 shiny 应用程序,我想在其中设置字云中显示的字数。每次单词为1时,词云中不显示任何单词,而正常情况下会显示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 ),            
    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)
}

这里有一个只显示一个词的解决方法:

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