R闪亮:用TermDocumentMatrix封闭两倍?

时间:2014-08-14 02:35:29

标签: r shiny

我尝试使用this solution作为我的问题的指南无济于事,并希望有人能够轻易看到我的错误。

我在as.double(y)中遇到" 错误的相同错误:无法强制键入'关闭'对于' double' "类型的向量,但我尝试使用Shiny来plot a TermDocumentMatrix。我的代码如下:

ui.R

library(shiny)
library(shinyIncubator)

shinyUI(navbarPage("Test", id="nav",
  tabPanel("Correlation Plot",
  progressInit(),

  # Sidebar with a slider and selection inputs
  sidebarPanel(width = 5,
               selectInput("selection", "Choose a reason:", 
                           choices = books),
               actionButton("update", "Change"),
               hr(),
               sliderInput("freq", 
                           "Minimum Frequency:", 
                           min = 1,  max = 50, value = 15),
               sliderInput("correl", 
                           "Correlation Threshold:", 
                           min = 0,  max = 1,  value = 0.8)
  ),

  # Show plot
  mainPanel(
    plotOutput("cplot")
  )
  )
))

global.R

library(tm)
library(wordcloud)
library(memoise)

# The list of valid books
books <<- list("1" = "1", "2"= "2")

# Using "memoise" to automatically cache the results
getTermDoc <- memoise(function(book) {
  if (!(book %in% books))
    stop("Unknown book")

  text = read.table(sprintf("./%s.txt", book),sep="\r",quote="")

  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,stopwords("english"))
  myDTM = TermDocumentMatrix(myCorpus)

})

server.R

library(shiny)
library(shinyIncubator)
library(tm)

shinyServer(function(input, output, session) {
  # Define a reactive expression for the document term matrix
   doc <- reactive({
    # Change when the "update" button is pressed...
    input$update
    # ...but not for anything else
    isolate({
      withProgress(session, {
        setProgress(message = "Processing corpus...")
        getTermDoc(input$selection)
      })
    })
  })

  plot_rep <- repeatable(plot)
  #correlation plot
    output$cplot <- renderPlot({
        plot_rep(doc, 
            terms = findFreqTerms(doc, lowfreq = 500),
            #terms = findFreqTerms(doc, lowfreq = input$freq),          
            #weighting = TRUE,
            corThreshold = 0.975
            #corThreshold = input$correl
            #nodeAttrs=list(fillcolor=vc)
            )
  })

})

1 个答案:

答案 0 :(得分:4)

致电时

doc <- reactive({...})

reactive调用本质上是返回一个函数,您必须调用该函数才能获得doc的值。而不是

plot_rep(doc, 
    terms = findFreqTerms(doc, lowfreq = 500)

使用

plot_rep(doc(), 
    terms = findFreqTerms(doc()), lowfreq = 500)