The following code is modified based on the post Analyze Instagram with R on R-bloggers.
library(rjson)
library(RCurl)
library(httr)
library(rCharts)
library(reshape2)
## Install from github if you don't have the package "rCharts".
if (FALSE){
require(devtools)
install_github('rCharts', 'ramnathv')
}
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
full_url <- oauth_callback()
full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x=full_url, replacement="\\1")
print(full_url)
Now go to http://instagram.com/developer/ to register an application using your Instagtam account. I don’t think the “Website” column really matters if you just want to plot the data. I put my blog URL in it.
Then go to Register a New Client, put the full_url printed above in the OAuth redirect_uri column.
Paste the relevant information of your app to define the following variables.
app_name <- "yourAppName"
client_id <- "yourClientID"
client_secret <- "yourClientSecret"
scope <- "basic" # no need to change this
instagram <- oauth_endpoint(
authorize = "https://api.instagram.com/oauth/authorize",
access = "https://api.instagram.com/oauth/access_token")
myapp <- oauth_app(app_name, client_id, client_secret)
The browser should open after running the following line, asking your permission to the app.
ig_oauth <- oauth2.0_token(instagram, myapp,scope="basic", type = "application/x-www-form-urlencoded",cache=FALSE)
tmp <- strsplit(toString(names(ig_oauth$credentials)), '"')
token <- tmp[[1]][4]
Here comes the fun part. You can analyze your own account, or anyone else’s.
## Specify the user name you want to explore.
## This is my account. Also check out my favotite cat breeder's account "fjarilflickans"
username <- "min.ma"
## Search for the username. The function will give a list of users (around 50 people) with user names similar to the one you provided.
## Select the first one which should be the perfect match provided that the user exists.
user_info <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/search?q=',username,'&access_token=',token,sep="")),unexpected.escape = "keep")
received_profile <- user_info$data[[1]]
## Get the recent media data (last 20 pictures) and plot the number of Likes.
## Return an error message if the user does not exist.
if(grepl(received_profile$username,username))
{
user_id <- received_profile$id
#Get recent media
media <- fromJSON(getURL(paste('https://api.instagram.com/v1/users/',user_id,'/media/recent/?access_token=',token,sep="")))
df = data.frame(no = 1:length(media$data))
for(i in 1:length(media$data))
{
#comments
df$comments[i] <-media$data[[i]]$comments$count
#likes:
df$likes[i] <- media$data[[i]]$likes$count
#date
df$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]]$created_time), origin="1970-01-01"))
}
#Visualization -- interactive plot using rCharts
m1 <- mPlot(x = "date", y = c("likes", "comments"), data = df, type = "Line",
labels = c("Number of Likes", "Number of Comments"))
m1$set(pointSize = 1, lineWidth = 2, lineColors = c("#66C2A5", "#FC8D62"))
}else
{
print("Error: User not found!")
}