rstudio / webshot2

Take screenshots of web pages from R

Home Page:https://rstudio.github.io/webshot2/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rmdshot doesn't work when i add a params argument

egehankinik opened this issue · comments

When I use the rmdshot function on a shiny runtime rmd file, it works fine. However, when I use it with the added rmd._arg = list(params = list(n=1)), I get the error "Warning: Error in wait_until_server_exists: argument "p" is missing, with no default". In the package the wait_until_server_exists function is defined with two expected parameters, url and p however the rmdshot function only provides the URL parameter to the function which, I assume, is the reason for the error, but then why does it work without the parameters?

I tried editing the function to include p in the parameters but this time I got an error saying the params = list(1) is unused

i edited the rmdshot function to be:
function (doc, file = "webshot.png", ..., delay = NULL, rmd_args = list(),
port = getOption("shiny.port"), envvars = NULL)
{
runtime <- rmarkdown::yaml_front_matter(doc)$runtime
rmdshot_shiny1 = function(doc, file, ..., rmd_args, port,
envvars) {
port <- available_port(port)
url <- shiny_url(port)
p <- r_background_process(function(...) {
rmarkdown::run(...)
}, args = list(file = doc, shiny_args = list(port = port),
render_args = rmd_args), envvars = envvars)
on.exit({
p$kill()
})
wait_until_server_exists(url, p)
fileout <- webshot(url, file = file, ...)
invisible(fileout)
}
if (is_shiny(runtime)) {
if (is.null(delay))
delay <- 3
rmdshot_shiny1(doc, file, ..., delay = delay, rmd_args = rmd_args,
port = port, envvars = envvars)
}
else {
if (is.null(delay))
delay <- 0.2
outfile <- tempfile("webshot", fileext = ".html")
do.call(rmarkdown::render, c(list(doc, output_file = outfile),
rmd_args))
webshot(file_url(outfile), file = file, ...)
}
}
and it seems to work now

Thanks for the issue report @egehankinik! I can reproduce the error with the following issue-52.Rmd document.

---
title: "Issue 52"
output: html_document
runtime: shiny

params:
  n_breaks: 35
---

Here's an R Markdown document with `runtime: shiny` and `params`.
The value of `params$n_breaks` is `r params$n_breaks`.

Trying to screenshot the document causes this error:

webshot2::rmdshot(
  "issue-52.Rmd",
  file = "issue-52.png",
  rmd_args = list(params = list(n_breaks = 20))
)
#' Error:
#' ! argument "p" is missing, with no default
#' ---
#' Backtrace:
#'     ▆
#'  1. └─webshot2::rmdshot("issue-52.Rmd", file = "issue-52.png", rmd_args = list(params = list(n_breaks = 20)))
#'  2.   └─webshot2:::rmdshot_shiny(...)
#'  3.     └─webshot2:::wait_until_server_exists(url)

I also came to a similar conclusion as you as to the changes needed to fix this. I'll wrap those up in a PR shortly.