Tools to Work with the 'Splash' JavaScript Rendering Service in R
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

62 řádky
2.4KB

  1. #' Execute a custom rendering script and return a result.
  2. #'
  3. #' @md
  4. #' @param splash_obj Object created by a call to [splash()]
  5. #' @param lua_source Browser automation script. See [Splash Script](http://splash.readthedocs.io/en/stable/scripting-tutorial.html#scripting-tutorial) Tutorial for more info.
  6. #' @param timeout A timeout (in seconds) for the render (defaults to 30). Without
  7. #' reconfiguring the startup parameters of the Splash server (not this package)
  8. #' the maximum allowed value for the timeout is 60 seconds.
  9. #' @param allowed_domains Comma-separated list of allowed domain names. If present, Splash
  10. #' won’t load anything neither from domains not in this list nor from subdomains of
  11. #' domains not in this list.
  12. #' @param proxy Proxy profile name or proxy URL.
  13. #' @param filters Comma-separated list of request filter names.
  14. #' @param save_args A list of argument names to put in cache.
  15. #' @param load_args Parameter values to load from cache
  16. #' @return `raw` content from the `httr` call. Given the vast diversity of possible return
  17. #' values, it's up to the caller to handle the return value.
  18. #' @family splash_renderers
  19. #' @export
  20. #' @examples \dontrun{
  21. #' splash_local %>%
  22. #' execute_lua('
  23. #' function main(splash)
  24. #' splash:go("https://projects.fivethirtyeight.com/congress-trump-score/")
  25. #' splash:wait(0.5)
  26. #' return splash:evaljs("memberScores")
  27. #' end
  28. #' ') -> res
  29. #'
  30. #' rawToChar(res) %>%
  31. #' jsonlite::fromJSON(flatten=TRUE) %>%
  32. #' purrr::map(tibble::as_tibble) -> member_scores
  33. #'
  34. #' member_scores
  35. #' }
  36. execute_lua <- function(splash_obj, lua_source, timeout=30, allowed_domains,
  37. proxy, filters, save_args, load_args) {
  38. params <- list(lua_source=lua_source, timeout=timeout)
  39. if (!missing(allowed_domains)) params$allowed_domains <- allowed_domains
  40. if (!missing(proxy)) params$proxy <- proxy
  41. if (!missing(filters)) params$filters <- filters
  42. if (!missing(save_args)) params$save_args <- save_args
  43. if (!missing(load_args)) params$load_args <- load_args
  44. if (is.null(splash_obj$user)) {
  45. res <- httr::GET(splash_url(splash_obj), path="execute", encode="json", query=params)
  46. } else {
  47. res <- httr::GET(
  48. splash_url(splash_obj), path="execute", encode="json", query=params,
  49. httr::authenticate(splash_obj$user, splash_obj$pass)
  50. )
  51. }
  52. httr::stop_for_status(res)
  53. out <- httr::content(res, as="raw")
  54. out
  55. }