After running WordCloud function R studio stop responding. I have terminate the session but nothing works, restated my machine but no help

wordcloud(words = namesCorpus,max.words = 100,random.order = FALSE,rot.per = .10,random.color = FALSE,use.r.layout = TRUE)

Have you tried reseting Rstudio's state?

Hi, I have tried that but no help. I am attaching the screenshot. It happens everytime I run wordcloud. R session gets aborted.

Does this work for you in R, but not in RStudio? (see FAQ below for disambiguation)

You didn't said that before, if this happens when you run the script again then is possible that you are just running out of RAM memory but I can't be sure without a REPRoducible EXample (reprex)

Here is the reprex

twitterUser<-getUser("narendramodi")
#> Error in getUser("narendramodi"): could not find function "getUser"
tweets<-userTimeline(twitterUser,n=1000)
#> Error in userTimeline(twitterUser, n = 1000): could not find function "userTimeline"
newnarendratweets.df<-twListToDF(tweets)
#> Error in twListToDF(tweets): could not find function "twListToDF"
newnarendratweets.df$text<-sapply(newnarendratweets.df$text, function(x) iconv(x,to='UTF-8' ))
#> Error in lapply(X = X, FUN = FUN, ...): object 'newnarendratweets.df' not found
newnarendratweets.df$created<-ymd_hms(newnarendratweets.df$created)
#> Error in ymd_hms(newnarendratweets.df$created): could not find function "ymd_hms"


#Plot tweets by time 
ggplot(data = newnarendratweets.df, aes(x=created))+geom_histogram(aes(fill=..count..))+
  theme(legend.position = "none")+xlab("Month")+ylab("Number of tweets")+ scale_fill_gradient(low="midnightblue", high="aquamarine4")
#> Error in ggplot(data = newnarendratweets.df, aes(x = created)): could not find function "ggplot"

ggplot(data = trendingtweets.df, aes(x=created))+geom_histogram(aes(fill=..count..))+
  theme(legend.position = "none")+xlab("Date")+ylab("Number of tweets")+ scale_fill_gradient(low="midnightblue", high="aquamarine4")
#> Error in ggplot(data = trendingtweets.df, aes(x = created)): could not find function "ggplot"


#wordcloud for trending tweets account

tweetaccount<-str_extract_all(trendingtweets.df$text,"@\\w+")
#> Error in str_extract_all(trendingtweets.df$text, "@\\w+"): could not find function "str_extract_all"
namesCorpus<-Corpus(VectorSource(tweetaccount))
#> Error in Corpus(VectorSource(tweetaccount)): could not find function "Corpus"
pal<-brewer.pal(9,"YlGnBu")
#> Error in brewer.pal(9, "YlGnBu"): could not find function "brewer.pal"
pal<-pal[-(1:4)]
#> Error in eval(expr, envir, enclos): object 'pal' not found
set.seed(42)
wordcloud(freq = 20,words = namesCorpus,scale = c(3,.5),max.words = 100,random.order = FALSE,random.color = FALSE,rot.per = .10,colors = pal,use.r.layout = TRUE)
#> Error in wordcloud(freq = 20, words = namesCorpus, scale = c(3, 0.5), : could not find function "wordcloud"

That is not actually reproducible, please read the link I gave you before, narrow down your code to just the problematic part and try to make a proper reprex.

I have follow the same guidelines mentioned in the document you shared. This is how it is coming.

I have also run the code in R instead of r studio. Now I am getting this error

Error in strwidth(words[i], cex = size[i], ...) :
invalid input '"@nã")' in 'utf8towcs'

Have you noticed that you are not including any library calls? That is why you are getting all those error messages in your reprex, also we don't have access to your API credentials, so it would be better if you provide sample data for the wordcloud instead of the code you used for getting the data from the Twitter api.

Hi Andresrcs,

I am including libraries and all the packages I am not getting why it is not showing in the reprex. The problem is everytime I am running the wordcloud function R studio stops responding.

  • I have run the same code in R it was showing this Error
    strwidth(words[i], cex = size[i], ...) :
    invalid input '"@nã")' in 'utf8towcs'

  • the funniest pasrt is I have run this code before in the R studio before it start crashing and everything was working fine. I was able to get the wordcloud that time.

How can I share the data? I am new in the community.

Read the link I gave you more carefully, it explains a couple of ways to do it, including this one.

This is the error I am getting

Could not paste clipboard as tibble. Text could not be parsed as table.

trendingtweets.df is the dataframe you have to provide as sample data, before you transform it to a corpus object.(i.e. namesCorpus).

Abpve is the link of the trendingtweet.df data file. I hope this will help.

This is the entire code

install.packages('tm')
install.packages('ggplot')
install.packages('wordcloud')
install.packages('lubridate')
install.packages("twitteR")
install.packages('wordcloud')
library('twitteR')
library('tm')
library('ggmap')
library('ggplot2')
library('stringr')
library('worldcloud')
library('lubridate')
library('data.table')

Connect to twitter app

Consumer_secret<- ""
Consumer_key<- ""
Access_token<-""
Access_secret<-""
setup_twitter_oauth(consumer_secret = Consumer_secret, consumer_key = Consumer_key,access_token = Access_token,access_secret = Access_secret)

twitterUser<-getUser("narendramodi")
tweets<-userTimeline(twitterUser,n=1000)
newnarendratweets.df<-twListToDF(tweets)
newnarendratweets.df$text<-sapply(newnarendratweets.df$text, function(x) iconv(x,to='UTF-8' ))
newnarendratweets.df$created<-ymd_hms(newnarendratweets.df$created)

#Plot tweets by time
ggplot(data = newnarendratweets.df, aes(x=created))+geom_histogram(aes(fill=..count..))+
theme(legend.position = "none")+xlab("Month")+ylab("Number of tweets")+ scale_fill_gradient(low="midnightblue", high="aquamarine4")

ggplot(data = trendingtweets.df, aes(x=created))+geom_histogram(aes(fill=..count..))+
theme(legend.position = "none")+xlab("Date")+ylab("Number of tweets")+ scale_fill_gradient(low="midnightblue", high="aquamarine4")

#WordCloud for trending tweets
tweetsaccount<-str_extract_all(trendingtweets.df$text,"@\w+")
namesCorpus<-Corpus(VectorSource(tweetsaccount))
pal<-brewer.pal(5,"YlGnBu")
pal<-pal[-(1:4)]
wordcloud(words = namesCorpus,scale = c(3,.5),max.words = 100,,random.color = FALSE,use.r.layout = TRUE,rot.per = .10,colors = pal)

It seems like the problem is with the presence of some non ASCII characters, unfortunately I have no experience working with non English or Spanish characters so I don't even know what encoding to try, hopefully someone else here would be able to help you with that.

Thank you so much! It really helped.