Tools to Work with the 'Splash' JavaScript Rendering Service in R
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.5KB

  1. #' Retrieve the Docker image for Splash
  2. #'
  3. #' @md
  4. #' @param host Docker host; defaults to `localhost`
  5. #' @return `harbor` `host` object
  6. #' @export
  7. #' @examples \dontrun{
  8. #' install_splash()
  9. #' splash_container <- start_splash()
  10. #' stop_splash(splash_container)
  11. #' }
  12. install_splash <- function(host = harbor::localhost) {
  13. harbor::docker_pull(host, "hrbrmstr/splashttpd")
  14. }
  15. #' Start a Splash server Docker container
  16. #'
  17. #' If using this in an automation context, you should consider adding a
  18. #' `Sys.sleep(3)` (or higher) after starting the docker container.
  19. #'
  20. #' @param host Docker host; defaults to `localhost`
  21. #' @param add_tempdir This is `FALSE` initially since you could try to run
  22. #' the splash image on a remote system. It has to be a local one for this to work.
  23. #' If `TRUE` then a local temporary directory (made with [tempdir()])
  24. #' will be added to the mount configuration for use with [render_file()]. You will need to
  25. #' ensure the necessary system temp dirs are accessible as a mounts. For
  26. #' macOS this means adding `/private` to said Docker config.
  27. #' @note you need Docker running on your system and have pulled the container with
  28. #' [install_splash] for this to work. You should save the resultant `host`
  29. #' object for use in [stop_splash].
  30. #' @return `harbor` `container` object
  31. #' @export
  32. #' @examples \dontrun{
  33. #' install_splash()
  34. #' splash_container <- start_splash()
  35. #' stop_splash(splash_container)
  36. #' }
  37. start_splash <- function(host = harbor::localhost, add_tempdir=FALSE) {
  38. doc_opts <- c("-p", "5023:5023",
  39. "-p", "8050:8050",
  40. "-p", "8051:8051")
  41. if (add_tempdir)
  42. doc_opts <- c(doc_opts,
  43. sprintf("--volume=%s", sprintf("%s:/splashfiles", .pkgenv$temp_dir)))
  44. # purrr::walk(doc_opts, message)
  45. harbor::docker_run(host,
  46. image = "hrbrmstr/splashttpd",
  47. detach = TRUE,
  48. docker_opts = doc_opts)
  49. }
  50. #' Stop a running a Splash server Docker container
  51. #'
  52. #' @param splash_container Docker `container` object created by [start_splash()]
  53. #' @note you need Docker running on your system and have pulled the container with
  54. #' [install_splash()] and started the Splash container with [start_splash()] for this
  55. #' to work. You will need the `container` object from [start_splash()] for this to work.
  56. #' @export
  57. #' @examples \dontrun{
  58. #' install_splash()
  59. #' splash_container <- start_splash()
  60. #' stop_splash(splash_container)
  61. #' }
  62. stop_splash <- function(splash_container) {
  63. harbor::container_rm(splash_container, force=TRUE)
  64. }