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.

60 lines
2.5KB

  1. #' Return a image (in JPEG format) of the javascript-rendered page.
  2. #'
  3. #' @md
  4. #' @param quality JPEG quality parameter in range from 0 to 100. Default is quality=75.
  5. #' @inheritParams render_html
  6. #' @inheritParams render_png
  7. #' @family splash_renderers
  8. #' @return a [magick] image object
  9. #' @references [Splash docs](http://splash.readthedocs.io/en/stable/index.html)
  10. #' @export
  11. render_jpeg <- render_jpg <- function(
  12. splash_obj = splash_local, url, base_url=NULL, quality=75, width, height,
  13. timeout=30, resource_timeout, wait=0, render_all=TRUE,
  14. proxy, js, js_src, filters, allowed_domains, allowed_content_types,
  15. forbidden_content_types, viewport="full", images, headers, body,
  16. http_method, save_args, load_args) {
  17. wait <- check_wait(wait)
  18. params <- list(url=url, timeout=timeout,
  19. wait=if (render_all & wait == 0) 0.5 else wait,
  20. viewport=viewport,
  21. quality=quality,
  22. render_all=as.numeric(render_all))
  23. if (!missing(width)) params$width <- width
  24. if (!missing(height)) params$height <- height
  25. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  26. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  27. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  28. if (!missing(js)) params$js <- jsonlite::unbox(js)
  29. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  30. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  31. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  32. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  33. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  34. if (!missing(images)) params$images <- as.numeric(images)
  35. if (!missing(headers)) params$headers <- headers
  36. if (!missing(body)) params$body <- jsonlite::unbox(body)
  37. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  38. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  39. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  40. if (is.null(splash_obj$user)) {
  41. res <- httr::GET(splash_url(splash_obj), path="render.jpeg", encode="json", query=params)
  42. } else {
  43. res <- httr::GET(
  44. splash_url(splash_obj), path="render.html", encode="json", query=params,
  45. httr::authenticate(splash_obj$user, splash_obj$pass)
  46. )
  47. }
  48. httr::stop_for_status(res)
  49. magick::image_read(httr::content(res, as="raw"))
  50. }