@ У Бертиля Барона правильный ответ на ваш самый насущный вопрос. Я думал, что предоставлю несколько советов по оптимизации вашего приложения / кода
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"))
})