rstudio / bslib

Tools for theming Shiny and R Markdown via Bootstrap 3, 4, or 5.
https://rstudio.github.io/bslib/
Other
486 stars 58 forks source link

bs_theme(bootswatch) affects future shiny apps and cannot be overridden until the R session restarts #683

Closed daattali closed 1 year ago

daattali commented 1 year ago

Using both the CRAN and the latest github versions of {bslib}, first run the following app:

library(shiny)

ui <- fluidPage(
  theme = bslib::bs_theme(bootswatch = "cerulean"),
  actionButton("test", "test")
)

server <- function(input, output, session) {}

shinyApp(ui, server)

Kill the app, then run it again with a different theme:

library(shiny)

ui <- fluidPage(
  theme = bslib::bs_theme(bootswatch = "cosmo"),
  actionButton("test", "test")
)

server <- function(input, output, session) {}

shinyApp(ui, server)

The previous cerulean theme is still the one that's used. Only if I refresh the R session, I can get a different theme to show up.

Note that this is NOT the case if I use the deprecated {shinythemes}

gadenbuie commented 1 year ago

Thanks for reporting @daattali, but unfortunately I can't reproduce this. I can switch between "cerulean" and "cosmo" in the same R session and the app reflects the change.

Can you share a bit more information about your session and R packages? The output from sessioninfo::session_info() would be a good place to start.

gadenbuie commented 1 year ago

Oh! Actually, I can reproduce it. It's important that shiny::devmode(FALSE) is turned off (which also means you might be able to get around this locally by turning on shiny::devmode()).

For me, "cosmo" seems to be the sticky theme. Switching from "cerulean" to "cosmo" works fine, but after that the button is always "cosmo" styled.

That said, this could also be a result of browser caching. Certain browsers, especially those used for previews inside IDEs, are more aggressive about caching. In the above case where "cosmo" seemed to be sticky, the problem also goes away if I open the preview app in a new browser window outside of the IDE preview. So again, it would be helpful to have some more information about your system and where and how you're viewing these apps.

daattali commented 1 year ago

This happens to be in chrome and firefox, and both are "sticky" - whichever one I run first. If I first run "cerulean" and open it in Chrome, then I stop the app and change to "cosmo" and open it in a different browser I still don't see cosmo.

If you can't reproduce it to the same degree that I can, then you can close the issue. I think I'd rather stick with default theme and custom CSS because pre-set themes seem to have conflicts anyway :)

gadenbuie commented 1 year ago

This happens to be in chrome and firefox, and both are "sticky" - whichever one I run first.

This is interesting, especially the part about seeing the same theme in a different browser. Can you confirm that the behavior persists even if you go into the developer tools and disable caching (under the Network tab in either Chrome or Firefox) and then reload?

I'm interested in digging into this a little more and it would still be helpful to have a session info report so I can get closer to replicating your environment.

daattali commented 1 year ago

It doesn't seem to be happening deterministically. I think you're right that it's a caching issue, even when I saw it happening in another browser.

It looks like I can also get the button to be a mix between the two themes 😄

This is cerulean:

image

This is cosmo:

image

This is what I get sometimes when I try to switch:

image

I'm hapy to drop this since I don't actually use themes.

If you still want it:

> devtools::session_info()
─ Session info ────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23 ucrt)
 os       Windows 10 x64 (build 18363)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_Canada.utf8
 ctype    English_Canada.utf8
 tz       America/New_York
 date     2023-07-17
 rstudio  2022.07.2+576 Spotted Wakerobin (desktop)
 pandoc   NA

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version    date (UTC) lib source
 bslib         0.5.0      2023-06-09 [1] CRAN (R 4.2.3)
 cachem        1.0.6      2021-08-19 [1] CRAN (R 4.2.1)
 callr         3.7.3      2022-11-02 [1] CRAN (R 4.2.2)
 cli           3.4.1      2022-09-23 [1] CRAN (R 4.2.1)
 crayon        1.5.2      2022-09-29 [1] CRAN (R 4.2.1)
 devtools      2.4.5      2022-10-11 [1] CRAN (R 4.2.2)
 digest        0.6.32     2023-06-26 [1] CRAN (R 4.2.1)
 ellipsis      0.3.2      2021-04-29 [1] CRAN (R 4.2.1)
 fastmap       1.1.0      2021-01-25 [1] CRAN (R 4.2.1)
 fs            1.5.2      2021-12-08 [1] CRAN (R 4.2.1)
 glue          1.6.2      2022-02-24 [1] CRAN (R 4.2.1)
 htmltools     0.5.5      2023-03-23 [1] CRAN (R 4.2.3)
 htmlwidgets   1.6.2      2023-03-17 [1] CRAN (R 4.2.3)
 httpuv        1.6.6      2022-09-08 [1] CRAN (R 4.2.1)
 jquerylib     0.1.4      2021-04-26 [1] CRAN (R 4.2.1)
 jsonlite      1.8.5      2023-06-05 [1] CRAN (R 4.2.3)
 later         1.3.0      2021-08-18 [1] CRAN (R 4.2.1)
 lifecycle     1.0.3      2022-10-07 [1] CRAN (R 4.2.1)
 magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.2.1)
 memoise       2.0.1      2021-11-26 [1] CRAN (R 4.2.1)
 mime          0.12       2021-09-28 [1] CRAN (R 4.2.0)
 miniUI        0.1.1.1    2018-05-18 [1] CRAN (R 4.2.1)
 pkgbuild      1.4.0      2022-11-27 [1] CRAN (R 4.2.2)
 pkgload       1.3.2      2022-11-16 [1] CRAN (R 4.2.3)
 prettyunits   1.1.1      2020-01-24 [1] CRAN (R 4.2.1)
 processx      3.8.0      2022-10-26 [1] CRAN (R 4.2.2)
 profvis       0.3.7      2020-11-02 [1] CRAN (R 4.2.1)
 promises      1.2.0.1    2021-02-11 [1] CRAN (R 4.2.1)
 ps            1.7.2      2022-10-26 [1] CRAN (R 4.2.2)
 purrr         1.0.1      2023-01-10 [1] CRAN (R 4.2.3)
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.2.1)
 Rcpp          1.0.9      2022-07-08 [1] CRAN (R 4.2.1)
 remotes       2.4.2      2021-11-30 [1] CRAN (R 4.2.1)
 rlang         1.1.0.9000 2023-04-14 [1] Github (r-lib/rlang@ea2fe5f)
 rstudioapi    0.14       2022-08-22 [1] CRAN (R 4.2.1)
 sass          0.4.6      2023-05-03 [1] CRAN (R 4.2.3)
 sessioninfo   1.2.2      2021-12-06 [1] CRAN (R 4.2.1)
 shiny       * 1.7.4      2022-12-15 [1] CRAN (R 4.2.3)
 stringi       1.7.8      2022-07-11 [1] CRAN (R 4.2.1)
 stringr       1.5.0      2022-12-02 [1] CRAN (R 4.2.2)
 urlchecker    1.0.1      2021-11-30 [1] CRAN (R 4.2.1)
 usethis       2.1.6      2022-05-25 [1] CRAN (R 4.2.1)
 vctrs         0.6.2      2023-04-19 [1] CRAN (R 4.2.3)
 xtable        1.8-4      2019-04-21 [1] CRAN (R 4.2.1)

 [1] C:/Users/Dean-X1C/AppData/Local/R/win-library/4.2
 [2] C:/Program Files/R/R-4.2.1/library

───────────────────────────────────────────────────────────────────────────────────────────────────────────
gadenbuie commented 1 year ago

Thanks for sharing your session info @daattali. After a bit more testing, I'm pretty certain this is ultimately a caching issue.

For anyone else running into this problem, the immediate solution is to run shiny::devmode() to enable shiny's developer mode in your local R session. This ensures that the theme dependencies are completely re-rendered and there's much less chance that your preview browser will cache the results.

When I do see the incorrect styles being applied — e.g. if I start with "cerulean", switch to "cosmo", and then switch back to "cerulean" – the Network tab in my browser's developer tools (here Firefox) indicates that bootstrap.min.css was loaded from cache rather than a new request. This is the tell-tale sign that browser caching is at fault.

image

In that same tab, if I disable caching via the checkbox

image

and refresh the page, the theme assets are download anew rather than cached.

image

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you have found a related problem, please open a new issue (with a reproducible example or feature request) and link to this issue. :raising_hand: Need help? Connect with us on Discord or Posit Community.