From f0201015ad06bf7a7fb6a5fe0807c56a5b1fbd20 Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Thu, 9 Feb 2017 16:17:10 -0500 Subject: [PATCH] render file --- NAMESPACE | 1 + R/docker-splash.r | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ R/docker.r | 53 ----------------------------------------- R/render_file.R | 31 ++++++++++++++++++++++++ R/zzz.r | 6 +++++ man/install_splash.Rd | 2 +- man/render_file.Rd | 28 ++++++++++++++++++++++ man/start_splash.Rd | 9 +++++-- man/stop_splash.Rd | 2 +- 9 files changed, 141 insertions(+), 57 deletions(-) create mode 100644 R/docker-splash.r delete mode 100644 R/docker.r create mode 100644 R/render_file.R create mode 100644 R/zzz.r create mode 100644 man/render_file.Rd diff --git a/NAMESPACE b/NAMESPACE index db8f02e..400d17a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(HARviewer) export(HARviewerOutput) export(install_splash) export(renderHARviewer) +export(render_file) export(render_har) export(render_html) export(render_jpeg) diff --git a/R/docker-splash.r b/R/docker-splash.r new file mode 100644 index 0000000..2c1c311 --- /dev/null +++ b/R/docker-splash.r @@ -0,0 +1,66 @@ +#' Retrieve the Docker image for Splash +#' +#' @md +#' @param host Docker host; defauolts to `localhost` +#' @return `harbor` `host` object +#' @export +#' @examples \dontrun{ +#' install_splash() +#' splash_container <- start_splash() +#' stop_splash(splash_container) +#' } +install_splash <- function(host = harbor::localhost) { + harbor::docker_pull(host, "hrbrmstr/splashttpd") +} + +#' Start a Splash server Docker container +#' +#' @param host Docker host; defauolts to `localhost` +#' @param add_tempdir if `TRUE` then a local temporary directory (made with [tempdir]()) +#' will be added to the mount configuration for use with [render_file](). You will need to +#' ensure the necessary system temp dirs are accessible as a mounts. For +#' macOS this means adding `/private` to said Docker config. +#' @note you need Docker running on your system and have pulled the container with +#' [install_spash] for this to work. You should save the resultant `host` +#' object for use in [stop_splash]. +#' @return `harbor` `container` object +#' @export +#' @examples \dontrun{ +#' install_splash() +#' splash_container <- start_splash() +#' stop_splash(splash_container) +#' } +start_splash <- function(host = harbor::localhost, add_tempdir=TRUE) { + + doc_opts <- c("-p", "5023:5023", + "-p", "8050:8050", + "-p", "8051:8051") + + if (add_tempdir) + doc_opts <- c(doc_opts, + sprintf("--volume=%s", sprintf("%s:/splashfiles", .pkgenv$temp_dir))) + + # purrr::walk(doc_opts, message) + + harbor::docker_run(host, + image = "hrbrmstr/splashttpd", + detach = TRUE, + docker_opts = doc_opts) +} + +#' Stop a running a Splash server Docker container +#' +#' @param splash_container saved Splash container id from [start_splash] +#' @param splash_container Docker `container` object created by [start_splash] +#' @note you need Docker running on your system and have pulled the container with +#' [install_spash] and started the Splash container with [start_splash] for this +#' to work. You will need the `container` object from [start_splash] for this to work. +#' @export +#' @examples \dontrun{ +#' install_splash() +#' splash_container <- start_splash() +#' stop_splash(splash_container) +#' } +stop_splash <- function(splash_container) { + harbor::container_rm(splash_container, force=TRUE) +} diff --git a/R/docker.r b/R/docker.r deleted file mode 100644 index 406539a..0000000 --- a/R/docker.r +++ /dev/null @@ -1,53 +0,0 @@ -#' Retrieve the Docker image for Splash -#' -#' @md -#' @param host Docker host; defauolts to `localhost` -#' @return `harbor` `host` object -#' @export -#' @examples \dontrun{ -#' install_splash() -#' splash_container <- start_splash() -#' stop_splash(splash_container) -#' } -install_splash <- function(host = harbor::localhost) { - harbor::docker_pull(host, "scrapinghub/splash") -} - -#' Start a Splash server Docker container -#' -#' @param host Docker host; defauolts to `localhost` -#' @note you need Docker running on your system and have pulled the container with -#' [install_spash] for this to work. You should save the resultant `host` -#' object for use in [stop_splash]. -#' @return `harbor` `container` object -#' @export -#' @examples \dontrun{ -#' install_splash() -#' splash_container <- start_splash() -#' stop_splash(splash_container) -#' } -start_splash <- function(host = harbor::localhost) { - harbor::docker_run(host, - image = "scrapinghub/splash", - detach = TRUE, - docker_opts = c("-p", "5023:5023", - "-p", "8050:8050", - "-p", "8051:8051")) -} - -#' Stop a running a Splash server Docker container -#' -#' @param splash_container saved Splash container id from [start_splash] -#' @param splash_container Docker `container` object created by [start_splash] -#' @note you need Docker running on your system and have pulled the container with -#' [install_spash] and started the Splash container with [start_splash] for this -#' to work. You will need the `container` object from [start_splash] for this to work. -#' @export -#' @examples \dontrun{ -#' install_splash() -#' splash_container <- start_splash() -#' stop_splash(splash_container) -#' } -stop_splash <- function(splash_container) { - harbor::container_rm(splash_container, force=TRUE) -} diff --git a/R/render_file.R b/R/render_file.R new file mode 100644 index 0000000..c986e58 --- /dev/null +++ b/R/render_file.R @@ -0,0 +1,31 @@ +#' Return the HTML or image (png) of the javascript-rendered page in a local file +#' +#' The suggested use-case for this is rendering a widget +#' +#' TODO Test if container is running +#' TODO Enable passing in of an htmlwidget and use saveWidget +#' +#' @md +#' @param splash_obj Object created by a call to [splash]() +#' @param file_path Absolute path to a filename on the local host. **This only works with a locally running Splash instance started with [start_splash]().** +#' @param output either `html` or `png` to get the page content or an image capture +#' @param viewport View width and height (in pixels) of the browser viewport to render the web page. Format is "`x`". e.g. 800x600. Default value is 1024x768. +#' @return An XML document or `magick` object +#' @export +render_file <- function(splash_obj, file_path, output=c("html", "png"), wait=0, viewport="1024x768") { + + output <- match.arg(output, c("html", "png")) + + file.copy(file_path, .pkgenv$temp_dir) + + fil <- basename(file_path) + + URL <- sprintf("http://localhost:9999/%s", fil) + + if (output == "html") { + render_html(splash_obj, URL, wait=wait, viewport=viewport) + } else { + render_png(splash_obj, URL, wait=wait, viewport=viewport) + } + +} \ No newline at end of file diff --git a/R/zzz.r b/R/zzz.r new file mode 100644 index 0000000..010f25a --- /dev/null +++ b/R/zzz.r @@ -0,0 +1,6 @@ +.pkgenv <- new.env(parent=emptyenv()) + +.onAttach <- function(...) { + temp_dir <- normalizePath(gsub("//", "/", path.expand(tempdir()))) + assign("temp_dir", temp_dir, envir=.pkgenv) +} \ No newline at end of file diff --git a/man/install_splash.Rd b/man/install_splash.Rd index ce1924e..d665ac3 100644 --- a/man/install_splash.Rd +++ b/man/install_splash.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/docker.r +% Please edit documentation in R/docker-splash.r \name{install_splash} \alias{install_splash} \title{Retrieve the Docker image for Splash} diff --git a/man/render_file.Rd b/man/render_file.Rd new file mode 100644 index 0000000..f67ba25 --- /dev/null +++ b/man/render_file.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/render_file.R +\name{render_file} +\alias{render_file} +\title{Return the HTML or image (png) of the javascript-rendered page in a local file} +\usage{ +render_file(splash_obj, file_path, output = c("html", "png"), wait = 0, + viewport = "1024x768") +} +\arguments{ +\item{splash_obj}{Object created by a call to \url{splash}} + +\item{file_path}{Absolute path to a filename on the local host. \strong{This only works with a locally running Splash instance started with \url{start_splash}.}} + +\item{output}{either \code{html} or \code{png} to get the page content or an image capture} + +\item{viewport}{View width and height (in pixels) of the browser viewport to render the web page. Format is "\code{x}". e.g. 800x600. Default value is 1024x768.} +} +\value{ +An XML document or \code{magick} object +} +\description{ +The suggested use-case for this is rendering a widget +} +\details{ +TODO Test if container is running +TODO Enable passing in of an htmlwidget and use saveWidget +} diff --git a/man/start_splash.Rd b/man/start_splash.Rd index 702f471..8f6ee5e 100644 --- a/man/start_splash.Rd +++ b/man/start_splash.Rd @@ -1,13 +1,18 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/docker.r +% Please edit documentation in R/docker-splash.r \name{start_splash} \alias{start_splash} \title{Start a Splash server Docker container} \usage{ -start_splash(host = harbor::localhost) +start_splash(host = harbor::localhost, add_tempdir = TRUE) } \arguments{ \item{host}{Docker host; defauolts to `localhost`} + +\item{add_tempdir}{if `TRUE` then a local temporary directory (made with [tempdir]()) +will be added to the mount configuration for use with [render_file](). You will need to + ensure the necessary system temp dirs are accessible as a mounts. For + macOS this means adding `/private` to said Docker config.} } \value{ `harbor` `container` object diff --git a/man/stop_splash.Rd b/man/stop_splash.Rd index 3d22b5d..85a163a 100644 --- a/man/stop_splash.Rd +++ b/man/stop_splash.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/docker.r +% Please edit documentation in R/docker-splash.r \name{stop_splash} \alias{stop_splash} \title{Stop a running a Splash server Docker container}