闪亮的应用程序中未显示 Wordcloud 图

Wordcloud plot is not displayed in shiny app

我有下面这个闪亮的应用程序,我在其中尝试根据侧边栏的闪亮小部件创建电子邮件列的文字云。用户将更改标签,然后更改最大单词数和最小频率。我完全按照 this app 的示例,但无法显示 wordcloud。

## app.R ##
library(shiny)
library(shinydashboard)
library(ggplot2)
library(readr)
library(tm)
library(wordcloud)
library(memoise)
df<-structure(list(email = c("' hi i think you need to give us a little more detailed information on wed NUMBER dec NUMBER gianni ponzi wrote i have a prob when trying to install linux tried redhat suse on my laptop you get _exactly_ the same problem with suse and redhat is that right what versions of these have you used i can start the install but after about NUMBERmin the whole pc just dies as in freezes reboots do you get any errors i know it s not a linux prob and here is what i have encountered i had the same problem when installing win on it and eventually sorted it out by disabling the infrared port did you disable it in the bios or in windows i m guessing this might be same prob although i m not sure i am very new to linux so it s not that easy for me to work it out i did manage to follow the setup procedure at one stage using images on disks and it cuts out either as it s trying to verify what cd rom i have or just after hence my suspicion of the infrared port again NUMBER what laptop is it what is the processor and how much ram does it have NUMBER you cannot simply boot off the cdrom it must be quite an old laptop then or else cdrom booting is disabled in the bios is the cdrom external or strange in some way gavin irish linux users group ilug URL URL for un subscription information list maintainer listmaster URL '", 
                             "' save up to NUMBER on life insurance why spend more than you have to life quote savings ensuring your family s financial security is very important life quote savings makes buying life insurance simple and affordable we provide free access to the very best companies and the lowest rates life quote savings is fast easy and saves you money let us help you get started with the best values in the country on new coverage you can save hundreds or even thousands of dollars by requesting a free quote from lifequote savings our service will take you less than NUMBER minutes to complete shop and compare save up to NUMBER on all types of life insurance hyperlink click here for your free quote protecting your family is the best investment you ll ever make if you are in receipt of this email in error and or wish to be removed from our list hyperlink please click here and type remove if you reside in any state which prohibits e mail solicitations for insurance please disregard this email '"
), label = c("ham", "spam")), row.names = c(NA, -2L), class = c("tbl_df", 
                                                                "tbl", "data.frame"))
 

ui <- dashboardPage(
  dashboardHeader(title = "Text Classification"),
  dashboardSidebar(
    selectInput("selection", "Choose spam or ham:",
                choices = unique(df$label),
                selected = "spam"
                ),
    sliderInput("freq",
                "Minimum Frequency:",
                min = 1,  max = 50, value = 15),
    sliderInput("max",
                "Maximum Number of Words:",
                min = 1,  max = 300,  value = 100),
    actionButton("update", "Change")
  ),
  
  dashboardBody(
      plotOutput("word"),

  )
)

server <- function(input, output) {
  
  emails <<- list(df$email)
  # Using "memoise" to automatically cache the results
  getTermMatrix <- memoise(function(email) {
    # Careful not to let just any name slip in here; a
    # malicious user could manipulate this value.
    if (!(email %in% emails))
      stop("Unknown email")
    
    text <- readLines(sprintf("./%s.txt.gz", email),
                      encoding="UTF-8")
    
    myCorpus = Corpus(VectorSource(text))
    myCorpus = tm_map(myCorpus, content_transformer(tolower))
    myCorpus = tm_map(myCorpus, removePunctuation)
    myCorpus = tm_map(myCorpus, removeNumbers)
    myCorpus = tm_map(myCorpus, removeWords,
                      c(stopwords("SMART"), "thy", "thou", "thee", "the", "and", "but"))
    
    myDTM = TermDocumentMatrix(myCorpus,
                               control = list(minWordLength = 1))
    
    m = as.matrix(myDTM)
    
    sort(rowSums(m), decreasing = TRUE)
  })
  
  function(input, output, session) {
    # Define a reactive expression for the document term matrix
    terms <- reactive({
      # Change when the "update" button is pressed...
      input$update
      # ...but not for anything else
      isolate({
        withProgress({
          setProgress(message = "Processing corpus...")
          getTermMatrix(input$selection)
        })
      })
    })
    
    # Make the wordcloud drawing predictable during a session
    wordcloud_rep <- repeatable(wordcloud)
    
    output$word <- renderPlot({
      v <- terms()
      wordcloud_rep(names(v), v, scale=c(4,0.5),
                    min.freq = input$freq, max.words=input$max,
                    colors=brewer.pal(8, "Dark2"))
    })
  }
}

shinyApp(ui, server)

一个问题是您没有任何 .txt.gz 文件(这些是作者的书籍)。因此,text <- readLines() 不会 return 什么。 试试下面的代码。

## app.R ##
library(shiny)
library(shinydashboard)
library(ggplot2)
library(readr)
library(tm)
library(wordcloud)
library(memoise)
library(Rcpp)
df<-structure(list(email = c("' hi i think you need to give us a little more detailed information on wed NUMBER dec NUMBER gianni ponzi wrote i have a prob when trying to install linux tried redhat suse on my laptop you get _exactly_ the same problem with suse and redhat is that right what versions of these have you used i can start the install but after about NUMBERmin the whole pc just dies as in freezes reboots do you get any errors i know it s not a linux prob and here is what i have encountered i had the same problem when installing win on it and eventually sorted it out by disabling the infrared port did you disable it in the bios or in windows i m guessing this might be same prob although i m not sure i am very new to linux so it s not that easy for me to work it out i did manage to follow the setup procedure at one stage using images on disks and it cuts out either as it s trying to verify what cd rom i have or just after hence my suspicion of the infrared port again NUMBER what laptop is it what is the processor and how much ram does it have NUMBER you cannot simply boot off the cdrom it must be quite an old laptop then or else cdrom booting is disabled in the bios is the cdrom external or strange in some way gavin irish linux users group ilug URL URL for un subscription information list maintainer listmaster URL '", 
                             "' save up to NUMBER on life insurance why spend more than you have to life quote savings ensuring your family s financial security is very important life quote savings makes buying life insurance simple and affordable we provide free access to the very best companies and the lowest rates life quote savings is fast easy and saves you money let us help you get started with the best values in the country on new coverage you can save hundreds or even thousands of dollars by requesting a free quote from lifequote savings our service will take you less than NUMBER minutes to complete shop and compare save up to NUMBER on all types of life insurance hyperlink click here for your free quote protecting your family is the best investment you ll ever make if you are in receipt of this email in error and or wish to be removed from our list hyperlink please click here and type remove if you reside in any state which prohibits e mail solicitations for insurance please disregard this email '"
), label = c("ham", "spam")), row.names = c(NA, -2L), class = c("tbl_df", 
                                                                "tbl", "data.frame"))

getTermMatrix <- function(email_type){

  em <- df$email[df$label == email_type]
  
  myCorpus = VCorpus(VectorSource(em))
  
  myCorpus = tm_map(myCorpus, content_transformer(tolower))
  myCorpus = tm_map(myCorpus, removePunctuation)
  myCorpus = tm_map(myCorpus, removeNumbers)
  myCorpus = tm_map(myCorpus, removeWords,
                    c(stopwords("SMART"), "thy", "thou", "thee", "the", "and", "but"))
  
  myDTM = TermDocumentMatrix(myCorpus,
                             control = list(minWordLength = 3))
  
  m = as.matrix(myDTM)
  
  v <- sort(rowSums(m), decreasing = TRUE)
  return(v)
}


ui <- dashboardPage(
  dashboardHeader(title = "Text Classification"),
  dashboardSidebar(
    selectInput("selection", "Choose spam or ham:",
                choices = unique(df$label),
                selected = "spam"
    ),
    sliderInput("freq",
                "Minimum Frequency:",
                min = 1,  max = 50, value = 15),
    sliderInput("max",
                "Maximum Number of Words:",
                min = 1,  max = 300,  value = 100),
    actionButton("update", "Change")
  ),
  
  dashboardBody(
    plotOutput("word"),
    
  )
)

server <- function(input, output, session) {
    # Define a reactive expression for the document term matrix
    terms <- reactive({
      # Change when the "update" button is pressed...
      input$update
      # ...but not for anything else
      isolate({
        withProgress({
          setProgress(message = "Processing corpus...")
          getTermMatrix(input$selection)
        })
      })
    })
    
    # Make the wordcloud drawing predictable during a session
    wordcloud_rep <- repeatable(wordcloud)
    
    output$word <- renderPlot({
      v <- terms()
      wordcloud_rep(names(v), v, scale=c(4,0.5),
                    min.freq = input$freq, max.words=input$max,
                    colors=brewer.pal(8, "Dark2"))
    })
  }


shinyApp(ui, server)