【发布时间】:2015-04-25 03:45:12
【问题描述】:
我正在尝试制作一个 Shiny 应用程序,它允许您在 Twitter 中搜索一个词,然后分析获得的推文的感受并计算正面和负面推文的百分比。我有所有已实现的功能,但我无法在屏幕上写出结果。计算百分比的函数是无效的,也就是说什么都不返回,我该怎么做才能在屏幕上显示结果?
在 ui.r 中:
sidebarPanel(textInput("term", "Put the term",""),
textInput("number","Number of tweets",""), submitButton("Search")),
mainPanel(("Results of the search"),textOutput("result"))
在 server.r 中:
myterm<-reactive({myterm<-TweetFrame(input$term, input$number)})
cleanterms <- reactive({CleanTweets(myterm()["text"])})
sentimentsTweets<-reactive({sentimentsTweets<-sentimentalanalysis(cleanterms()["text"])})
output$result <- renderPrint({paste(print(sentimentsTweets()["score"]))})
output$result <- renderPrint({CalculatePercentaje(as.vector(sentimentsTweets()))})
我希望我知道如何编写显示获得的推文得分以及如何编写调用函数CalculatePercentage的结果,这是函数:
CalculatePercentage<-function(sentimentTweets){
neutral <- 0
negativo <- 0
positivo <- 0
for(i in 1:length(sentimentTweets$score)){
if(sentimentTweets$score[i] == 0){
neutral <- neutral + 1
} else {
if(sentimentTweets$score[i] > 0){
positivo <- positivo + 1
} else {
negativo <- negativo + 1
}
}
}
cat("El porcentaje de tweets neutrales es ", (neutral * 100)/ length(sentimentTweets$score), "% \n")
cat("El porcentaje de tweets positivos es ", (positivo * 100)/ length(sentimentTweets$score), "% \n")
cat("El porcentaje de tweets negativos es ", (negativo * 100)/ length(sentimentTweets$score), "% \n")
}
【问题讨论】:
-
如果我有“情感分析”功能,我可以完成这项工作
-
好的,我解释一下,该函数返回一个两行矩阵,第一行是给每条推文包含的分数,取决于正面或负面单词,第二行包含文本每条推文。
-
请输入代码。或者在我下面发布的模板中尝试一下作为答案。
-
该函数接收一个包含每条推文内容的向量,实际上,您不需要知道函数的内容,因为我的问题是如何将函数的结果写在屏幕上?
-
我已经贴出了函数的代码