Skip to content

Instantly share code, notes, and snippets.

@neilfws
Created August 2, 2018 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilfws/0c42237c4e1a96531d5ada2621f7ad07 to your computer and use it in GitHub Desktop.
Save neilfws/0c42237c4e1a96531d5ada2621f7ad07 to your computer and use it in GitHub Desktop.
# function to make the cloud
# assumes rtweet authentication configured as per documentation
make_tweetcloud <- function(user = "neilfws", tweets = 100) {
require(tidyverse)
require(tidytext)
require(rtweet)
require(wordcloud)
get_timeline(user, n = tweets, full_text = TRUE) %>%
mutate(text = gsub("@[A-Za-z0-9_]+", "", text)) %>%
unnest_tokens(word, text) %>%
anti_join(stop_words) %>%
count(word) %>%
filter(!word %in% c("https", "t.co", "amp"),
!grepl("^\\d+$", word)) %>%
with(wordcloud(word,
n,
max.words = 100,
min.freq = 3,
colors = brewer.pal(8, "Dark2")))
}
@neilfws
Copy link
Author

neilfws commented Aug 2, 2018

The first mutate is to remove user names; there is probably a better way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment