-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtwitter.R
More file actions
33 lines (27 loc) · 1.23 KB
/
twitter.R
File metadata and controls
33 lines (27 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#credit Dave Tang http://davetang.org/muse/2013/04/06/using-the-r_twitter-package/
#install the necessary packages
#install.packages("twitteR")
#install.packages("wordcloud")
#install.packages("tm")
library("twitteR")
library("wordcloud")
library("tm")
#get your consumerKey and consumerSecret from https://apps.twitter.com/
consumer_key <- 'consumer_key'
consumer_secret <- 'consumer_secret'
access_token <- '25649945-access_token'
access_secret <- 'access_secret'
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
#search tweets for hashtag
tweets <- searchTwitter("#svcc", n=1500)
length(tweets)
tweets_message <- sapply(tweets, function(x) x$getText())
#create corpus
tweets_message_corpus <- Corpus(VectorSource(tweets_message))
tweets_message_corpus <- tm_map(tweets_message_corpus,
content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')), mc.cores=1)
tweets_message_corpus <- tm_map(tweets_message_corpus, content_transformer(tolower), mc.cores=1)
tweets_message_corpus <- tm_map(tweets_message_corpus, removePunctuation, mc.cores=1)
tweets_message_corpus <- tm_map(tweets_message_corpus, function(x)removeWords(x,stopwords()), mc.cores=1)
#build the wordcloud
wordcloud(tweets_message_corpus)