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 comments for “Connect to the Facebook API with R for Windows Users

  1. January 20, 2012 at 4:18 am

    Thanks for that. It seems that many people experienced this problem.

    • Rob
      April 7, 2012 at 6:27 pm

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

  2. May 4, 2012 at 1:48 pm

    I Like it, is there any visual tools for that?
    Thank you

Leave a Reply

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