Visualize Foursquare Check-ins with R

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on LinkedInEmail this to someone

You can use this tutorial to visualize foursquare check-ins in the ThinkToStartR package with:

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 Visualize Foursquare Check-ins 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:

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:

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

 

Visualize Foursquare check-ins

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

So let´s create our empty map object with:

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.

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.

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

You can find the whole code at github: https://github.com/JulianHill/RTutorials/blob/master/r_foursquare_map.r

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on LinkedInEmail this to someone

JulianHi

I´m an International Business student from Germany, interested in Data Analytics and Machine Learning with a focus on Marketing Applications. My favorite language is R.

You may also like...

5 Responses

  1. jstuartmill says:

    Very awesome. Thanks for posting.

  2. MY says:

    Thank you for the post! But I met with an error after the authentication was successful, which was:

    “Error in fromJSON(getURL(paste(“https://api.foursquare.com/v2/users/self/venuehistory?oauth_token=”, :
    error in evaluating the argument ‘content’ in selecting a method for function ‘fromJSON’: Error in function (type, msg, asError = TRUE) :
    SSL certificate problem, verify that the CA cert is OK. Details:
    error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed”

    I will greatly appreciate that if you might give me any solution on that. Thanks!

  3. MY says:

    Also what does the data refer to in your “Get all your data” part? How can I get the data from my fourquare API? Thanks!

  1. May 30, 2014

    Hey
    do you use a windows computer?

    The data part was done with my thinktostartR package.
    you can find the actual code over here: https://github.com/JulianHill/ThinkToStartR/blob/master/ThinkToStartR/R/foursquare_map.r

    Regards

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">