Skip to content
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

Make Shiny Autoreload Work #528

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rhino
Title: A Framework for Enterprise Shiny Applications
Version: 1.5.0.9003
Version: 1.5.0.9004
Authors@R:
c(
person("Kamil", "Żyła", role = c("aut", "cre"), email = "[email protected]"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* `lint_sass()` now uses `stylelint` 14.16 (the last major version supporting stylistic rules)
* Upgrade all remaining Node.js dependencies to latest versions and fix vulnerabilities.
* The minimum supported Node.js version is now 16.
4. `shiny.autoreload` now works correctly with `box::use`. Note: this requires
`legacy_entrypoint` to be unset.

# [rhino 1.5.0](https://github.com/Appsilon/rhino/releases/tag/v1.5.0)

Expand Down
22 changes: 16 additions & 6 deletions R/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,25 @@ load_main_module <- function() {
app <- NULL
main <- NULL
box::use(app/main)
main
main_env <- environment()
if (isTRUE(getOption("shiny.autoreload", FALSE))) {
shiny:::autoReloadCallbacks$register(function() {
evalq(box::reload(main), main_env)
})
}
main_env
}

as_top_level <- function(shiny_module) {
as_top_level <- function(shiny_module_env) {
# Necessary to avoid infinite recursion / bugs due to lazy evaluation:
# https://adv-r.hadley.nz/function-factories.html?q=force#forcing-evaluation
force(shiny_module)
force(shiny_module_env)

# The actual function must be sourced with `keep.source = TRUE` for reloading to work:
# https://github.com/Appsilon/rhino/issues/157
wrap <- source(fs::path_package("rhino", "as_top_level.R"), keep.source = TRUE)$value

wrap(shiny_module)
wrap(shiny_module_env)
}

with_head_tags <- function(ui) {
Expand Down Expand Up @@ -110,6 +116,8 @@ with_head_tags <- function(ui) {
#' into a [Shiny module](https://shiny.rstudio.com/articles/modules.html)
#' (functions taking a single `id` argument).
#'
#' Note: `legacy_entrypoint` might not work in conjunction with `shiny.autoreload`.
#'
#' @return An object representing the app (can be passed to `shiny::runApp()`).
#'
#' @examples
Expand All @@ -133,9 +141,11 @@ app <- function() {
main <- new.env()
source(fs::path("app", "main.R"), local = main)
} else {
main <- load_main_module()
main_env <- load_main_module()
if (!identical(entrypoint, "box_top_level")) {
main <- as_top_level(main)
main <- as_top_level(main_env)
} else {
main <- main_env$main
}
}
shiny::shinyApp(
Expand Down
6 changes: 3 additions & 3 deletions inst/as_top_level.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This function is defined in the `inst` directory, as it must be sourced with `keep.source = TRUE`
# for reloading to work: https://github.com/Appsilon/rhino/issues/157
function(shiny_module) {
function(shiny_module_env) {
list(
# Wrap the UI in a function to support Shiny bookmarking.
ui = function(request) shiny_module$ui("app"),
ui = function(request) shiny_module_env$main$ui("app"),
# The curly braces below are essential: https://github.com/Appsilon/rhino/issues/157
server = function(input, output) {
shiny_module$server("app")
shiny_module_env$main$server("app")
}
)
}
2 changes: 2 additions & 0 deletions man/app.Rd

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

Loading