-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add basic appshot.shiny.appobj
functionality
#55
Conversation
Could not use library(shiny)
ui <- fluidPage(
plotOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlot({
plot(cars)
})
}
obj <- shinyApp(ui, server)
run_and_snapshot <- function(shinyAppObj) {
initialized <- FALSE
observe({
if (!initialized) {
invalidateLater(5000)
initialized <<- TRUE
return()
}
# Do whatever
message("Take snapshot")
shiny::stopApp()
})
print(obj)
message("Got here")
} |
@schloerke Can you elaborate on that? Calling system2 didn't ever return if you called it from where |
Updated to use |
R/appshot.R
Outdated
if (r_session$is_alive()) { | ||
# try again later | ||
shiny::invalidateLater(200) | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would also be good to have a timeout check, just in case the R process with webshot hangs for some reason.
R/appshot.R
Outdated
}, | ||
args | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add on.exit(r_session$kill())
to make sure that it goes away? Although now that I think of it, that won't ensure that phantomjs process will exit -- in fact, it's probably the reverse, since if the child R process receives the hard kill signal, it won't be able to send any signals to the phantomjs process. Anyway, something should happen to ensure the child R process and grandchild phantomjs process exit.
Now that I think of it again, maybe killing the R process will cause the phantomjs process to exit. But the behavior might also be different on Windows.
R/appshot.R
Outdated
r_session <- callr::r_bg( | ||
function(...) { | ||
# Wait for app to start | ||
Sys.sleep(0.5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This delay should be a parameter. Same for appshot.character
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is fair for us to keep it internal. It is a delay for webshot to start processing. The delay in taking the picture could be increased by the 'delay' parameter. By keeping it static at 0.5, it is a minimum of 0.5 seconds in processing before the webshot is taken.
R/appshot.R
Outdated
@@ -40,14 +42,73 @@ appshot.character <- function(app, file = "webshot.png", ..., | |||
p <- processx::process$new("R", args = c("--slave", "-e", cmd)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switch to callr::r_bg
. We might not even need the processx
package as a (direct) dependency.
R/appshot.R
Outdated
webshot_timeout <- webshot_timeout + 10 | ||
} | ||
webshot_timeout_ms <- webshot_timeout * 1000 | ||
count <- 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use times instead of counts.
R/appshot.R
Outdated
shiny::runApp(app, port = port, display.mode = "normal") | ||
|
||
# return webshot::webshot file value | ||
r_session$get_result() # safe to call as the r_session must have ended |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be invisible()
?
@wch Added
|
Adds an appshot method for shiny app objects.