I've tried using this solution as a guide for my problem to no avail and was hoping someone could easily see my mistake.
I'm getting the same error of "Error in as.double(y) : cannot coerce type 'closure' to vector of type 'double'", but I'm trying to plot a TermDocumentMatrix by using Shiny. My code is as follows:
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)
)
})
})