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.

70 lines
1.6KB

  1. splash_url <- function(splash_obj) { sprintf("http://%s:%s", splash_obj$host, splash_obj$port) }
  2. #' Configure parameters for connecting to a Splash server
  3. #'
  4. #' @param host host or IP address
  5. #' @param port port the server is running on (default is 8050)
  6. #' @export
  7. splash <- function(host, port=8050L) {
  8. list(host=host, port=port)
  9. }
  10. #' @rdname splash
  11. #' @export
  12. splash_local <- splash("localhost")
  13. s_GET <- purrr::safely(GET)
  14. #' Test if a Splash server is up
  15. #'
  16. #' @md
  17. #' @param splash_obj A splash connection object
  18. #' @return `TRUE` if Slash server is running, otherwise `FALSE`
  19. #' @export
  20. splash_active <- function(splash_obj) {
  21. res <- s_GET(splash_url(splash_obj), path="_ping")
  22. if (is.null(res$result)) return(FALSE)
  23. if (httr::status_code(res$result) >=300) return(FALSE)
  24. httr::content(res$result, as="text", encoding="UTF-8") %>%
  25. jsonlite::fromJSON() -> out
  26. out$url <- splash_url(splash_obj)
  27. message(sprintf("Status of splash instance on [%s]: %s. Max RSS: %s\n", out$url, out$status, out$maxrss))
  28. if ("status" %in% names(out)) return(out$status == "ok")
  29. return(FALSE)
  30. }
  31. #' Retrieve debug-level info for a Splash server
  32. #'
  33. #' @param splash_obj A splash connection object
  34. #' @export
  35. splash_debug <- function(splash_obj) {
  36. httr::GET(splash_url(splash_obj), path="_debug") %>%
  37. httr::stop_for_status() %>%
  38. httr::content(as="text", encoding="UTF-8") %>%
  39. jsonlite::fromJSON() -> out
  40. out$url <- splash_url(splash_obj)
  41. class(out) <- c("splash_debug", class(out))
  42. out
  43. }
  44. #' @rdname splash_debug
  45. #' @keywords internal
  46. #' @export
  47. print.splash_debug <- function(x, ...) {
  48. print(str(x))
  49. invisible(x)
  50. }