Helper functions

Ian Lyttle

2017-07-16

library("htmltools")
library("vembedr")

Start time

It’s a bit inconvenient to visit a service’s API page to figure out how do something relatively common, such as specifying a start time.

Instead, you can pipe the output of your embed function to the use_start_time() function, which figures out which service you are using, then does the “right thing” to modify the query or fragment.

The use_start_time() function will treat these all of these inputs equivalently:

embed_youtube(id = "8SGif63VW6E") %>% 
  use_start_time("4m12s")

Hat tip to Aurélien Ginolhac for suggesting this video using this start-time.

The use_start_time() function can be used with all of the embed() functions.

Vimeo caveat

For Vimeo there is one caveat: the specification of start-time causes the video to autoplay without an API recourse to prevent autoplay. Please see this Vimeo-forum thread for more informaton.

Embed based on URL

If you do not need to customization your iframe (i.e., change the default size), this may be useful to you. Simply provide the URL where the video is hosted - the embed_url() function will determine if the URL is from one of the supported services - if so, it will return the HTML to embed the video.

embed_url("https://www.youtube.com/watch?v=q2nNzNo_Xps")

If you need to make additional customization, you can use the suggest_embed() function (which this function uses) to provide the R code, which you can then modify to your needs.

Suggest code

If you have the URL of the video you want to embed, you can use the suggest_embed() function to return to you some suggested code for you to use.

Behind the scenes, the function parses the URL to determine the service, the identifier, and (if applicable) the start time.

YouTube

You can provide the URL from your browser’s address window:

suggest_embed("https://www.youtube.com/watch?v=1-vcErOPofQ")
## embed_youtube("1-vcErOPofQ")

You can also provide the URL from the YouTube “Share” button:

suggest_embed("https://youtu.be/1-vcErOPofQ?t=28s")
## embed_youtube("1-vcErOPofQ") %>%
##   use_start_time("28s")

Vimeo

suggest_embed("https://vimeo.com/131407894")
## embed_vimeo("131407894")

Channel 9

This works for the UseR! 2016 videos:

suggest_embed("https://channel9.msdn.com/Events/useR-international-R-User-conference/useR2016/Day-3-Siepr-130-Ligtning-Talks-100-PM-140-PM?ocid=player")
## embed_user2016("Day-3-Siepr-130-Ligtning-Talks-100-PM-140-PM")

It also works for the other Channel 9 videos:

suggest_embed("https://channel9.msdn.com/Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads")
## embed_channel9("Shows/Going+Deep/Brian-Beckman-Dont-fear-the-Monads")

Video specification

Let’s say you do not have a particular video in mind, but you want to experiment with the arguments and the query parameters. For this set of functions, the video id is specified, any argument you provide is passed along to embed_youtube(), embed_vimeo(), or embed_channel9().

rickroll_youtube()
rickroll_vimeo()
rickroll_channel9()