NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true") knitr::opts_chunk$set( collapse = TRUE, comment = "#>", purl = NOT_CRAN, eval = NOT_CRAN )
First, load the Rinstapkg package and log in. There are two ways to authenticate:
It is recommended to use OAuth 2.0 if you apply for and are approved for an Instagram App. This way multiple users can use your app without having to explicity share their credentials with you. Note that the OAuth credentials will be stored in locally cached file entitled ".httr-oauth-Rinstapkg" in the current working directory.
If you do not have an approved app, you can still use this package by providing your username and password.
library(Rinstapkg) # OAuth2.0 token authentication, which we won't use here #token_path <- here::here("tests", "testthat", "Rinstapkg_token.rds") #suppressMessages(ig_auth(token = token_path, verbose = FALSE)) # Username-Password authentication settings <- readRDS(here::here("tests", "testthat", "Rinstapkg_test_settings.rds")) ig_auth(username=settings$dev$username, password=settings$dev$password)
library(Rinstapkg) ig_auth(username = "MY_USERNAME", password = "MY_PASSWORD")
After logging in with ig_auth()
, you can check your connectivity by looking at
the information returned about your timeline (ig_my_timeline()
), inbox (ig_my_inbox()
),
or recent activity (ig_my_recent_activity()
). It should be information about you!
A simple function to start with is verifying that you can retrieve your timeline as
it would appear in the Instagramm app or online. This function returns data that
would appear in the authenticated user's timeline feed. By default, the data is
returned as a tidy tbl_df
where each row represents one post from the feed. If
you prefer to work with a list format, then just specify return_df=FALSE
as an
argument.
timeline_results <- ig_my_timeline() timeline_results
With the ig_get_followers()
function you can retrieve a tbl_df
of all the users
that follow a particular user. Just provide the user_id
of the account whose followers
you would like to get. NOTE: The Instagram APIs use Ids to retrieve information so
instead of giving the account's username (typically starting with an \@symbol), you
need to first grab the user_id
of that account using the username, then supply it
to the ig_get_followers()
function.
# Get Justin's Biebers beliebers! # Side Note: A belieber is a HUGE Justin Bieber fan. bieber_user_id <- ig_get_user_id("justinbieber") follower_results <- ig_get_followers(bieber_user_id) follower_results
In the example above you'll notice that we didn't retrieve all 100M+ followers that
Justin Bieber has. By default the function only returns the top 10 pages of followers,
but you can set the max_pages
argument equal to Inf
to return all of them. Caution:
This might take awhile!
# return all 100M+ followers of Justin Bieber follower_results <- ig_get_followers(bieber_user_id, max_pages = Inf)
With the ig_get_following()
function you can retrieve a tbl_df
of all the users
that a particular user is following. For example, Justin Bieber follows ~100 users. Who
are those lucky few?
following_results <- ig_get_following(bieber_user_id) following_results
The Rinstapkg package has quite a bit of unit test coverage. These tests are an excellent source of examples because they cover most all cases of utilizing the package functions, which goes beyond what we have covered here in the "Getting Started" vignette.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.