0
我正試圖用反應詞雲構建一個Shiny應用程序。截至目前,它只產生一個靜態詞雲,當我選擇不同的輸入時不會改變。輸出Wordcloud不會更新閃亮
這是我使用的軟件包:
library(shiny)
library(tm)
library(wordcloud)
library(SnowballC)
library(memoise)
ui.R
ui <- fluidPage(
# Application title
titlePanel("Word Cloud"), sidebarLayout(
# Sidebar with a slider and selection inputs
sidebarPanel(
selectInput("selection", "Choose an agency:", choices = Agencies),
actionButton("update", "Change"),
hr(),
sliderInput("freq",
"Minimum Frequency:",
min = 1, max = 50, value = 15),
sliderInput("max", "Maximum Number of Words:", min = 1, max = 300, value = 100)),
# Show Word Cloud
mainPanel(
plotOutput("plot"))))
server.R
server <- function(input, output) {
# Define a reactive expression for the document term matrix
terms <- reactive({
input$update
# ...but not for anything else
Agencies <<- list("NASA" = "NASA", "DOD" = "DOD")
getTermMatrix <- function(Agency) {
if(!(Agency %in% Agencies))
stop("Unknown Agency")
PropCorpus <- Corpus(VectorSource(x$Proposal.Title))
PropCorpus <- tm_map(PropCorpus, PlainTextDocument)
myCorpus = Corpus(VectorSource(PropCorpus))
myCorpus = tm_map(myCorpus, content_transformer(tolower))
myCorpus = tm_map(myCorpus, removePunctuation)
myCorpus = tm_map(myCorpus, removeNumbers)
myDTM = TermDocumentMatrix(myCorpus, control = list(minWordLength = 1))
m = as.matrix(myDTM)
sort(rowSums(m), decreasing = TRUE)
}
getTermMatrix(input$selection)
})
# Make the wordcloud drawing predictable during a session
wordcloud_rep <- repeatable(wordcloud)
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"))
})
}
我的數據基本上是兩列一個與該機構的名稱和一個描述不同的合同。
我認爲你缺少一些代碼在這裏。在「getTermMatrix」中,您引用了在此處未定義的變量「x」和「PlainTextDocument」。我懷疑他們是由你本地定義的。基本上,你在製作詞雲的信息在這裏沒有定義。你可能會在本地定義它們,這就是爲什麼當你運行你的閃亮程序時事物並沒有改變。 –
或者也許還有另一個塊,你沒有告訴我們... –
我看不到輸入$選擇用於實際獲得TDM。你的函數只在你的if語句中調用變量,但對語料庫創建保持沉默。所以我認爲它只是創建一個沒有任何輸入規範的語料庫。 –