无法在闪亮的应用程序中呈现表格

时间:2017-07-07 15:38:51

标签: r nlp shiny

我想在我的情节下使用以下代码获得数据表。

Global.R

library(tm)
library(wordcloud)
library(memoise)
library(SnowballC)
library(dplyr)


# The list of valid books
books <<- list("Jane Eyre" = "JaneEyre",
           "Wuthering Heights" = "WutheringHeights")

 # Using "memoise" to automatically cache the results
 getTermMatrix <- memoise(function(book) {
 # Careful not to let just any name slip in here; a
 # malicious user could manipulate this value.
 if (!(book %in% books))
    stop("Unknown book")

 text <- readLines(sprintf("./%s.txt", book),
                  encoding="UTF-8")

 myCorpus = Corpus(VectorSource(text)) %>%
    tm_map(removePunctuation) %>%
    tm_map(removeNumbers) %>%
    tm_map(content_transformer(tolower)) %>%
    tm_map(removeWords, stopwords("english")) %>%
    tm_map(stripWhitespace) %>%
    tm_map(stemDocument)


 myDTM = TermDocumentMatrix(myCorpus,
                           control = list(minWordLength = 1))


 # Create term-document matrices & remove sparse terms
 tdm <- DocumentTermMatrix(myCorpus) %>%
    removeSparseTerms(1 - (5/length(myCorpus)))

 # Calculate and sort by word frequencies
 word.freq <- sort(colSums(as.matrix(tdm)), 
                    decreasing = T)

 # Create frequency table
 tablewc <- data.frame(word = names(word.freq), 
                      absolute.frequency = word.freq, 
                      relative.frequency = 
                          word.freq/length(word.freq))

 # Remove the words from the row names
 rownames(tablewc) <- NULL

    # Show the 10 most common words
    #head(tablewc, 10)

    m = as.matrix(myDTM)

    sort(rowSums(m), decreasing = TRUE)
})

Ui.R

  fluidPage(
             # Application title
            titlePanel("Word Cloud"),

  sidebarLayout(
    # Sidebar with a slider and selection inputs
    sidebarPanel(
        selectInput("selection", "Choose a book:",
                    choices = books),
        actionButton("update", "Change"),
        hr(),
        sliderInput("freq",
                    "Minimum Frequency:",
                    min = 50,  max = 400, value = 150),
        sliderInput("max",
                    "Maximum Number of Words:",
                    min = 50,  max = 250,  value = 100)
    ),

    mainPanel(
        h3(plotOutput("plot")),

        tableOutput("view")

    # Show Word Cloud
    #mainPanel(
     #   plotOutput("plot")
          )
       )
  )

Server.R

 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$view <- renderTable({
      head(tablewc,50)
      })

     output$plot <- 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"))

        })
   }

代码工作正常,但我收到错误: runApp(&#39; wordcloud&#39)

聆听http://127.0.0.1:3916 警告:head:object&#39; tablewc&#39;找不到

请帮忙。

数据可以从www.gutenberg.com下载 书名简爱和Wutheringheights

0 个答案:

没有答案