Skip to content

Commit

Permalink
add chromote_sleep_seconds arg
Browse files Browse the repository at this point in the history
  • Loading branch information
tdhock committed Jan 24, 2025
1 parent 299ff5e commit ac5c123
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
12 changes: 6 additions & 6 deletions R/z_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#' @param required_opts Character vector of plot.list element names
#' which are checked (stop with an error if not present). Use
#' required_opts=NULL to skip check.
#' @param chromote_sleep_seconds if numeric, chromote will be used to take a screenshot of the data viz, pausing this number of seconds to wait for rendering (experimental).
#' @param ... Additional options passed onto \code{animint2dir}.
#'
#' @return The function returns the initialized GitHub repository object.
Expand All @@ -36,7 +37,7 @@
#' }
#'
#' @export
animint2pages <- function(plot.list, github_repo, owner=NULL, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title","source"), ...) {
animint2pages <- function(plot.list, github_repo, owner=NULL, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title","source"), chromote_sleep_seconds=NULL, ...) {
for(opt in required_opts){
if(!opt %in% names(plot.list)){
stop(sprintf("plot.list does not contain option named %s, which is required by animint2pages", opt))
Expand All @@ -48,20 +49,19 @@ animint2pages <- function(plot.list, github_repo, owner=NULL, commit_message = "
stop(sprintf("Please run `install.packages('%s')` before using this function", pkg))
}
}

if(requireNamespace("chromote") && requireNamespace("magick")) {
res <- animint2dir(plot.list, open.browser = FALSE, ...)
if(requireNamespace("chromote") && requireNamespace("magick") && is.numeric(chromote_sleep_seconds)) {
chrome.session <- chromote::ChromoteSession$new()
res <- animint2dir(plot.list, open.browser = FALSE, ...)
#Find available port and start server
portNum <- servr::random_port()
normDir <- normalizePath(res$out.dir, winslash = "/", mustWork = TRUE)
start_servr(serverDirectory = normDir, port = portNum, tmpPath = normDir)
Sys.sleep(3)
Sys.sleep(chromote_sleep_seconds)
url <- sprintf("http://localhost:%d", portNum)
chrome.session$Page$navigate(url)
screenshot_path <- file.path(res$out.dir, "Capture.PNG")
screenshot_full <- file.path(res$out.dir, "Capture_full.PNG")
Sys.sleep(3)
Sys.sleep(chromote_sleep_seconds)
## Capture screenshot
chrome.session$screenshot(screenshot_full, selector = ".plot_content")
image_raw <- magick::image_read(screenshot_full)
Expand Down
8 changes: 8 additions & 0 deletions inst/examples/WorldBank-facets-map.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ wb.facets <- animint(
geom_line(aes(
year, life.expectancy, group=country, colour=region),
clickSelects="country",
help="Time series of life expectancy, one line per country",
data=TS(not.na),
size=4,
alpha=1,
Expand All @@ -93,13 +94,15 @@ wb.facets <- animint(
year, life.expectancy, colour=region, label=country),
showSelected="country",
clickSelects="country",
help="Names of selected countries",
data=TS(min.years),
hjust=1)+
## TS2
make_widerect(not.na, "year", data.fun=TS2)+
geom_path(aes(
fertility.rate, year, group=country, colour=region),
clickSelects="country",
help="Time series of fertility rate, one line per country",
data=TS2(not.na),
size=4,
alpha=1,
Expand All @@ -110,6 +113,7 @@ wb.facets <- animint(
key=country), # key aesthetic for smooth transitions!
clickSelects="country",
showSelected="year",
help="Scatter plot for the selected year, one point per country",
alpha=1,
alpha_off=0.3,
chunk_vars=character(),
Expand All @@ -119,6 +123,7 @@ wb.facets <- animint(
key=country), #also use key here!
showSelected=c("country", "year", "region"),
clickSelects="country",
help="Names of selected countries",
chunk_vars=character(),
data=SCATTER(not.na))+
scale_size_animint(breaks=10^(9:5))+
Expand All @@ -127,10 +132,12 @@ wb.facets <- animint(
5, 85, label=paste0("year = ", year),
key=1),
showSelected="year",
title="Selected year",
data=SCATTER(years))+
## MAP
geom_polygon(aes(
x, y, group=group, fill=region),
title="World map",
clickSelects="country",
color="black",
color_off="transparent",
Expand All @@ -143,6 +150,7 @@ wb.facets <- animint(
selector.types=list(country="multiple"),
source="https://github.com/animint/animint2/blob/master/inst/examples/WorldBank-facets-map.R",
out.dir="WorldBank-facets-map",
video="https://vimeo.com/1050117030",
title="World Bank data (multiple selection, facets)")
options(browser="firefox")
wb.facets
Expand Down
3 changes: 3 additions & 0 deletions man/animint2pages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/checkExtraParams.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac5c123

Please sign in to comment.