[英]passing value from searchTwitter with textInput value to get_nrc_sentiment
I am planning to do a online Twitter sentiment analyzer that user can enter the search term by themselves using flexdashboard. 我打算做一个在线Twitter情绪分析器,用户可以使用flexdashboard自己输入搜索词。 Here is the code.
这是代码。
#Getting search term
textInput("data", label = "Enter search term", value = "", width = NULL, placeholder = NULL)
sliderInput("maxTweets", "Number of recent tweets to use for analysis:", min = 10, max = 1000, value = 500)
actionButton("enter", label = "Enter")
twt <- reactive({
if(input$enter!=0){
isolate({
twt <- searchTwitter(input$data, n=input$maxTweets, lang = "en", resultType = "recent")
})
}
})
#print tweets searched
twt
Here is the problem. 这是问题所在。 When I run the code below, an error occurred "Error in get_nrc_sentiment: Data must be a character vector.".
当我运行下面的代码时,发生错误“ get_nrc_sentiment中的错误:数据必须是字符向量。”。 I have tried map_chr and as.character before but I don't have the column to select.
我之前尝试过map_chr和as.character,但没有可供选择的列。
#Get sentiment
s <- reactive({
s<- get_nrc_sentiment(twt())
})
#Print output
s
As mentioned in my comment I would strongly advise moving to rtweet , as stated on the twitteR
Github page : 正如我的评论中所提到的,我强烈建议转到rtweet ,如
twitteR
Github页面上所述:
This is the start of a relatively leisurely deprecation period for twitteR, in favor of using rtweet.
这是twitteR相对悠闲的弃用期的开始,转而使用rtweet。 Please start looking to switch over to that package.
请开始寻找切换到该软件包的方法。 If you have any questions contact myself or @mkearney
如果您有任何疑问,请联系我自己或@mkearney
The last commit on that repository was 2 years ago. 该存储库的最后一次提交是2年前。
If you want to use twitteR
nonetheless. 如果仍然要使用
twitteR
。
twt <- searchTwitter(input$data, n=100, lang = "en", resultType = "recent")
twt <- twiListToDF(twt) # turn data.frame into a list
twt$text <- as.character(twt$text) # ensure it's a character vector
class(twt)
class(twt$text)
syuzhet::get_nrc_sentiment(txt$text)
You had to make sure it was 1) a vector 2) of type character 您必须确保它是1)矢量2)字符类型
syuzhet::get_nrc_sentiment(as.factor(c("Hello", "World")))
#> Error in syuzhet::get_nrc_sentiment(as.factor(c("Hello", "World"))) :
Data must be a character vector.
syuzhet::get_nrc_sentiment(list("Hello", "World"))
#> Error in syuzhet::get_nrc_sentiment(list("Hello", "World")) :
Data must be a character vector.
This is my answer. 这是我的答案。
```{r}
#Getting search term
textInput("data", label = "Enter search term", value = "")
actionButton("enter", label = "Enter")
twt <- reactive({
if(input$enter!=0){
isolate({
return(search_tweets(input$data, n=100, lang = "en", include_rts = FALSE))
})
}
})
```
````{r}
#Get sentiment score of tweets
s <- reactive({
return(get_nrc_sentiment(twt()$text))
})
#Print sentiment score
renderTable(head(s()))
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.