Tools to Work with the 'Splash' JavaScript Rendering Service in R
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

74 rindas
2.9KB

  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, http2 = FALSE,
  17. engine = c("webkit", "chromium")) {
  18. wait <- check_wait(wait)
  19. engine <- match.arg(engine[1], c("webkit", "chromium"))
  20. http2 <- if (engine == "chromium") NULL else as.integer(as.logical(http2[1]))
  21. viewport <- if (engine == "chromium") NULL else jsonlite::unbox(viewport[1])
  22. render_all <- if (engine == "chromium") NULL else as.integer(render_all[1])
  23. wait <- if (is.null(render_all)) 0.5 else if (render_all & wait == 0) 0.5 else wait
  24. list(
  25. url = url,
  26. timeout = timeout,
  27. wait = wait,
  28. viewport = viewport,
  29. render_all = render_all,
  30. quality = quality,
  31. http2 = http2,
  32. engine = engine
  33. ) -> params
  34. if (!missing(width)) params$width <- width
  35. if (!missing(height)) params$height <- height
  36. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  37. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  38. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  39. if (!missing(js)) params$js <- jsonlite::unbox(js)
  40. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  41. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  42. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  43. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  44. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  45. if (!missing(images)) params$images <- as.numeric(images)
  46. if (!missing(headers)) params$headers <- headers
  47. if (!missing(body)) params$body <- jsonlite::unbox(body)
  48. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  49. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  50. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  51. if (is.null(splash_obj$user)) {
  52. res <- httr::GET(splash_url(splash_obj), path="render.jpeg", encode="json", query=params)
  53. } else {
  54. res <- httr::GET(
  55. splash_url(splash_obj), path="render.jpeg", encode="json", query=params,
  56. httr::authenticate(splash_obj$user, splash_obj$pass)
  57. )
  58. }
  59. check_or_report_status(res)
  60. magick::image_read(httr::content(res, as="raw"))
  61. }