Connect to the Facebook API with R for Windows Users

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 )
}

4 thoughts on “Connect to the Facebook API with R for Windows Users

    • Thanks for putting this up. I’m trying to get into the Twitter API and am running into the same Curl / SSL stuff.

  1. Pingback: How to extract content from facebook using R | Build Future Repository

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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>