R新手在这里......
我正试图在r闪亮的app上创建一个wordcloud。
以下是我在ui.R中的内容:
tabPanel("Word Cloud", sidebarLayout( # Sidebar with a slider and selection inputs sidebarPanel( selectInput("Subcategory", "Choose a Subcategory:", choices = makeup$SubCategory), sliderInput("freq", "Maximum Number of Words:", min = 1, max = 7, value = 5), sliderInput("max", "Minimum Frequency:", min = 1, max = 6000, value = 1000) ), mainPanel( plotOutput("plot") ))
以下是我服务器中的内容.R:
shinyServer(function(input, output, session) { word = reactive({ myCorpus = Corpus(VectorSource(subset(makeup, SubCategory == > input$Subcategory, select = ChemicalName))) myCorpus = tm_map(myCorpus, content_transformer(tolower)) myCorpus = tm_map(myCorpus, removePunctuation) myCorpus = tm_map(myCorpus, removeNumbers) myDTM = TermDocumentMatrix(myCorpus) m = as.matrix(myDTM) v = sort(rowSums(m),decreasing = TRUE) data.frame(word=names(v),freq=v)$word }) freq = reactive({ myCorpus = Corpus(VectorSource(subset(makeup, SubCategory == > input$Subcategory, select = ChemicalName))) myCorpus = tm_map(myCorpus, content_transformer(tolower)) myCorpus = tm_map(myCorpus, removePunctuation) myCorpus = tm_map(myCorpus, removeNumbers) myDTM = TermDocumentMatrix(myCorpus) m = as.matrix(myDTM) v = sort(rowSums(m),decreasing = TRUE) data.frame(word=names(v),freq=v)$freq }) output$plot <- renderPlot({ wordcloud(words = word, freq = freq, random.order=FALSE, rot.per=0.3, scale=c(4,.5),max.words=15, colors=brewer.pal(8,"Dark2")) })
我无法让我的闪亮应用程序显示wordcloud ......
我也收到一条警告信息:
。警告:最大错误:参数
的无效'type'(闭包)
有什么想法吗?!
谢谢!
答案 0 :(得分:1)
反应是功能而不是变量。试试这个 - 它应该有用。
output$plot <- renderPlot({
wordcloud(words = word(),
freq = freq(),
random.order=FALSE, rot.per=0.3,
scale=c(4,.5),max.words=15,
colors=brewer.pal(8,"Dark2"))
})
答案 1 :(得分:1)
shinyServer(function(input, output, session) {
word = reactive({
myCorpus = Corpus(VectorSource(subset(makeup,
SubCategory == > input$Subcategory,
select = ChemicalName)))
myCorpus = tm_map(myCorpus, content_transformer(tolower))
myCorpus = tm_map(myCorpus, removePunctuation)
myCorpus = tm_map(myCorpus, removeNumbers)
myDTM = TermDocumentMatrix(myCorpus)
#m = as.matrix(myDTM) ##this can cause an memory error if your corpus is even moderately sized.
#row_sums from the slam package is much more efficient as it is designed to operate on sparse matrices and TDM/DTMs from the tm package are sparse matrices which are constructed using slam
v = sort(slam::row_sums(myDTM),decreasing = TRUE)
data.frame(word=names(v),freq=v) #Since you've already calculated frequency here there is no need for the freq reactive. We can just access the freq from this data.frame
})
#this is a redundant reprocessing of your corpus (which can be time consuming if it is big.
#freq = reactive({
# myCorpus = Corpus(VectorSource(subset(makeup, SubCategory == > input$Subcategory, select = ChemicalName)))
# myCorpus = tm_map(myCorpus, content_transformer(tolower))
# myCorpus = tm_map(myCorpus, removePunctuation)
# myCorpus = tm_map(myCorpus, removeNumbers)
# myDTM = TermDocumentMatrix(myCorpus)
# m = as.matrix(myDTM)
# v = sort(rowSums(m),decreasing = TRUE)
# data.frame(word=names(v),freq=v)$freq
# })
output$plot <- renderPlot({
wordcloud(words = word()$word,
freq = word()$freq,
random.order=FALSE, rot.per=0.3,
scale=c(4,.5),max.words=15,
colors=brewer.pal(8,"Dark2"))
})