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.

62 lines
2.8KB

  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) {
  22. wait <- check_wait(wait)
  23. params <- list(
  24. url = url, timeout = timeout,
  25. wait = if (render_all & wait == 0) 0.5 else wait,
  26. viewport = jsonlite::unbox(viewport),
  27. render_all = as.numeric(render_all)
  28. )
  29. if (!missing(width)) params$width <- width
  30. if (!missing(height)) params$height <- height
  31. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  32. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  33. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  34. if (!missing(js)) params$js <- jsonlite::unbox(js)
  35. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  36. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  37. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  38. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  39. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  40. if (!missing(images)) params$images <- as.numeric(images)
  41. if (!missing(headers)) params$headers <- headers
  42. if (!missing(body)) params$body <- jsonlite::unbox(body)
  43. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  44. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  45. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  46. if (is.null(splash_obj$user)) {
  47. res <- httr::GET(splash_url(splash_obj), path="render.png", encode="json", query=params)
  48. } else {
  49. res <- httr::GET(
  50. splash_url(splash_obj), path="render.html", encode="json", query=params,
  51. httr::authenticate(splash_obj$user, splash_obj$pass)
  52. )
  53. }
  54. httr::stop_for_status(res)
  55. magick::image_read(httr::content(res, as = "raw"))
  56. }