Skip to content

Commit

Permalink
feat: add a warning for direct rhino::app() calls
Browse files Browse the repository at this point in the history
The encouraged way to run a Rhino app is shiny::runApp(). Using runApp
allows for shiny.autoreload (and possibly other features) to work.
  • Loading branch information
TymekDev committed Jan 5, 2024
1 parent 7c7793a commit d0e86b0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
24 changes: 22 additions & 2 deletions R/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ with_head_tags <- function(ui) {
#' Rhino application
#'
#' The entrypoint for a Rhino application.
#' Your `app.R` should contain nothing but a call to `rhino::app()`.
#' Your `app.R` should contain nothing but a call to `rhino::app()`,
#' and the app should be started with `shiny::runApp()`.
#'
#' This function is a wrapper around `shiny::shinyApp()`.
#' It reads `rhino.yml` and performs some configuration steps (logger, static files, box modules).
Expand All @@ -87,6 +88,10 @@ with_head_tags <- function(ui) {
#' It should export two functions which take a single `id` argument -
#' the `ui` and `server` of your top-level Shiny module.
#'
#' Using `rhino::app()` directly is discouraged.
#' Being a wrapper around `shiny::shinyApp()`,
#' it does not allow features such as shiny.autoreload to work.
#'
#' # Legacy entrypoint
#'
#' It is possible to specify a different way to load your application
Expand All @@ -110,6 +115,9 @@ with_head_tags <- function(ui) {
#' into a [Shiny module](https://shiny.rstudio.com/articles/modules.html)
#' (functions taking a single `id` argument).
#'
#' @param warn_on_direct_call `TRUE` by default. Set to `FALSE` to squelch warning when calling
#' `rhino::app()` directly.
#'
#' @return An object representing the app (can be passed to `shiny::runApp()`).
#'
#' @examples
Expand All @@ -118,7 +126,19 @@ with_head_tags <- function(ui) {
#' rhino::app()
#' }
#' @export
app <- function() {
app <- function(warn_on_direct_call = TRUE) {
is_direct_call <- identical(parent.frame(), .GlobalEnv)
if (isTRUE(warn_on_direct_call) && isTRUE(is_direct_call)) {
cli::cli_warn(
c(
"!" = paste(
"{.code rhino::app()} shouldn't be used directly.",
"Please use {.code shiny::runApp()} instead."
),
"i" = "To squelch this warning set {.code warn_on_direct_call = TRUE}."
)
)
}
setup_box_path()
box::purge_cache()
configure_logger()
Expand Down
13 changes: 11 additions & 2 deletions man/app.Rd

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

0 comments on commit d0e86b0

Please sign in to comment.