By combining the twitter API and the AmCAT API we can upload tweets that match a given query directly to AmCAT.
This howto uses the twitteR and amcatr packages. The following code will load these packages, installing them if needed. You can skip the 'install_github' steps after the first time.
options(repos=structure(c(CRAN=""))) if (!require("devtools")) {install.packages("devtools"); library(devtools)} if (!require("plyr")) {install.packages("plyr"); library(plyr)} library(devtools) install_github("twitteR", username="geoffjentry") install_github("amcat-r", "amcat") library("twitteR") library("amcatr")
First, you need to get a number of tokens (a kind of passwords) from twitter:
token = '...' token_secret = '...' consumer_key = "..." consumer_secret = "..."
load("~/twitterauth.rdata")
Now you can connect using the setup_twitter_oauth function:
setup_twitter_oauth(consumer_key, consumer_secret, token, token_secret)
Please see the documentation for the Twitter API and the twitteR package for all the possibilities of the API. As the following simple example shows, you can search for keywords and get a list or results
tweets = searchTwitteR("pvda", geocode="52.36656,4.90269,200km", result_type="recent") tweets[[1]] class(tweets[[1]]) tweets[[1]]$text
To make it easier to manipulate the tweets, we can convert them from a list of status
objects to a data.frame, for which we use the ldply
(list-dataframe-ply) function from the plyr package, taking advantage of the fact that as.data.frame
works on a single status object:
tweets = ldply(tweets, as.data.frame) names(tweets)
Now we can directly upload these tweets to AmCAT using the amcat.upload.articles
function.
conn = amcat.connect("http://amcat.vu.nl") project=1 # change this to your project ID set = amcat.add.articles.to.set(conn, project=project, articleset.name="Tweets", articles=NULL) amcat.upload.articles(conn, project=project, articleset=set, medium="twitter", text=tweets$text, headline=tweets$text, date=tweets$created, author=tweets$screenName, addressee=tweets$replyToSN)
Now we can inspect our articleset on the AmCAT web site, or do a query from r, e.g.:
amcat.hits(conn, sets=set, queries="vvd")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.