Developing a Shopify App With R

Ben83
Shopify Partner
13 0 6

Hi, I was wondering if it's possible to build a shopify app using R (specifically http://shiny.rstudio.com/).  Has anyone done or seen this?

Pre-emptive response to "Why don't you use Rails?"

There's a big community of data scientist like me who are good with crunching numbers and building models in R, but suck at web development.  R Shiny makes it easy to build web apps, so we can focus on what we're good at - statistics.  So, if only I could integrate R with Shopify I think I could build some really cool stuff (e.g. product reccomendation engines, customer lifetime value estimator, etc.)

What I've tried so far

Using the httr package, I was able to "install" an app on a dev store, and then query that store's orders.  This looks like

# OAuth Methods

apikey <- "my_api_key89338f84fhp349hr"
secret <- "my_secret_keyf89j438f0j239fn4"

#======================================================================================================
# Get the access_token

getToken <- function(storename="bens-test-store-4"){
  # Returns the shopify_token
  
  shopify_endpoints <- oauth_endpoint(authorize=paste0("https://", storename, ".myshopify.com/admin/oauth/authorize"),
                                      access=paste0("https://", storename, ".myshopify.com/admin/oauth/access_token"))
  shopify_app <- oauth_app("shopify", key=apikey, secret=secret)
  shopify_token <- oauth2.0_token(shopify_endpoints, shopify_app, scope="read_orders")
  return(shopify_token)
}

#======================================================================================================
# Make requests

getOrders <- function(storename="bens-test-store-4", token){
  req <- GET(paste0("https://", storename, ".myshopify.com/admin/orders.json"), authenticate(user=apikey, password=token$credentials$access_token))
  stop_for_status(req)
  orders <- content(req, as="parsed")
  return(orders)
}

shopify_token <- getToken()
getOrders(token=shopify_token)

What's making my brain spin is how the flow of user authentication works.  For example, suppose my app lives at myapp.com.  When someone goes to myapp.com, a few situations could arise

  1. User has already installed myapp and is currently logged in
  2. User has already installed myapp and is not logged in
  3. User has not installed myapp

How do people check for each of these scenarios and accordingly 1) prompt the user to sign up, 2) prompt the user to login, or 3) Display the user's data (assuming he is logged in)?

0 Likes
Jamie_D_
Shopify Staff (Retired)
Shopify Staff (Retired)
531 1 101

Hey Ben,

It's definitely interesting to hear of someone building Shopify applications in R -- I wasn't aware of the potential for building web applications with R.

The most naive approach here is to simply perform the OAuth handshake every time. This is the method described in the tutorial here. Upon redirecting the merchant to the installation URL (e.g. http://#{shop}/admin/oauth/authorize?client_id=#{@key}&scope=#{scopes}&redirect_uri=https://#{@app_u...), the user will be prompted with an installation screen if they have not yet installed the app. If the app is already installed, they will simply be redirected to the redirect_uri immediately.

A more complicated login flow would involve using sessions. You can take a look at our example Rails application shopify_app for some insight on this.

Hope this helps. Happy building!

Cheers,

Jamie

kleinhansda
New Member
1 0 0

Hi @Ben83 

As a fellow data scientist I had just the same idea. Did you finally manage?

Best,

David

0 Likes