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.r b/R/docker-splash.r similarity index 65% rename from R/docker.r rename to R/docker-splash.r index 406539a..2c1c311 100644 --- a/R/docker.r +++ b/R/docker-splash.r @@ -10,12 +10,16 @@ #' stop_splash(splash_container) #' } install_splash <- function(host = harbor::localhost) { - harbor::docker_pull(host, "scrapinghub/splash") + 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]. @@ -26,13 +30,22 @@ install_splash <- function(host = harbor::localhost) { #' splash_container <- start_splash() #' stop_splash(splash_container) #' } -start_splash <- function(host = harbor::localhost) { +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 = "scrapinghub/splash", + image = "hrbrmstr/splashttpd", detach = TRUE, - docker_opts = c("-p", "5023:5023", - "-p", "8050:8050", - "-p", "8051:8051")) + docker_opts = doc_opts) } #' Stop a running a Splash server Docker container 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}