如何修复在以下代码中找不到的对象?
How to fix object not found in the following code?
我正在用 R 做一个情感分析项目,每当我 运行 代码、使用的库和代码如下(我也没有忘记在我的代码中添加 api 详细信息):
library("twitteR")
library("ROAuth")
library("NLP")
library("twitteR")
library("syuzhet")
library("tm")
library("SnowballC")
library("stringi")
library("topicmodels")
library("syuzhet")
library("ROAuth")
library("wordcloud")
library("ggplot2")
# authorisation keys
#provided by me in the code.
setup_twitter_oauth(consumer_key,consumer_secret,access_token, access_secret)
tweets_g <- searchTwitter("#google", n=500,lang = "en")
google_tweets <- twListToDF(tweets_g)
View(google_tweets)
google_text<- google_tweets$text
google_text<- tolower(google_text)
google_text <- gsub("rt", "", google_text)
google_text <- gsub("@\w+", "", google_text)
google_text <- gsub("[[:punct:]]", "", google_text)
google_text <- gsub("http\w+", "", google_text)
google_text <- gsub("[ |\t]{2,}", "", google_text)
google_text <- gsub("^ ", "", google_text)
google_text <- gsub(" $", "", google_text)
#clean up by removing stop words
google_tweets.text.corpus <- tm_map(google_tweets.text.corpus, function(x)removeWords(x,stopwords()))
#generate wordcloud
wordcloud(google_tweets.text.corpus,min.freq = 10,colors=brewer.pal(8, "Dark2"),random.color = TRUE,max.words = 500)
#getting emotions using in-built function
mysentiment_google<-get_nrc_sentiment((google_text))
#calculationg total score for each sentiment
Sentimentscores_google<-data.frame(colSums(mysentiment_google[,]))
names(Sentimentscores_google)<-"Score"
Sentimentscores_google<-cbind("sentiment"=rownames(Sentimentscores_google),Sentimentscores_google)
rownames(Sentimentscores_google)<-NULL
#plotting the sentiments with scores
ggplot(data=Sentimentscores_google,aes(x=sentiment,y=Score))+geom_bar(aes(fill=sentiment),stat = "identity")+
theme(legend.position="none")+
xlab("Sentiments")+ylab("scores")+ggtitle("Sentiments of people behind the tweets on tech giant GOOGLE")
出现在 运行R 脚本上的错误消息是:
Loading required package: RColorBrewer
Attaching package: ‘ggplot2’
The following object is masked from ‘package:NLP’:
annotate
[1] "Using direct authentication"
Error in tm_map(google_text.corpus, function(x) removeWords(x, stopwords())) :
object 'google_text.corpus' not found
Execution halted````
关键是object 'google_text.corpus' not found
您正在尝试调用您尚未定义的变量。你需要问问自己,你认为 'google_text.corpus' 应该是什么,然后定义它,就像你在行 google_tweets <- twListToDF(tweets_g)
中对变量 google_tweets
所做的那样。
尝试删除语料库。只需用此代码段替换您的代码
#generate wordcloud
wordcloud(min.freq = 10,colors=brewer.pal(8, "Dark2"),random.color = TRUE,max.words = 500)
我正在用 R 做一个情感分析项目,每当我 运行 代码、使用的库和代码如下(我也没有忘记在我的代码中添加 api 详细信息):
library("twitteR")
library("ROAuth")
library("NLP")
library("twitteR")
library("syuzhet")
library("tm")
library("SnowballC")
library("stringi")
library("topicmodels")
library("syuzhet")
library("ROAuth")
library("wordcloud")
library("ggplot2")
# authorisation keys
#provided by me in the code.
setup_twitter_oauth(consumer_key,consumer_secret,access_token, access_secret)
tweets_g <- searchTwitter("#google", n=500,lang = "en")
google_tweets <- twListToDF(tweets_g)
View(google_tweets)
google_text<- google_tweets$text
google_text<- tolower(google_text)
google_text <- gsub("rt", "", google_text)
google_text <- gsub("@\w+", "", google_text)
google_text <- gsub("[[:punct:]]", "", google_text)
google_text <- gsub("http\w+", "", google_text)
google_text <- gsub("[ |\t]{2,}", "", google_text)
google_text <- gsub("^ ", "", google_text)
google_text <- gsub(" $", "", google_text)
#clean up by removing stop words
google_tweets.text.corpus <- tm_map(google_tweets.text.corpus, function(x)removeWords(x,stopwords()))
#generate wordcloud
wordcloud(google_tweets.text.corpus,min.freq = 10,colors=brewer.pal(8, "Dark2"),random.color = TRUE,max.words = 500)
#getting emotions using in-built function
mysentiment_google<-get_nrc_sentiment((google_text))
#calculationg total score for each sentiment
Sentimentscores_google<-data.frame(colSums(mysentiment_google[,]))
names(Sentimentscores_google)<-"Score"
Sentimentscores_google<-cbind("sentiment"=rownames(Sentimentscores_google),Sentimentscores_google)
rownames(Sentimentscores_google)<-NULL
#plotting the sentiments with scores
ggplot(data=Sentimentscores_google,aes(x=sentiment,y=Score))+geom_bar(aes(fill=sentiment),stat = "identity")+
theme(legend.position="none")+
xlab("Sentiments")+ylab("scores")+ggtitle("Sentiments of people behind the tweets on tech giant GOOGLE")
出现在 运行R 脚本上的错误消息是:
Loading required package: RColorBrewer
Attaching package: ‘ggplot2’
The following object is masked from ‘package:NLP’:
annotate
[1] "Using direct authentication"
Error in tm_map(google_text.corpus, function(x) removeWords(x, stopwords())) :
object 'google_text.corpus' not found
Execution halted````
关键是object 'google_text.corpus' not found
您正在尝试调用您尚未定义的变量。你需要问问自己,你认为 'google_text.corpus' 应该是什么,然后定义它,就像你在行 google_tweets <- twListToDF(tweets_g)
中对变量 google_tweets
所做的那样。
尝试删除语料库。只需用此代码段替换您的代码
#generate wordcloud
wordcloud(min.freq = 10,colors=brewer.pal(8, "Dark2"),random.color = TRUE,max.words = 500)