From 1ed092a77bbaed0592605d7d46759ec6391e820f Mon Sep 17 00:00:00 2001 From: Bob Rudis Date: Fri, 10 Feb 2017 15:56:15 -0500 Subject: [PATCH] execute examples and splash_local --- DESCRIPTION | 3 ++- NAMESPACE | 1 + NEWS.md | 1 + R/execute.r | 16 ++++++++++++++++ R/splashr.r | 4 ++++ man/execute_lua.Rd | 18 ++++++++++++++++++ man/splash.Rd | 6 ++++++ 7 files changed, 48 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 4d3470e..5c5200f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,7 +19,8 @@ URL: http://github.com/hrbrmstr/splashr BugReports: https://github.com/hrbrmstr/splashr/issues License: AGPL Suggests: - testthat + testthat, + tibble Depends: R (>= 3.2.0) Imports: diff --git a/NAMESPACE b/NAMESPACE index c6f7ab1..1f6f1f2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,7 @@ export(render_png) export(splash) export(splash_active) export(splash_debug) +export(splash_local) export(start_splash) export(stop_splash) export(writeHAR) diff --git a/NEWS.md b/NEWS.md index 9ba16f6..fddf97e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,7 @@ * added `execute`() * modified `splash_active`() +* added `splash_local` global variable to avoid typing `splash("localhost")` 0.1.0 diff --git a/R/execute.r b/R/execute.r index 0ac7951..758451b 100644 --- a/R/execute.r +++ b/R/execute.r @@ -11,6 +11,22 @@ #' @param load_args Parameter values to load from cache #' @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. #' @export +#' @examples \dontrun{ +#' splash_local %>% +#' execute_lua(' +#' function main(splash) +#' splash:go("https://projects.fivethirtyeight.com/congress-trump-score/") +#' splash:wait(0.5) +#' return splash:evaljs("memberScores") +#' end +#' ') -> res +#' +#' rawToChar(res) %>% +#' jsonlite::fromJSON(flatten=TRUE) %>% +#' purrr::map(tibble::as_tibble) -> member_scores +#' +#' member_scores +#' } execute_lua <- function(splash_obj, lua_source, timeout=30, allowed_domains, proxy, filters, save_args, load_args) { diff --git a/R/splashr.r b/R/splashr.r index 1a1a2c5..055df64 100644 --- a/R/splashr.r +++ b/R/splashr.r @@ -9,6 +9,10 @@ splash <- function(host, port=8050L) { list(host=host, port=port) } +#' @rdname splash +#' @export +splash_local <- splash("localhost") + s_GET <- purrr::safely(GET) #' Test if a Splash server is up diff --git a/man/execute_lua.Rd b/man/execute_lua.Rd index 40489f9..30a9f3c 100644 --- a/man/execute_lua.Rd +++ b/man/execute_lua.Rd @@ -30,3 +30,21 @@ execute_lua(splash_obj, lua_source, timeout = 30, allowed_domains, proxy, \description{ Execute a custom rendering script and return a result. } +\examples{ +\dontrun{ +splash_local \%>\% + execute_lua(' +function main(splash) + splash:go("https://projects.fivethirtyeight.com/congress-trump-score/") + splash:wait(0.5) + return splash:evaljs("memberScores") +end +') -> res + +rawToChar(res) \%>\% + jsonlite::fromJSON(flatten=TRUE) \%>\% + purrr::map(tibble::as_tibble) -> member_scores + +member_scores +} +} diff --git a/man/splash.Rd b/man/splash.Rd index 6940d0c..5a2d235 100644 --- a/man/splash.Rd +++ b/man/splash.Rd @@ -1,10 +1,15 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/splashr.r +\docType{data} \name{splash} \alias{splash} +\alias{splash_local} \title{Configure parameters for connecting to a Splash server} +\format{An object of class \code{list} of length 2.} \usage{ splash(host, port = 8050L) + +splash_local } \arguments{ \item{host}{host or IP address} @@ -14,3 +19,4 @@ splash(host, port = 8050L) \description{ Configure parameters for connecting to a Splash server } +\keyword{datasets}