-
Notifications
You must be signed in to change notification settings - Fork 133
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
shiny.autoreload in {golem} #263
Comments
Ok so as far as I can tell it's because this behavior is triggered by For example, if I create a new golem, delete the R/ folder, create a shiny app in a folder called R and do : > options(shiny.autoreload = TRUE)
> runApp(appDir = 'R') The option works as expected. And : > trace("runApp", where = asNamespace("shiny"))
> trace("initAutoReloadMonitor", where = asNamespace("shiny"))
Tracing function "initAutoReloadMonitor" in package
"namespace:shiny"
[1] "initAutoReloadMonitor"
> runApp(appDir = 'R')
trace: runApp(appDir = "R")
Tracing initAutoReloadMonitor(appDir) on entry So runApp on directory works. Then back in a golem app: > options(shiny.autoreload = TRUE)
> trace("runApp", where = asNamespace("shiny"))
> trace("initAutoReloadMonitor", where = asNamespace("shiny")) Then launch the run_dev, > # Run the application
> lpppa::run_app()
Tracing runApp(x) on entry
Loading required package: shiny
Listening on http://127.0.0.1:5164 I have the trace of Not sure how to handle that so far 🤔 |
I am having this issue and it's slowing down my development workflow. I did some test too (by intentionally not using golem to find out the root cause) and I suspect it's the issue with Shiny modules? Golem app uses modules and it gets affected too? Filed similar issues at shiny but without any help. autoreload doesn't load with Shiny modules #2711 In my package (it's golem but doesn't matter because I am running the app without any golem configurations), R/ dir have three files
autoreload works without module # app.R
library(shiny)
ui <- fluidPage(
selectInput("sample_input", "changeLabel", choices = letters[1:3])
)
server <- function(input, output, session) {
NULL
}
shinyApp(ui = ui, server = server) # shiny_run.R
options(shiny.autoreload = TRUE)
shiny::runApp() Editing the "changeLabel" in autoreload doesn't work with module # my_module.R
moduleUI <- function(id) {
ns <- NS(id)
selectInput(ns("sample_input"), "changeLabel", choices = letters[1:3])
}
moduleServer <- function(input, output, session) {
NULL
} # app.R
library(shiny)
source("my_module.R")
ui <- fluidPage(
moduleUI('test')
)
server <- function(input, output, session) {
callModule(moduleServer, 'test')
}
shinyApp(ui = ui, server = server) # shiny_run.R
options(shiny.autoreload = TRUE)
shiny::runApp() Editing the "changeLabel" in |
I'm just getting started with this, but I've been using a relatively hacky solution for this issue. Not sure if it's helpful to others, but I'll throw it here in case: So, each time you change bits of your shiny application, you're supposed to run Instead of getting auto.reload to work, I just use bash to watch for changes in the Step 1: Set the portI set that in
I just use port 11616 for fun, since it spells "app" (A=1, P=16). Step 2: Create
|
My solution to this issue makes use of a very small Javascript script, which automatically reloads the Shiny app (which is in fact a web page) after a desired length of time. I realize that many Shiny users are not comfortable with using Javascript and I, myself, am learning as of the time of writing this. So, I will write my solution in the form of a detailed step by step guide: 1- Create a Javascript fileThe 2- Add the JavaScript codeCopy the code below, paste it in
Do not forget to change the reload interval, which is 1000 milliseconds above (i.e. one second), if you want to. 3- Run your applicationRun your application as usual by running the code in WarningKeep in mind that this hack is mostly useful for building your UI and that you will not be able to efficiently test your app while the script is running because it will automatically reload. For this reason In order to stop the script from running, you can just comment it out by using
Source: this solution was adapted from this Stackoverflow thread. |
Hello everyone! I have also came to this problem and it's slowing down my productivity. The solution proposed by @julianstanley is quite similar to the Shiny's native autoreload, but I have actually realized that I like more something similar to the classic devtools shortcut
I don't like killing processes through filtering their PID, so here, PID is saved to file and later used for killing.
The app port is hardcoded here, so change it according to your settings. Make sure you have curl installed, as it is used to check whether the app is running.
I don't understand how shrtcts is parsing the shortcut command, but if you directly use in shortcut definition what's in the
I think this sort of shortcut could be added to golem's RStudio addin. |
Ok folks, so to sum up the issue is that the autoreload only works when calling Here is the workaround to make this work:
Enjoy 🎉 |
Thanks for the follow-up, @ColinFay . When my colleague and I originally encountered this issue, we came to the conclusion that our development workflow must have been fundamentally different from yours. At the time, we would make amendments to our business logic code, and then re-start our app to confirm that everything worked as intended. This involved a lot of waiting for the app to re-load, etc. The conclusion that we came to is that you probably follow more of a test-driven-development workflow, where you run smaller pieces of your business logic without the need to start-up the entire app. Working this way means that the auto-reload feature is generally less necessary, as you only really need to start -up the app in development when testing UI features. Chapter 10 of your book gets into TDD somewhat, but it could also be interesting to see a screencast that addresses this topic of "efficient development workflow". |
Hmm, I just tried this and it didn't work for me. My workflow was:
Attaching my session info below |
I followed the same steps and it also doesn't work for me. |
@dwhdai this refreshes only if you save the file inside R (not the browser), and also, there is an issue with I feel like this should be implemented at For example in |
EDIT: Unintentionally wrote this in French at first ... Hi @ColinFay, The aformentionned solution works (I merely added However, while the page refreshes, the UI changes do not appear. Unless I make a change to app.R, in which case the app in correctly refreshed and changes to modules taken into account. Of course, right after I wrote this I found the same kind of comment in the {shiny} repo ... In any case, while I do not have a solution to contribute, knowing how to do this is good enough to make it work, I just add/remove a character in app.R and I can dev the app while watching my changes So, thank you for writing a solution, and for what you guys do with {golem} :-) |
Note to self : here is a prototype of implementing an
|
Don't forget to put the stderr and stdout in a file. logs <- tempfile()
proc <- processx::process$new(
stderr = logs,
stdout = logs,
"Rscript", c(
"-e",
"golem::run_dev()"
)
)
Sys.sleep(5)
readLines(logs)
proc$kill() The same terminal as R would be the best : proc <- processx::process$new(
stderr = "",
stdout = "",
"Rscript", c(
"-e",
"golem::run_dev()"
)
)
Sys.sleep(5)
proc$kill() |
Can't wait for version 0.4.0 with this autoreload feature, it will make the app development process way quicker and a lot more pleasurable |
Hi, It is implemented in the
And inside your golem application |
Ive tested the solutions with a new golem project to nullify any of my own tinkering. Im using Windows 19043.1288, R 4.1.1 and shiny 1.7.1. |
Thank you, @Cervangirard. I'm having a strange issue, when I run
It is like the parameter I removed and reinstalled Golem several times using sessionInfo()
#> R version 4.1.1 (2021-08-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19043)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=English_United States.1252
#> [2] LC_CTYPE=English_United States.1252
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C
#> [5] LC_TIME=English_United States.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> loaded via a namespace (and not attached):
#> [1] digest_0.6.28 withr_2.4.2 magrittr_2.0.1 reprex_2.0.1
#> [5] evaluate_0.14 highr_0.9 stringi_1.7.5 rlang_0.4.11
#> [9] cli_3.0.1 rstudioapi_0.13 fs_1.5.0 rmarkdown_2.10
#> [13] tools_4.1.1 stringr_1.4.0 glue_1.4.2 xfun_0.27
#> [17] yaml_2.2.1 fastmap_1.1.0 compiler_4.1.1 htmltools_0.5.2
#> [21] knitr_1.36
**Golem 0.3.1.9002** Created on 2021-10-28 by the reprex package (v2.0.1) |
Hello, This is most likely becquse you wrote "autoreaload" instead of "autoreload" Best, Raphael |
Haha simplest solution ever (I had copied and pasted, then the typo happened). Thank you, @rsimonmd ! However, the command is not working for me either, I'm having the same issue as @vreederene-90 :
|
@Cervangirard , I believe there is a misspelling in the name of the branch |
Hello @ColinFay, Many thanks for all the work you are doing on Golem, great package, I am fan. Regarding this autoreload bug in golem, has it been addressed? Thanks!! |
The new
Moreover, autoreload always opens in a new external browser tab on reload. This is different from the normal Shiny autoreload which will refresh the preexisting browser tab. This feature would definitely be nice to have with some refinement. |
Ive written a small package that includes two workarounds to this problem: autoreload.fix. |
Hello @vreederene-90, Thanks for the effort. I am trying your package. So basically I have my golem project, I added the I also tried to run the command Anything I might be doing wrong? |
Please verify you have written |
@vreederene-90 I tried the following, with no success unfortunately.
What happens is that the application doesn't load. The browser opens a tab with 127.0.0.1:3070 but a white empty page is shown. Any ideas ? Thanks for your help |
We spent some time with @vreederene-90 to check his solution autoreload.fix (thanks again Rene) and it works pretty well ! |
Hi everyone, not sure if this is still relevant , but I encountered the same problem yesterday and I found the following fix to work as well: patch the function cached_func_with_file <- function(dir,
file,
func,
case.sensitive = FALSE) {
dir <- normalizePath(dir, mustWork = TRUE)
value <- NULL
filePattern <- getOption(
"shiny.autoreload.pattern",
".*\\.(r|html?|js|css|png|jpe?g|gif)$"
)
last_mtimes <- NULL
function(...) {
file.path.func <- if (case.sensitive) file.path else file.path.ci
fname <- file.path.func(dir, file)
files <- list.files(dir, filePattern, recursive = TRUE, ignore.case = TRUE)
files <- sort_c(files)
mtimes <- file.info(files)$mtime
names(mtimes) <- files
if (!identical(last_mtimes, mtimes)) {
value <<- func(fname, ...)
last_mtimes <<- mtimes
}
value
}
}
shiny_env <- environment(shiny:::cachedFuncWithFile)
unlockBinding("cachedFuncWithFile", shiny_env)
body(shiny_env$cachedFuncWithFile) <- body(cached_func_with_file)
lockBinding("cachedFuncWithFile", shiny_env)
options(shiny.autoreload = TRUE) After executing this code you can use Here you can see the the relevant code part of |
@toscm that didn't work for me, unfortunately. I ran your code in the Rstudio console then launched the app using It's definitely still a relevant issue though.. makes it hard to use golem on a day-to-day basis. |
Hello all I've tried some of the proposed solutions, unfortunately they don't work for me. Is there something else we could try in the meantime? Cheers |
I’ve had something that I’ve been using locally for a while that sort of works for my use case. I’ve packaged this up into a package and would welcome some comments! The way I’m using this currently is to use this:
It launches the golem app in the background using callr, but one thing it doesn’t do is automatically reconnect the shiny app, but you can do that with I also explicitly choose a port to run the shiny app on, this method doesn’t automatically fire up a web browser for me, so it’s easier for me to pick a port and stick with it: |
Hello @tomjemmett I downloaded {watcher} and I am running the In my main console, i am launching my Shiny app with Is that the correct way of using your package? Many thanks, |
you wouldn't need to launch the app with run_dev as the job will be running it for you. It won't automatically load a web browser though, so you do have to call this manually. So I tend to configure my run_app <- function(onStart = NULL, # nolint
options = list(port = 8081),
enableBookmarking = NULL, # nolint
uiPattern = "/", # nolint
...) {
...
} |
As of right now,
options(shiny.autoreload = TRUE)
doesn't work with golemThe text was updated successfully, but these errors were encountered: