Visualize Your Foursquare Check-ins with R

You can use this tutorial in the ThinkToStartR package with:

token <-ThinkToStart(“Foursquare_auth”,app_name=”R_Test”,app_id=”XXX”,app_secret=”XXX”)

map <- ThinkToStart(“Foursquare_map”,token) foursquare map r

Hey R-Fans! You sure all heard of Foursquare. It has more than 45 million users but it´s potential is not recognized by much people. So today I want to show you how easy it is to get your check-in history and visualize it with rCharts and a leaflet map.

The Foursquare app

To start we need to create a Foursquare app. We can do this at https://foursquare.com/developers/apps

foursquare app map R

Click on the green “Create a new App” button. This leads you to the settings page for your new app. Enter a name and a website address for your app. You can be creative and type in whatever you want. Click on “Save Changes” but leave the screen open as we need to edit it later. On the overview screen you can now see your Client id and Client secret. We will need them for the authentication with R. foursquare create app

Authentication

The authentication with OAuth2 is a little bit tricky with the Foursquare API. I implemented in my ThinkToStartR package but I will improve it in the next time. To intstall the package use:

require(devtools) #install if necessary (install.packages("devtools")

dev_mode(on=T)

install_github("ThinkToStartR",username="JulianHill")

For some more information take a look here: http://thinktostart.wordpress.com/2014/03/23/thinktostartr-package/ The authentication then basically is one function call:


require(ThinkToStartR)
library(rjson)
require(RCurl)

 

It will ask you to edit the Foursquare app settings. To do so go back to the app you created on the Foursquare website, click on “Edit this App” and add the URL at “Redirect URLs”.

R foursquare redirect URL foursquare app R map

 

Then hit enter in your R console and the authentication will go on. Log in to your Foursquare account and allow the app to access your account.

The function then returns a token which we will use for our API calls.

Remember that you installed the ThinkToStartR package in the dev_mode. So it will disappear as soon as you close the R instance.

Get all the Data!

Ok after the authentication process the fun part can start.

First we need to get the data and split the returned JSON in some smaller parts we can work with more easily:
data

This call returns your whole venue history. You might see the v argument at the end. This is actually the version of the Foursquare API you want to use. Foursquare uses the date for this. So it actually adds the current date.

In the next step we process the JSON object even further and create a dataframe where we can save all the important information we need


no_venues = length(data$response$venues$items)

df = data.frame(no = 1:no_venues)

for (i in 1:nrow(df)){

 #Add Name and the location of the Venue
 df$venue_name[i] <- venues[[i]]$venue$name
 df$venue_lat[i] <- venues[[i]]$venue$location$lat
 df$venue_lng[i] <- venues[[i]]$venue$location$lng

 ##########################
 #Add the address of the location
 if(length(venues[[i]]$venue$location$address)>0)
 {
 df$venue_address[i] <- venues[[i]]$venue$location$address
 }
 else{
 df$venue_address[i] <- "No Address Available"

 }

##########################
 #Add the citiy of the location
 if(length(venues[[i]]$venue$location$city)>0)
 {
 df$venue_city[i] <- venues[[i]]$venue$location$city
 }
 else{
 df$venue_city[i] <- "No City Available"

 }

 ##########################
 #Add the number of check-ins of the venue
 df$venue_checkinsCount[i] <- venues[[i]]$venue$stats[[1]]

 ##########################
 #Add the URL of the URL if defined
 if(length(venues[[i]]$venue$url)>0)
 {
 df$url[i] <- venues[[i]]$venue$url
 }
 else{
 df$url[i] <- NA

 }

}

The Visualization

Now that we have our dataframe we can visualize it with rCharts.


#Install if necessary:

#require(devtools)
#install_github('rCharts', 'ramnathv')

require(rCharts)

So let´s create our empty map object with:


map <- Leaflet$new()

We will calculate the mean longitude and latitude values of our venue history to set the initial view of our map. Because it wouldn´t make sense if most of your check-ins are in Germany but the initial view of the map shows the USA.


mean_lat <- mean(df$venue_lat)
mean_lon <- mean(df$venue_lng) 

map$setView(c(mean_lat, mean_lon), zoom = 5)

In the next step we will add a marker for every venue in the dataframe. We also create a popup for every marker which shows additional information.


for (i in 1:no_venues){

 #Get the name and the number of check-ins of the current venue
 name <- df$venue_name[i]
 checkins <- df$venue_checkinsCount[i]

 #Add the marker to the map but just add a website link if we have a URL for the venue

 #if URL is available
 if(is.na(df$url[i]))
 { map$marker(c(df$venue_lat[i], df$venue_lng[i]), bindPopup = paste(name,' <br> Checkins: ',checkins,sep=""))
 }
 else
 {
 map$marker(c(df$venue_lat[i], df$venue_lng[i]), bindPopup = paste(name,' <br> Checkins: ',checkins,'<br> <a href="',df$url[i],'" target="_blank">Website</a> ',sep=""))
 }

}

And that´s it! Congratulations for creating your first foursquare check-ins map! You can now open it or save it by tipping in


map

map$save('map.html',cdn=TRUE)

You can find the whole code at github:

https://github.com/JulianHill/RTutorials/blob/master/r_foursquare_map.r

2 thoughts on “Visualize Your Foursquare Check-ins with R

  1. Pingback: Visualize Your Foursquare Check-ins with R ← Patient 2 Earn

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s