如何在RShiny的单个输入列表中添加多个单词并将其添加到Wordcloud的停用词列表中

时间:2019-04-01 12:28:00

标签: r stop-words word-cloud

我已经在R Shiny中创建了一个Web应用程序来创建词云。用户界面具有输入字段,供用户添加内置词典以外的其他停用词。词云将是单个词/二字组/三字组词云。到目前为止,我已经成功了,输入字段中的多个单词将直接添加到停用词列表中,并创建单词云。但是困难在于二字组/三字组单词。 向遇到类似问题或知道解决方案的任何人寻求帮助。 请在下面找到我的代码。

shinyServer(function(input, output, session) {

  wc_data = reactive({

input$update

isolate({

  withProgress({
    setProgress(message = "Processing Corpus...")
    wc_file = input$wc
    if(!is.null(wc_file)){
      wc_text = readLines(wc_file$datapath)
    }
    else
    {
      wc_text = "A wordcloud is an shape image made of words that together resemble a cloudy shape image."
    }
wc_corpus_clean = tm_map(wc_corpus_clean, removeWords, c("since", "for", "this", "like", "that", "our", "united states", "will", "america", "s", "ve", "'",unlist(strsplit(input$text, "[,]"))))
makeplot <- function() {
stpwords = unlist(strsplit(input$text, "[,]"))   
withProgress({
  setProgress(message = "Creating WordCloud....")
  wc_corpus_1 = wc_data()
  bigrams = textcnt(wc_corpus_1, n = input$gram, method = "string")
  bigrams = bigrams[order(bigrams, decreasing = TRUE)]

  wc_corpus_clean = data.frame(bigrams = names(bigrams), freq = bigrams)
  if (input$gram > 1) {

  wc_corpus_new = wc_corpus_clean
  ronum = which(wc_corpus_new$bigrams  == stpwords)
  print(ronum)
  if (length(ronum) > 0) {
     wc_corpus_clean = wc_corpus_new[-ronum,]
   }
  }

  max_freq = max(wc_corpus_clean$freq) 
  print(max_freq)
  if (input$freq > max_freq){
    NULL }
  else
    par(mar=c(1,1,1,1))
  wordcloud(wc_corpus_clean$bigrams,wc_corpus_clean$freq, min.freq = input$freq, scale = c(2,0.9), max.words = input$max, colors = brewer.pal(8,"Dark2"),rot.per = 0.45,random.order = FALSE, random.color = TRUE)
})
  }

0 个答案:

没有答案