analyze instagram R

Analyze Instagram with R

analyze instagram R
This tutorial will show you how you create an Instagram app, create an authentication process with R and get data via the Instagram API.
There is no R package for this yet so we have to configure the authentication and data download process on our own. But Instagram offers a pretty good documented API and uses oAuth 2 which makes it easy to use with R and the httr package for example.


The place to start for everybody who wants to work with the Instagram API is
Here you can find all the information you need and also manage your apps.
So click on „Register Your Application“ and go through the login.
R analyze instagram
On the next screen you can set the parameters for your app. Choose an application name ,write a small description of what your app will be about and add a webiste
Then you have to enter an OAuth redirect URI. To choose it go to your R console and execute following code:
This will show you the preferred callback URI for httr. Copy this URL and paste it in your app settings.
This is how my settings look like:
Analyze instagram with R
After clicking on „Register“ you will be redirected your app authentication details we will need for our analysis.
In R we have to define 4 variables:
The first 3 you get from your app settings. The third on scope is basically the level of authorization you want to get. Basic is enough to download data like likes or comments. If you actually want to post something to Instagram you need another scope. You can find more information on the Instagram developer page about that.
Then we create our Instagram in R for the httr package. This is the app we will use to connect to the API. To do so we have to provide the access points.
In the next step we do the authentication
Now your browser should open and ask you to give permission to the app. After you returned to R you should have received your access token.

Our analysis focuses on analysing our own profile. Instagram is very restrictive when it comes to sandbox apps and they don´t return any other data than the data from your own profile.

Analyze Instagram with R

We can now download your most recent media items from Instagram.
This returns the recent 20 pictures of the user we will use for our analysis. We go through them with a for loop to extract the count of likes and comments and the date and time the photo was posted.
Instagram uses UNIX timestamps as their date. So we have to convert it to make it readable.


Now we can visualize the data. I will use the rCharts package to so. Of course you can also use ggplot2 or whatever package you like.

You can find the complete code of the tutorial on my github account:

Julian Hillebrand

During my time at university and learning about the basics of economics I started heavily exploring the possibilities and changes caused by digital disruptions and the process of digital transformation, whereby I focused on the importance of data and data analytics and combination with marketing and management.
My personal focus of interest lies heavily on technology, digital marketing and data analytics. I made early acquaintance with programming and digital technology and never stop being interested in following the newest innovations.

I am an open, communicative and curious person. I enjoy writing, blogging and speaking about technology.

  • Sal

    Hi Julia. Interesting approach. Thank you for sharing.

    Which package is fromJSON() from ? When I follow your exact procedure, I got an error message “Error: could not find function “fromJSON” ”


    • Hey,
      thanks for your hint.
      I used the fromJSON() function from the rjson package.
      You can find it in the version on github:

      Hope this could solve your problem


      • Sal

        Thank. I installed the proper json package. Is it normal to have the following warning message ?
        Warning message:
        In fromJSON(getURL(paste(“”, :
        unexpected escaped character ‘\_’ at pos 24. Keeping value.

        • Actually yes. Sometimes the Instagram API returns character combinations the fromJSON function can´t really deal with and it would normally cause it to abort the call. But I added this unexpected.escape = “keep” parameter to the function call. This will make the fromJSON function just give a warning message and no error message and it will also keep the data it received.


      • Sal

        Everything works fine now. Thank you
        My understanding is that the number of pictures imported into the ‘media’ list is set to 20 by default. Can this be modified ? (any number, or ‘all’, or ‘last 3 months’)

  • Rees Klintworth

    Is there any way to automatically close the browser page/get the access_token so that I don’t have to manually close the browser page?

    • Hey Rees
      I am also looking for a way at the moment. This would also be necessary I you would want to use such authentication in a shiny application for example.
      I´ll let you know when I can figure out a solution.


  • You can get more than 20 items, but you’ll have to set the pagination for the JSON response. Here’s how you get there:



    • Hey Marco
      thank you for this great info. I could really add this to the tutorial.
      I used the recent function without count as this is just a proof of concept.
      And to get a general overview 20 items are enough.


  • Rees Klintworth

    Also, I have attempted to deploy this in a shiny application (, but I am getting the following error: “Error : oauth_listener() needs an interactive environment.”

    Any advice as to how I can fix this? I’m new to oauth and everything that goes along with it.

    • Rees – looks like you fixed your problem! Share with us how you did?

  • philtiongson

    First of all, thanks very much for this. This is really useful and very informative. I am quite new to the world of R. Your posts are definitely helping me get the hang of using it.

    I have a question with regard to your code above. When I ran the following code –

    media <- fromJSON(getURL(paste(';,user_id,'/media/recent/?access_token=',token,sep="")))

    I get an error response from R that says the following:

    Error in function (type, msg, asError = TRUE) : SSL certificate problem: unable to get local issuer certificate

    May I ask for your help in addressing this error?

    Thank you very much!


    • Hey
      sorry for the late answer.
      I heard of several people with your problem.
      Do you use Windows and RStudio? This seems to be the case for the most people with the problem.
      Could you please try to run the code in the R console?


  • Chris

    I’ve been working on this for a couple days, because this tutorial is awesome and it’s a good display of some pretty cool things that R can do…plus, I have an excess of free time because of the holiday. Might as well put it to good use.

    I keep getting an error :

    Error in function (type, msg, asError = TRUE) :
    SSL certificate problem: unable to get local issuer certificate

    In the section where we look to bring the data from the instagram api into R:

    user_info <- fromJSON(getURL(paste(';,username,'&access_token=',token,sep="")),unexpected.escape = "keep")

    Any suggestions on what to do? I've tried doing some basic research on RCurl, and on a whim used the flag 'ssl.verifypeer=false' but that didn't fix the issue.

    I'm on Windows 7 using RStudio, R Version 3.1.2.

    • Hey
      these SSL problems appear pretty often on windows. Did you try it in the R Console?
      Sometimes RStudio has some problems with such kind of things.
      Please tell me if that worked for you.


  • Pingback: Instagram & R | SocialFunction()()

  • Louise

    Can you help me?

    when I type the code for authentication , when you open the browser appears this error

    {“code”: 400, “error_type”: “OAuthException”, “error_message”: “Redirect URI does not match registered redirect URI”}

  • Stephane Doyen

    Hi Julian and community,

    Thanks for the great tutorial!
    I have followed it carefully and can get it to work for the REST api. Now, I’m trying to implement and adapt your example to the real time part of instagram api using the httr package.

    I’m using the following code to POST the instructions:

    r = POST(url = ‘’,
    body = “client_id=XXXXX;client_secret=XXXX;aspect=media;access_token=XXXX;callback_url=http://localhost:1410/;object=tag;object_id=selfie”,
    encode = “form”,

    But all I get in return is: error_message: chr “Invalid URL. The URL may be on a private network.”

    Any help on to handle the PubSubHubbub protocol in R would be very much appreciated.

  • Thanks for your post, it help me a lot with analyzing instagram. For those who have error of SSL, maybe you want to consider adding this line in your script. hopefully it will help your issue.

    options(RCurlOptions = list(verbose = FALSE, capath = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”), ssl.verifypeer = FALSE))

    original post by skardhamar

    • Thanks for sharing this solution!
      I will add it to the tutorial.


  • I am on a Windows 7 machine, using RStudio. I solved the SSL certificate problem with this code:

    user_info <- fromJSON(getURL(paste(';,username,
    '&access_token=',token,sep=""), .opts = list(ssl.verifypeer = FALSE)
    ),unexpected.escape = "keep")

    The addition of .opts = list(ssl.verifypeer = FALSE) fixed my problems.

    Now, I am adapting this to retrieve a complete list of users who liked a photo, but Instagram's API only returns the first 120 users. Anyone know if pagination works for the /user endpoint, i can't seem to get it to work?

    And Julian, you are a rock star. Thanks for this tutorial.

    • Hey
      sorry for the late answer and thanks for the kind words.

      A user posted this code some time ago:

      I think that should make it work for you.


  • Vanessa

    how do we fix the error:
    Redirect URI does not match registered redirect URI

    • Hey
      could you please post the exact code you used?
      And where exactly does this error show up?


  • Pingback: Instagram API & R - Jabber Cruncher()

  • the code runs successfully but at the end im not getting the chart.. help

  • Hi. I’ve followed the directions, downloaded all necessary packages but don’t get a chart at the end. Is there something I’m not doing right?

    I’ve omitted some of the script for privacy but this is what it looks like.

    #Analyze Instagram with R
    #Author: Julian Hillebrand
    #Recreated for Research Purposes by Tamren Estevez-Lopez



    ## getting callback URL
    full_url <- oauth_callback()
    full_url <- gsub("(.*localhost:[0-9]{1,5}/).*", x=full_url, replacement="\1")
    #notetoself: this gets pasted on Instagram App Settings

    app_name <- "MiningWithR"
    client_id <- "omit"
    client_secret <- "omit"
    scope = "basic"

    instagram <- oauth_endpoint(
    authorize = ";,
    access = "😉
    myapp <- oauth_app(app_name, client_id, client_secret)

    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]


    username <- "OMIT"

    #search for the username
    user_info <- fromJSON(getURL(paste(';,username,'&access_token=',token,sep="")),unexpected.escape = "keep")
    received_profile <- user_info$data[[1]]
    user_id <- received_profile$id
    #Get recent media (20 pictures)
    media <- fromJSON(getURL(paste(';,user_id,'/media/recent/?access_token=',token,sep="")))
    df = data.frame(no = 1:length(media$data))
    for(i in 1:length(media$data))
    df$comments[i] <-media$data[[i]]$comments$count
    df$likes[i] <- media$data[[i]]$likes$count
    df$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]]$created_time), origin="1970-01-01"))
    m1 <- mPlot(x = "date", y = c("likes", "comments"), type = "Line", data = df)

    Any help would be greatly appreciated!

    • Hey
      what is the content of the m1 object at the end?

      Best regards

  • I’m having the same issue as shruti. Script is running successfully but I’m not getting a chart at the end. The last line I’ve entered is

    m1 <- mPlot(x = "date", y = c("likes", "comments"), type = "Line", data = df)

  • Joseph Southwell

    Se we were using this tutorial today and it started out the day working fine but now we can no longer get a list of users.

    It comes back empty every time no matter what we put in and in the documentation for their api there is a massive changelog entry for today. They added new scope called public_content. (searching users now requires public_content but changing that didn’t help.) They talk about sandboxing but I tried a substring of my own username and that didn’t work either.

    ig_oauth tmp token user_info user_info
    [1] 200

    • Sandbox isn’t available yet. It will be a limited ‘testing’ environment. If you didn’t register an app under your account prior to today you won’t be able to do anything until I think 12/3/2015. It’s apparently an attempt to discard malicious third party apps. The sandbox appears as if it will be greatly limited if you aren’t actually developing an application and submitting for review.

    • Shixiao Cui

      I am having the same problem with you. I got an empty list too.

  • Sergio Aguado


    I have some problems when I tried to get the token in shiny apps. Do you have an example? Thank you so much.

  • Karl Miller


    I got the message

    Error in file(con, “r”) : cannot open the connection

    if I run:


    Any suggestions or recommendations?

    Thank you so much.

  • felipe

    I know this post is already old. But I have a question regarding the July 2016 changes in developer policy. Before that, everything worked fine.

    As of now, I cannot use any of instaR functions on public content due to a lack of permissions from Instagram. When I try to get those, I am asked to provide a video cast of my working app. However, I only used, and would continue to use, Instagram for research purposes.

    Do you know any means, different than creating a full working app, to get access to public Instagram data, as we had before this policy updates?

  • Nikelle

    Hi Julian, what a great tutorial. All of the code worked perfectly for me, however, like some other users, I’ve noticed that there is no data displayed in my chart. My last line of code looks like this:
    m1 <- mPlot(x = "date", y = c("likes", "comments"), type = "Line", data = df)
    I then type m1 and hit enter, and a chart came up in the viewer (I'm using RStudio) however there is nothing inside the chart aside from the axis. Any thoughts on how to fix this? Thanks!!

    • Nikelle

      Actually, I think i figured this out, though I’m not too sure how. However, I am having a problem with the for loop. I am getting this error:

      Error in media$data[[i]]$comments :
      $ operator is invalid for atomic vectors

      Do you have any idea on what is going wrong? Thanks again.

      • Abbas

        Hi Nilelle
        I found the answer
        You have to use again double bracket for count as follows:
        for(i in 1:length(media$data))
        df$comments[i] <-media$data [[i]]$comments[["count"]]

        df$likes[i] <- media$data [[i]]$likes[["count"]]

        df$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]][["created_time"]]), origin="1970-01-01"))

    • Abbas

      Hello, Thanks for tutorial. I have the same problem as Nikelle. Of course I had a problem about $ in for loop but when I replaced [[ with [ it was solved but still have no data displayed in my chart. any comments?

    • Abbas

      Hi Nikelle
      have you found your answer?
      Because I have the same problem.

  • Luis de Quadras

    I only get two columns for df, the first one is no and the second one is date, date is empty and no has a 1 and a 0. Does anyone know what’s happening?
    Thanks in advanced

    • Vaibhav Agrawal

      I am having the same problem. Did anyone find the solution?

  • mohd kasim

    Is there any methods to connect YouTube with R for getting the data
    if it is please share ..

  • Joylyn Anderson
  • raviteja

    I have tried this, but getting errors in mPlot() and I downloaded all the latest packages, still issue is not resolved. Can somebody help me

  • Hariram N

    Does this tutorial still hold good after the insta regulations lately?

  • Thanks for creating this. I’m going to give it a try and see if it works with the current instagram API. It looks like a great application for Instagram marketing.