knitr::opts_chunk$set(tidy = TRUE, tidy.opts=list(blank=FALSE, width.cutoff=50), cache=FALSE) knitr::opts_chunk$set( tidy = TRUE, tidy.opts = list(blank = FALSE, width.cutoff = 50), cache = 1 ) knitr::knit_hooks$set( source = function(x, options) { if (options$engine == 'R') { # format R code x = highr::hilight(x, format = 'html') } else if (options$engine == 'bash') { # format bash code x = paste0('<span class="hl std">$</span> ', unlist(stringr::str_split(x, '\\n')), '\n', collapse = '') } x = paste(x, collapse = "\n") sprintf( "<div class=\"%s\"><pre class=\"%s %s\"><code class=\"%s %s\">%s</code></pre></div>\n", 'sourceCode', 'sourceCode', tolower(options$engine), 'sourceCode', tolower(options$engine), x ) } )
Earlier: Scraping data from web sites
rvest
Now: Social media
rtweet
request token
request token
and a secret
access token
secret
and access token
to encrypt and authenticate{ "id":"23462029838", "first_name":"Jason", "last_name":"Roos", "location":{ "id":"10952412900", "name":"Rotterdam, Netherlands" }, "friends":{ "data":[ { "name":"Nancy MacNancyface", "id":"5278757" }, { "name":"Davy McDavyface", "id":"72186234457" } ] } }
rtweet
and Rfacebook
rtweet
function search_tweets
library(tidyverse) library(rtweet) statuses <- search_tweets(q = '#hashtag', n = 10, lang = 'en', type = 'recent' )
library(tidyverse) library(rtweet) library(lubridate)
library(rtweet) token <- create_token( app = "my_twitter_research_app", consumer_key = "XYznzPFOFZR2a39FwWKN1Jp41", consumer_secret = "CtkGEWmSevZqJuKl6HHrBxbCybxI1xGLqrD5ynPd9jG0SoHZbD", acess_token = "9551451262-wK2EmA942kxZYIwa5LMKZoQA4Xc2uyIiEwu2YXL", access_secret = "9vpiSGKg1fIPQtxc5d5ESiFlZQpfbknEN1f1m2xe5byw7")
jmtroos <- lookup_users('jmtroos') %>% users_data() jmtroos %>% select(screen_name, name, location, description, followers_count, friends_count, statuses_count)
djt %>% select(text) %>% tidytext::unnest_tokens('word', 'text') %>% count(word) %>% arrange(desc(n)) %>% anti_join(bind_rows(tidytext::stop_words, tibble(word = c('https', 'amp', 't.co'))))
library(tidyverse) library(rtweet) search_tweets( '[ your code goes here ]' )
Try plotting the results with ggplot2
Suggestions:
#hashtag
by time of dayinstall.packages('tm', dependencies = TRUE) install.packages('topicmodels', dependencies = TRUE)
vignette('tm', package = 'tm')
(or https://cran.r-project.org/web/packages/tm/vignettes/tm.pdf if that doesn't work...)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.