Skip to content

Commit

Permalink
move functions into helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Lyttle committed May 21, 2016
1 parent 3c78b09 commit ce00aca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
19 changes: 5 additions & 14 deletions R/embed.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
# helper function to convert logical into a character parameter
.convert_allowfullscreen <- function(x){

if (x){
result <- ""
} else {
result <- NULL
}

result
}

#' Creates an iframe for video-embedding
#'
#' @param id character, url identifier
#' @param height numeric, height of iframe (px)
#' @param width numeric, width of iframe (px)
#' @param frameborder numeric, size of frame border (px)
#' @param allow_full_screen logical, indicates if to allow fullscreen
#' @param query list of items to include in query string
#'
#' @return html code describing iframe
#' @examples
Expand All @@ -32,10 +21,10 @@ embed_vimeo <- function(id, width = 500, height = 281,

url <- httr::parse_url("https://player.vimeo.com/video")

# update url
url$path <- paste(url$path, id, sep = "/")
url$query <- query


htmltools::tags$iframe(
src = httr::build_url(url),
width = width,
Expand All @@ -47,7 +36,9 @@ embed_vimeo <- function(id, width = 500, height = 281,
)
}

#' @rdname embed_vimeo
#' Creates an iframe for video-embedding
#'
#' @inheritParams embed_vimeo
#'
#' @examples
#' embed_youtube("dQw4w9WgXcQ")
Expand Down
51 changes: 51 additions & 0 deletions R/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# helper function to convert logical into a character parameter
.convert_allowfullscreen <- function(x){

if (x){
result <- ""
} else {
result <- NULL
}

result
}

#' Gets number of seconds given a string
#'
#' This is a helper function to get the number of seconds.
#'
#' This could be useful for composing query parameters for YouTube embeds.
#'
#' @param x character, describes a time duration, i.e. "3m15s"
#'
#' @return numeric, number of seconds
#' @examples
#' secs("45s")
#' secs("3m15s")
#' secs("1h1m5s")
#'
#' @seealso \code{\link{embed_youtube}}
#' @export
#
secs <- function(x){

parse <- function(x, char){
regex <- paste0("(\\d*)", char)

if (stringr::str_detect(x, regex)){
num <- as.numeric(stringr::str_match(x, regex)[[2]])
} else {
num <- 0
}

num
}

hours <- parse(x, "h")
minutes <- parse(x, "m")
seconds <- parse(x, "s")

result <- seconds + 60*(minutes + 60*hours)

result
}

0 comments on commit ce00aca

Please sign in to comment.