Browse Source

render file

master
boB Rudis 5 years ago
parent
commit
f0201015ad
  1. 1
      NAMESPACE
  2. 25
      R/docker-splash.r
  3. 31
      R/render_file.R
  4. 6
      R/zzz.r
  5. 2
      man/install_splash.Rd
  6. 28
      man/render_file.Rd
  7. 9
      man/start_splash.Rd
  8. 2
      man/stop_splash.Rd

1
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)

25
R/docker.r → 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

31
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 "`<width>x<height>`". 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)
}
}

6
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)
}

2
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}

28
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{<width>x<height>}". 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
}

9
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

2
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}

Loading…
Cancel
Save