Connect to the Facebook API with R for Windows Users

January 19, 2012
By

(This article was first published on Data Twirling » R, and kindly contributed to R-bloggers)

Recently there have been some great posts that highlight how easy it is to hook into the Facebook Graph API using R. Crawling Facebook with R started the discussion and Apply R highlighted how easy it was to plot our network.

In order to replicate the examples on Windows, most likely you will need to run a few extra lines of code before calling the API. Keep this code handy if you want to hook into Twitter or Google Analytics as well.

# download the file needed for authentication
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

# set the curl options
curl <- getCurlHandle()
options(RCurlOptions = list(capath = system.file("CurlSSL", "cacert.pem",
package = "RCurl"),
ssl.verifypeer = FALSE))
curlSetOpt(.opts = list(proxy = 'proxyserver:port'), curl = curl)

From here, you should be able to follow along with the code samples. Hope this helps.

UPDATE: It is worth noting that you can tweak the Facebook function as well to include cacert.pem in the getURL call, eliminating the need to set curl options.

download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

# http://romainfrancois.blog.free.fr/index.php?post/2012/01/15/Crawling-facebook-with-R
facebook <- <- function( path = "me", access_token = token, options){
if( !missing(options) ){
options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) )
} else {
options <- ""
}
data <- getURL( sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token ), cainfo="cacert.pem" )
fromJSON( data )
}

To leave a comment for the author, please follow the link and comment on his blog: Data Twirling » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.