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ů.

49 řádky
2.0KB

  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_sourc 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).
  7. #' @param allowed_domains Comma-separated list of allowed domain names. If present, Splash won’t load anything neither from domains not in this list nor from subdomains of domains not in this list.
  8. #' @param proxy Proxy profile name or proxy URL.
  9. #' @param filters Comma-separated list of request filter names.
  10. #' @param save_args A list of argument names to put in cache.
  11. #' @param load_args Parameter values to load from cache
  12. #' @return `raw` content from the `httr` call. Given the vast diversity of possible return values, it's up to the caller to handle the return value.
  13. #' @export
  14. #' @examples \dontrun{
  15. #' splash_local %>%
  16. #' execute_lua('
  17. #' function main(splash)
  18. #' splash:go("https://projects.fivethirtyeight.com/congress-trump-score/")
  19. #' splash:wait(0.5)
  20. #' return splash:evaljs("memberScores")
  21. #' end
  22. #' ') -> res
  23. #'
  24. #' rawToChar(res) %>%
  25. #' jsonlite::fromJSON(flatten=TRUE) %>%
  26. #' purrr::map(tibble::as_tibble) -> member_scores
  27. #'
  28. #' member_scores
  29. #' }
  30. execute_lua <- function(splash_obj, lua_source, timeout=30, allowed_domains,
  31. proxy, filters, save_args, load_args) {
  32. params <- list(lua_source=lua_source, timeout=timeout)
  33. if (!missing(allowed_domains)) params$allowed_domains <- allowed_domains
  34. if (!missing(proxy)) params$proxy <- proxy
  35. if (!missing(filters)) params$filters <- filters
  36. if (!missing(save_args)) params$save_args <- save_args
  37. if (!missing(load_args)) params$load_args <- load_args
  38. res <- httr::GET(splash_url(splash_obj), path="execute", encode="json", query=params)
  39. httr::stop_for_status(res)
  40. out <- httr::content(res, as="raw")
  41. out
  42. }