How to track Twitter unfollowers in R
I have Twitter account and it is relatively easy to see new followers or subscribers. However, I was looking for ways to know who are the unfollowers. I have noticed, that some (un)subscriptions happen in bulks, which made me thinking that either I tweeted some bullshit and upset bunch of people or spam bots work in sync. With that in mind I have created a simple R script, which produces Markdown and html reports about unfollowers. You can find an example of such report below.
The advantage of this script is that it does not require you to sign or share your data. You just have to install twitter package and you ready to go. Nevertheless, if you want to create Markdown report, you need to install markdown and knitr packages.
The source code and build scripts can be found here.
```{r echo=FALSE,message=FALSE}
require(twitteR)
setwd('~/git/twitTracker/')
usr=getUser("dzidorius")
tmp=sapply(usr$getFollowers(),function(x)x$screenName)
if(!file.exists('users.csv'))
{
## when file doesn't exist - take users list and add some artificial user
write.table(c(as.character(tmp),'dzidorius'),'users.csv')
}
old_list=as.character(read.table('users.csv')$x)
users=lookupUsers(old_list[which(!(old_list %in% as.character(tmp)))])
if(length(users)==0)
{
## stop() doesn't work under knitr
cat('no one left you')
}
```
```{r comment=NA,echo=FALSE,message=FALSE,results='asis'}
for(i in 1:length(users))
{
cat(paste("**",users[[i]]$name," @",users[[i]]$screenName,"**", "\n===\n",sep=""))
cat(paste("",sep=''))
cat(paste(" \n**Created:** ",users[[i]]$created,
" \n**Spam rate:** ",round(users[[i]]$followersCount/users[[i]]$friendsCount,digits=2),
" \n**Activity:** " , users[[i]]$statusesCount,
" \n**Location:** ", users[[i]]$location," \n",users[[i]]$description," \n",
"**Last status:** ",(users[[i]]$lastStatus$text),"\n\n",sep=""))
}
```
```{r comment=NA,echo=FALSE,message=FALSE,results='asis'}
write.table(as.character(tmp),'users.csv')
``` |
Dzidas @dzidorius
Created: 2010-02-18 13:09:29
Spam rate: 0.98
Activity: 315
Location: Luxembourg
Java, C++ and R developer & data junkie
Last status: It is rainy summer in #Luxembourg but there is a party! http://t.co/FZ7eZq6u
Matt said,
July 19, 2012 @ 1:29
Nice post. If you could put up an example of the HTML doc that’d be a good addition.
Dzidorius Martinaitis said,
July 19, 2012 @ 12:31
Matt,
an example of html code is embedded into post. If that is not enough – let me know.