如果不是R闪亮的声明

时间:2019-01-23 08:48:16

标签: r shiny word-cloud

在R Shiny中绘制wordcloud和barplot时,我被感动了一点。如果在服务器中,我们应该在MainPanel UI.R上给出什么命令。如果条件显示wordcloud,则应该给出Text。请在下面找到我的代码。 服务器代码正在运行,但Web应用程序未显示文本。请帮助我哪里出问题了。我是R的新手。

shinyUI(fluidPage(

  titlePanel("WordCloud"),
sidebarLayout(
sidebarPanel(
  fileInput("wc", "Upload a file for WordCloud", multiple = F, accept = "text/plain"),
  actionButton("update", "Create WordCloud"),
  hr(),
  sliderInput("freq", "Minimum Frequency", min = 1, max = 10, value = 3),
  sliderInput("max", "Maximum Number of Words:", min = 1, max = 300, value = 100)


),
 mainPanel( 
  tabsetPanel(type = "tab",
          tabPanel("WordCloud",plotOutput("wcplot")),
          tabPanel("Barplot", plotOutput("barchart"))   
  ))
  )))


shinyServer(function(input, output) {

 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, stripWhitespace)

    bigrams = textcnt(wc_corpus_clean, n = 2, method = "string")
    bigrams = bigrams[order(bigrams, decreasing = TRUE)]
    test_data = data.frame(bigrams = names(bigrams), freq = bigrams)
  })
})
  })

  wordcloud_rep = repeatable(wordcloud)

  output$wcplot = renderPlot({
    withProgress({
     setProgress(message = "Creating WordCloud....")
     wc_corpus_clean = wc_data()
  print(wc_corpus_clean)
  max_freq = max(wc_corpus_clean$freq) 
  if (input$freq > max_freq){
   paste("Please reduce the frequency")}
  else

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


  output$barchart = renderPlot({
   test_data = wc_data()
   ggplot(head(test_data,15), aes(reorder(bigrams,freq),freq, fill = bigrams)) +  
  geom_bar(stat = "identity",
           width = 0.5,
           # color = "black",
           size = 1) +
  coord_flip() + theme_minimal()+
  xlab("Bigrams") + ylab("Frequency") +
  ggtitle("Most frequent bigrams (15)")
 })
})

0 个答案:

没有答案