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

autoreload doesn't load with Shiny modules #2711

Open
mik3y64 opened this issue Nov 26, 2019 · 11 comments · May be fixed by #4184
Open

autoreload doesn't load with Shiny modules #2711

mik3y64 opened this issue Nov 26, 2019 · 11 comments · May be fixed by #4184

Comments

@mik3y64
Copy link

mik3y64 commented Nov 26, 2019

My R/ dir have three files

  • R/app.R
  • R/shiny_run.R
  • R/my_module.R

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 selectInput() does the change the label.

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 selectInput() does refresh the Shiny application but "changeLabel" does not change at all.

@Toniiiio
Copy link

Toniiiio commented Dec 19, 2019

Maybe, a small hint how one can further specify the issue. As far as i see, the issue is rather wth source(...).

Note, if you put the modules from my_module.R to app.R and changes the modules in there, you get the desired updates at run time.

However, if you put a random, (non-moduled) ui component in "my_module.r"source it and change it at run time, it wont get updated.

It gets even more interesting.

continually monitored for changes to files that have the extensions: r, htm, html, js, css, png, jpg, jpeg, gif. If any changes are detected, all connected Shiny sessions are reloaded.

  • And in fact: If you look closely, the site is refreshed, the ui change just does not take place.

  • However, if you change something within app.R after an edit in the sourced file, both edits can be seen.

tldr; Long text without a clear solution. But it might be of help for further debugging i hope.

@erikriverson
Copy link

@TiMaG , did you ever resolve this? In my shiny app, I am source()-ing a utils.r file from my app.r script. I see the UI refresh in the browser upon saving utils.r, but the changes are not loaded in until app.r is modified and saved. I believe this is the exact issue you describe here.

@Toniiiio
Copy link

@erikriverson sry, no.

@itkonen
Copy link

itkonen commented Sep 25, 2020

I had the same issue, but as as a workaround I included my source functions also inside the main app's server function. This will reload the module files everytime the autoreload is triggered. You need to set local = TRUE, otherwise the modules will end up in the global environment, where server function doesn't seem to find them. These extra source functions can be later removed from the production version of the app.

server <- function(input, output, session) {
  source("my_module.R", local = TRUE)
  ## app code...
}

@nlarusstone
Copy link

I'm also encountering this issue. I've set some debug points to ensure that initAutoReloadMonitor is picking up my files, and it is.

@GitHunter0
Copy link

I had the same issue, but as as a workaround I included my source functions also inside the main app's server function. This will reload the module files everytime the autoreload is triggered. You need to set local = TRUE, otherwise the modules will end up in the global environment, where server function doesn't seem to find them. These extra source functions can be later removed from the production version of the app.

server <- function(input, output, session) {
  source("my_module.R", local = TRUE)
  ## app code...
}

Thanks @itkonen , it is a workaround, but a incredibly useful one.

@epruesse
Copy link

I've got this for now, in the main server function:

    server_env = environment()
    lapply(
        list.files(path = "R", pattern = ".*\\.R$", full.names = TRUE),
        source,
        local = server_env
    )

The catch is that this does not lead to the ui parts being updated. Ultimately, I still always have to touch the app.R to get a full reload.

@asadow
Copy link

asadow commented Mar 5, 2023

It would be great to have this work out-of-the-box here, as it also would then work within a golem or Rhino project.

@taimur38
Copy link

Bumping this, really becomes an issue when you are building larger apps

@adamsimonini
Copy link

Same issue. It looks like there is no resolution coming

@ShixiangWang
Copy link

Any progress?

@gadenbuie gadenbuie linked a pull request Feb 3, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.