Difference between revisions of "Facebook and R"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
| (16 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Also see - [Getting Started with R] | Also see - [[Getting Started with R]] | [[R Articles]] | [[R Notes]] | ||
* http://pablobarbera.com/blog/archives/3.html | |||
* https://github.com/pablobarbera/streamR | |||
* [https://books.google.com.au/books?id=Zb4WBQAAQBAJ&pg=PA268&lpg=PA268&dq=facebook+oauth2+consumer+key&source=bl&ots=VixrRfKqXA&sig=wf3SiSwytd6RWFND5bj_3VpDBKw&hl=en&sa=X&ved=0ahUKEwj8p5f-r7POAhUCGZQKHXSRBFo4ChDoAQgaMAA#v=onepage&q=facebook%20oauth2%20consumer%20key&f=false Automated Data Collection with R: A Practical Guide to Web Scraping and Text Mining] | |||
<pre> | |||
> install.packages("ROAuth") | |||
also installing the dependencies ‘bitops’, ‘RCurl’, ‘digest’ | |||
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/bitops_1.0-6.zip' | |||
Content type 'application/zip' length 36372 bytes (35 KB) | |||
downloaded 35 KB | |||
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/RCurl_1.95-4.8.zip' | |||
Content type 'application/zip' length 2854329 bytes (2.7 MB) | |||
downloaded 2.7 MB | |||
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/digest_0.6.10.zip' | |||
Content type 'application/zip' length 169671 bytes (165 KB) | |||
downloaded 165 KB | |||
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/ROAuth_0.9.6.zip' | |||
Content type 'application/zip' length 52222 bytes (50 KB) | |||
downloaded 50 KB | |||
... | |||
> install.packages("httr") | |||
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/httr_1.2.1.zip' | |||
Content type 'application/zip' length 281284 bytes (274 KB) | |||
downloaded 274 KB | |||
... | |||
> library(httr) | |||
> Sys.setenv(FACEBOOK_CONSUMER_SECRET = "AAAA") | |||
> facebook <- oauth_endpoint( | |||
authorize = "https://www.facebook.com/dialog/oauth", | |||
access = "https://graph.facebook.com/oauth/access_token") | |||
> fb_app <- oauth_app("facebook", "1234567890") | |||
</pre> | |||
Some of the R commands... | |||
<pre> | |||
install.packages("Rfacebook") # from CRAN | |||
library(devtools) | |||
install_github("Rfacebook", "pablobarbera", subdir = "Rfacebook") # from GitHub | |||
</pre> | |||
<pre> | |||
library(Rfacebook) | |||
# token generated here: https://developers.facebook.com/tools/explorer | |||
token <- "XXXXXXXXXXXXXX" | |||
me <- getUsers("xxxx", token, private_info = TRUE) | |||
me$name # my name | |||
</pre> | |||
<pre> | |||
my_friends <- getFriends(token, simplify = TRUE) | |||
head(my_friends$id, n = 1) # get lowest user ID | |||
</pre> | |||
<pre> | |||
my_friends_info <- getUsers(my_friends$id, token, private_info = TRUE) | |||
table(my_friends_info$gender) # gender | |||
</pre> | |||
<pre> | |||
table(substr(my_friends_info$locale, 1, 2)) # language | |||
table(substr(my_friends_info$locale, 4, 5)) # country | |||
table(my_friends_info$relationship_status)["It's complicated"] # relationship status | |||
</pre> | |||
<pre> | |||
mat <- getNetwork(token, format = "adj.matrix") | |||
dim(mat) | |||
</pre> | |||
See - http://blog.revolutionanalytics.com/2013/11/how-to-analyze-you-facebook-friends-network-with-r.html | |||
<pre> | |||
posts <- searchFacebook(string = "upworthy", token, n = 500, since = "20 january 2016 00:00", until = "10 august2016 10:00") | |||
posts[which.max(posts$likes_count), ] | |||
</pre> | |||
<pre> | |||
## convert Facebook date format to R date format | |||
format.facebook.date <- function(datestring) { | |||
date <- as.POSIXct(datestring, format = "%Y-%m-%dT%H:%M:%S+0000", tz = "GMT") | |||
} | |||
## aggregate metric counts over month | |||
aggregate.metric <- function(metric) { | |||
m <- aggregate(page[[paste0(metric, "_count")]], list(month = page$month), | |||
mean) | |||
m$month <- as.Date(paste0(m$month, "-15")) | |||
m$metric <- metric | |||
return(m) | |||
} | |||
# create data frame with average metric counts per month | |||
page$datetime <- format.facebook.date(page$created_time) | |||
page$month <- format(page$datetime, "%Y-%m") | |||
df.list <- lapply(c("likes", "comments", "shares"), aggregate.metric) | |||
df <- do.call(rbind, df.list) | |||
# visualize evolution in metric | |||
library(ggplot2) | |||
library(scales) | |||
ggplot(df, aes(x = month, y = x, group = metric)) + geom_line(aes(color = metric)) + | |||
scale_x_date(breaks = "years", labels = date_format("%Y")) + scale_y_log10("Average count per post", | |||
breaks = c(10, 100, 1000, 10000, 50000)) + theme_bw() + theme(axis.title.x = element_blank()) | |||
</pre> | |||
<pre> | |||
post_id <- head(page$id, n = 1) ## ID of most recent post | |||
post <- getPost(post_id, token, n = 1000, likes = TRUE, comments = FALSE) | |||
users <- getUsers(post$likes$from_id, token) | |||
table(users$gender) # gender | |||
table(substr(users$locale, 4, 5)) # country | |||
</pre> | |||
[[Category:R]] | [[Category:R]] | ||
Latest revision as of 15:47, 9 August 2016
Also see - Getting Started with R | R Articles | R Notes
- http://pablobarbera.com/blog/archives/3.html
- https://github.com/pablobarbera/streamR
- Automated Data Collection with R: A Practical Guide to Web Scraping and Text Mining
> install.packages("ROAuth")
also installing the dependencies ‘bitops’, ‘RCurl’, ‘digest’
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/bitops_1.0-6.zip'
Content type 'application/zip' length 36372 bytes (35 KB)
downloaded 35 KB
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/RCurl_1.95-4.8.zip'
Content type 'application/zip' length 2854329 bytes (2.7 MB)
downloaded 2.7 MB
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/digest_0.6.10.zip'
Content type 'application/zip' length 169671 bytes (165 KB)
downloaded 165 KB
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/ROAuth_0.9.6.zip'
Content type 'application/zip' length 52222 bytes (50 KB)
downloaded 50 KB
...
> install.packages("httr")
trying URL 'http://cran.ms.unimelb.edu.au/bin/windows/contrib/3.3/httr_1.2.1.zip'
Content type 'application/zip' length 281284 bytes (274 KB)
downloaded 274 KB
...
> library(httr)
> Sys.setenv(FACEBOOK_CONSUMER_SECRET = "AAAA")
> facebook <- oauth_endpoint(
authorize = "https://www.facebook.com/dialog/oauth",
access = "https://graph.facebook.com/oauth/access_token")
> fb_app <- oauth_app("facebook", "1234567890")
Some of the R commands...
install.packages("Rfacebook") # from CRAN
library(devtools)
install_github("Rfacebook", "pablobarbera", subdir = "Rfacebook") # from GitHub
library(Rfacebook)
# token generated here: https://developers.facebook.com/tools/explorer
token <- "XXXXXXXXXXXXXX"
me <- getUsers("xxxx", token, private_info = TRUE)
me$name # my name
my_friends <- getFriends(token, simplify = TRUE) head(my_friends$id, n = 1) # get lowest user ID
my_friends_info <- getUsers(my_friends$id, token, private_info = TRUE) table(my_friends_info$gender) # gender
table(substr(my_friends_info$locale, 1, 2)) # language table(substr(my_friends_info$locale, 4, 5)) # country table(my_friends_info$relationship_status)["It's complicated"] # relationship status
mat <- getNetwork(token, format = "adj.matrix") dim(mat)
See - http://blog.revolutionanalytics.com/2013/11/how-to-analyze-you-facebook-friends-network-with-r.html
posts <- searchFacebook(string = "upworthy", token, n = 500, since = "20 january 2016 00:00", until = "10 august2016 10:00") posts[which.max(posts$likes_count), ]
## convert Facebook date format to R date format
format.facebook.date <- function(datestring) {
date <- as.POSIXct(datestring, format = "%Y-%m-%dT%H:%M:%S+0000", tz = "GMT")
}
## aggregate metric counts over month
aggregate.metric <- function(metric) {
m <- aggregate(page[[paste0(metric, "_count")]], list(month = page$month),
mean)
m$month <- as.Date(paste0(m$month, "-15"))
m$metric <- metric
return(m)
}
# create data frame with average metric counts per month
page$datetime <- format.facebook.date(page$created_time)
page$month <- format(page$datetime, "%Y-%m")
df.list <- lapply(c("likes", "comments", "shares"), aggregate.metric)
df <- do.call(rbind, df.list)
# visualize evolution in metric
library(ggplot2)
library(scales)
ggplot(df, aes(x = month, y = x, group = metric)) + geom_line(aes(color = metric)) +
scale_x_date(breaks = "years", labels = date_format("%Y")) + scale_y_log10("Average count per post",
breaks = c(10, 100, 1000, 10000, 50000)) + theme_bw() + theme(axis.title.x = element_blank())
post_id <- head(page$id, n = 1) ## ID of most recent post post <- getPost(post_id, token, n = 1000, likes = TRUE, comments = FALSE) users <- getUsers(post$likes$from_id, token) table(users$gender) # gender table(substr(users$locale, 4, 5)) # country