You can use this tutorial to visualize foursquare check-ins in the ThinkToStartR package with:
1234 #Package Usagetoken <- ThinkToStart("Foursquare_auth",app_name="R_Test",app_id="XXX",app_secret="XXX")map <- ThinkToStart("Foursquare_map",token)
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 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.
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:
1 2 3 4 5 |
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:
1 2 3 |
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”. 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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 } } |
Visualize Foursquare check-ins
Now that we have our dataframe we can visualize it with rCharts.
1 2 3 4 5 6 |
#Install if necessary: #require(devtools) #install_github('rCharts', 'ramnathv') require(rCharts) |
So let´s create our empty map object with:
1 |
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.
1 2 3 4 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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
1 2 3 |
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