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.

74 lines
3.2KB

  1. #' Return an image (in PNG format) of the javascript-rendered page.
  2. #'
  3. #' @md
  4. #' @param width,height Resize the rendered image to the given width/height (in
  5. #' pixels) keeping the aspect ratio. These are optional
  6. #' @param render_all If `TRUE` extend the viewport to include the whole webpage
  7. #' (possibly very tall) before rendering.
  8. #' @family splash_renderers
  9. #' @return a [magick] image object
  10. #' @references [Splash docs](http://splash.readthedocs.io/en/stable/index.html)
  11. #' @inheritParams render_html
  12. #' @export
  13. #' @examples \dontrun{
  14. #' render_png(url = "https://httpbin.org/")
  15. #' }
  16. render_png <- function(
  17. splash_obj = splash_local, url, base_url=NULL, width, height,
  18. timeout=30, resource_timeout, wait=0, render_all=TRUE,
  19. proxy, js, js_src, filters, allowed_domains, allowed_content_types,
  20. forbidden_content_types, viewport="full", images, headers, body,
  21. http_method, save_args, load_args, http2 = FALSE,
  22. engine = c("webkit", "chromium")) {
  23. wait <- check_wait(wait)
  24. engine <- match.arg(engine[1], c("webkit", "chromium"))
  25. http2 <- if (engine == "chromium") NULL else as.integer(as.logical(http2[1]))
  26. viewport <- if (engine == "chromium") NULL else jsonlite::unbox(viewport[1])
  27. render_all <- if (engine == "chromium") NULL else as.integer(render_all[1])
  28. wait <- if (is.null(render_all)) 0.5 else if (render_all & wait == 0) 0.5 else wait
  29. list(
  30. url = url, timeout = timeout,
  31. wait = wait,
  32. viewport = viewport,
  33. render_all = render_all,
  34. http2 = http2,
  35. engine = engine
  36. ) -> params
  37. if (!missing(width)) params$width <- width
  38. if (!missing(height)) params$height <- height
  39. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  40. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  41. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  42. if (!missing(js)) params$js <- jsonlite::unbox(js)
  43. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  44. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  45. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  46. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  47. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  48. if (!missing(images)) params$images <- as.numeric(images)
  49. if (!missing(headers)) params$headers <- headers
  50. if (!missing(body)) params$body <- jsonlite::unbox(body)
  51. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  52. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  53. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  54. if (is.null(splash_obj$user)) {
  55. res <- httr::GET(splash_url(splash_obj), path="render.png", encode="json", query=params)
  56. } else {
  57. res <- httr::GET(
  58. splash_url(splash_obj), path="render.png", encode="json", query=params,
  59. httr::authenticate(splash_obj$user, splash_obj$pass)
  60. )
  61. }
  62. check_or_report_status(res)
  63. magick::image_read(httr::content(res, as = "raw"))
  64. }