-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Validate path as shiny app #4091
Comments
If this is for a third-party tool to help run Shiny apps, I'd recommend either requiring your users to follow a naming convention established by your tool or to ask users to explicitly name the primary file containing the Shiny app. Unfortunately, when |
Understood. Do you have a suggestion on how to try validating a shiny app via |
You could maybe start the app in a background R session with callr? You could also source the app script and inspect the return value. According to the If you can describe your use case a little more, I might be able to provide some more targeted advice. 😄 |
The goal is to prepare a shiny app for deployment. The directory is zipped, copied, and moved else where. Then from that location However before all of that stuff is done, my goal is to verify that the app is indeed a certified runnable shiny app! |
{callr} is a good helper. thank you! I think this is sufficient and quite handy. Though I'm sure i'm missing something with the error and listening on check. iis_shiny_app <- function(app_dir, max_check_time = 10L, poll_duration = 5000L) {
# store when check first starts
start_check_time <- Sys.time()
# helper function to start shiny app on random port
run_app <- function(app_dir) shiny::runApp(app_dir, port = httpuv::randomPort())
# start app in background process
rp <- callr::r_bg(run_app, list(app_dir = app_dir))
# set polling duration (5 sec default)
.poll_res <- rp$poll_io(poll_duration) # set polling duration
while (TRUE) {
# read a line from the output
line <- rp$read_error_lines()
# if there is output check
if (length(line) > 0) {
error_detected <- any(grepl("Error in shinyAppDir", line))
app_start_detected <- any(grepl("Listening on", line))
if (error_detected || app_start_detected) {
break
}
}
# check the time diff
check_length <- difftime(
Sys.time(),
start_check_time,
units = "secs"
)
# if ther time check is exceeded, error out
if (check_length > max_check_time) {
cli::cli_abort(
c(
"Cannot determine if valid shiny app",
i = "Waited {max_check_time} seconds."
)
)
}
}
# kill the process
.killed_process <- rp$kill()
# return the check
!error_detected
} |
Why don't you use It will throw an error if the directory doesn't contain a vaild shiny app, which you could
|
@ismirsehregal because in the case of success the function will run indefinitely |
@JosiahParry: No it won't, as Please see
Example:
|
@ismirsehregal thank you for this hint! This works much better: is_shiny_app <- function(path) {
tryCatch(
!is.null(shiny::shinyAppDir(path)),
error = function(cond) FALSE
)
} |
Thanks @ismirsehregal and @JosiahParry, that's an elegant solution! |
I feel that this has been resolved as a one off matter but the shiny package would benefit from having an official way to do this. I’d respectfully request that this issue be reopened as a feature request. Thanks! |
The
shiny::runApp()
function is very flexible and will run an app from a path e.g.app.R
www/index.html
or from a directory that contains aui.R
andserver.R
function.It would be great to have a function to validate that a path is indeed a shiny app without having to run
runApp()
and catch if an error occurs.The text was updated successfully, but these errors were encountered: