rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.86k forks source link

`thematic_shiny` causes plot to fail in flexdashboard #3902

Open erikarasnick opened 1 year ago

erikarasnick commented 1 year ago

Describe the problem

When I specify a preset bootswatch theme in the yaml header, then use thematic::thematic_shiny() I get this error:

Error: attempt to apply non-function
---
title: "Demo 10 - thematic"
output: 
  flexdashboard::flex_dashboard:
    theme: 
      version: 5
      bootswatch: "quartz"
runtime: shiny
---

```{r global}
library(tidyverse)
library(flexdashboard)
ggplot2::theme_set(ggplot2::theme_bw())
thematic::thematic_shiny(bg = "auto", fg = "auto", font = "auto")
sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2]
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white',
           xlab = 'Waiting time to next eruption (in mins)',
           main = 'Histogram of waiting times')    
      })


### Session Info

<details>
<pre><code>
‘/tmp/RtmpgTyNVb/downloaded_packages’
> devtools::session_info() 
─ Session info ─────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       Ubuntu 20.04.6 LTS
 system   x86_64, linux-gnu
 ui       RStudio
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2023-09-18
 rstudio  2023.06.1+524.pro1 Mountain Hydrangea (server)
 pandoc   3.1.1 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown)

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────
 ! package        * version    date (UTC) lib source
   bsicons          0.1.1      2023-08-11 [1] RSPM (R 4.3.0)
   bslib          * 0.5.1.9000 2023-09-18 [1] Github (rstudio/bslib@7e51550)
   cachem           1.0.8      2023-05-01 [1] RSPM (R 4.3.0)
   callr            3.7.3      2022-11-02 [1] RSPM (R 4.3.0)
   cli              3.6.1      2023-03-23 [1] RSPM (R 4.3.0)
   colorspace       2.1-0      2023-01-23 [1] RSPM (R 4.3.0)
   crayon           1.5.2      2022-09-29 [1] RSPM (R 4.3.0)
   curl             5.0.2      2023-08-14 [1] RSPM (R 4.3.0)
   data.table       1.14.8     2023-02-17 [1] RSPM (R 4.3.0)
   devtools         2.4.5      2022-10-11 [1] RSPM (R 4.3.0)
   digest           0.6.33     2023-07-07 [1] RSPM (R 4.3.0)
   dplyr          * 1.1.3      2023-09-03 [1] RSPM (R 4.3.0)
   ellipsis         0.3.2      2021-04-29 [1] RSPM (R 4.3.0)
   evaluate         0.21       2023-05-05 [1] RSPM (R 4.3.0)
   fansi            1.0.4      2023-01-22 [1] RSPM (R 4.3.0)
   fastmap          1.1.1      2023-02-24 [1] RSPM (R 4.3.0)
   flexdashboard    0.6.2      2023-08-11 [1] RSPM (R 4.3.0)
   forcats        * 1.0.0      2023-01-29 [1] RSPM (R 4.3.0)
   fs               1.6.3      2023-07-20 [1] RSPM (R 4.3.0)
   generics         0.1.3      2022-07-05 [1] RSPM (R 4.3.0)
   ggplot2        * 3.4.3      2023-08-14 [1] RSPM (R 4.3.0)
   glue             1.6.2      2022-02-24 [1] RSPM (R 4.3.0)
   gridlayout     * 0.2.1      2023-09-18 [1] Github (rstudio/gridlayout@f00d0e6)
   gtable           0.3.4      2023-08-21 [1] RSPM (R 4.3.0)
   here             1.0.1      2020-12-13 [1] RSPM (R 4.3.0)
   hms              1.1.3      2023-03-21 [1] RSPM (R 4.3.0)
   htmltools        0.5.6.9000 2023-09-18 [1] Github (rstudio/htmltools@f8ef163)
   htmlwidgets      1.6.2      2023-03-17 [1] RSPM (R 4.3.0)
   httpuv           1.6.11     2023-05-11 [1] RSPM (R 4.3.0)
   httr             1.4.7      2023-08-15 [1] RSPM (R 4.3.0)
   jquerylib        0.1.4      2021-04-26 [1] RSPM (R 4.3.0)
   jsonlite         1.8.7      2023-06-29 [1] RSPM (R 4.3.0)
   knitr            1.44       2023-09-11 [1] RSPM (R 4.3.0)
   later            1.3.1      2023-05-02 [1] RSPM (R 4.3.0)
   lattice          0.21-8     2023-04-05 [2] CRAN (R 4.3.1)
   lazyeval         0.2.2      2019-03-15 [1] RSPM (R 4.3.0)
   lifecycle        1.0.3      2022-10-07 [1] RSPM (R 4.3.0)
   lubridate      * 1.9.2      2023-02-10 [1] RSPM (R 4.3.0)
   magrittr         2.0.3      2022-03-30 [1] RSPM (R 4.3.0)
   memoise          2.0.1      2021-11-26 [1] RSPM (R 4.3.0)
   mime             0.12       2021-09-28 [1] RSPM (R 4.3.0)
   miniUI           0.1.1.1    2018-05-18 [1] RSPM (R 4.3.0)
   munsell          0.5.0      2018-06-12 [1] RSPM (R 4.3.0)
   pillar           1.9.0      2023-03-22 [1] RSPM (R 4.3.0)
   pkgbuild         1.4.2      2023-06-26 [1] RSPM (R 4.3.0)
   pkgconfig        2.0.3      2019-09-22 [1] RSPM (R 4.3.0)
   pkgload          1.3.2.1    2023-07-08 [1] RSPM (R 4.3.0)
   plotly         * 4.10.2     2023-06-03 [1] RSPM (R 4.3.0)
   prettyunits      1.1.1      2020-01-24 [1] RSPM (R 4.3.0)
   processx         3.8.2      2023-06-30 [1] RSPM (R 4.3.0)
   profvis          0.3.8      2023-05-02 [1] RSPM (R 4.3.0)
   promises         1.2.1      2023-08-10 [1] RSPM (R 4.3.0)
   ps               1.7.5      2023-04-18 [1] RSPM (R 4.3.0)
   purrr          * 1.0.2      2023-08-10 [1] RSPM (R 4.3.0)
   R6               2.5.1      2021-08-19 [1] RSPM (R 4.3.0)
   ragg             1.2.5      2023-01-12 [1] RSPM (R 4.3.0)
   rappdirs         0.3.3      2021-01-31 [1] RSPM (R 4.3.0)
   Rcpp             1.0.11     2023-07-06 [1] RSPM (R 4.3.0)
   readr          * 2.1.4      2023-02-10 [1] RSPM (R 4.3.0)
   remotes          2.4.2.1    2023-07-18 [1] RSPM (R 4.3.0)
   rlang            1.1.1      2023-04-28 [1] RSPM (R 4.3.0)
   rmarkdown        2.24       2023-08-14 [1] RSPM (R 4.3.0)
   rprojroot        2.0.3      2022-04-02 [1] RSPM (R 4.3.0)
   rstudioapi       0.15.0     2023-07-07 [1] RSPM (R 4.3.0)
   sass             0.4.7      2023-07-15 [1] RSPM (R 4.3.0)
   scales           1.2.1      2022-08-20 [1] RSPM (R 4.3.0)
   sessioninfo      1.2.2      2021-12-06 [1] RSPM (R 4.3.0)
   shiny          * 1.7.5      2023-08-12 [1] RSPM (R 4.3.0)
   shinydashboard * 0.7.2      2021-09-30 [1] RSPM (R 4.3.0)
   shinyuieditor  * 0.5.0      2023-09-18 [1] Github (rstudio/shinyuieditor@9675068)
   sourcetools      0.1.7-1    2023-02-01 [1] RSPM (R 4.3.0)
   stringi          1.7.12     2023-01-11 [1] RSPM (R 4.3.0)
   stringr        * 1.5.0      2022-12-02 [1] RSPM (R 4.3.0)
   systemfonts      1.0.4      2022-02-11 [1] RSPM (R 4.3.0)
   textshaping      0.3.6      2021-10-13 [1] RSPM (R 4.3.0)
   thematic       * 0.1.3      2023-09-18 [1] Github (rstudio/thematic@8af5f8d) (on disk 0.1.3.9000)
   tibble         * 3.2.1      2023-03-20 [1] RSPM (R 4.3.0)
   tidyr          * 1.3.0      2023-01-24 [1] RSPM (R 4.3.0)
   tidyselect       1.2.0      2022-10-10 [1] RSPM (R 4.3.0)
   tidyverse      * 2.0.0      2023-02-22 [1] RSPM (R 4.3.0)
   timechange       0.2.0      2023-01-11 [1] RSPM (R 4.3.0)
   tzdb             0.4.0      2023-05-12 [1] RSPM (R 4.3.0)
   urlchecker       1.0.1      2021-11-30 [1] RSPM (R 4.3.0)
   usethis          2.2.2      2023-07-06 [1] RSPM (R 4.3.0)
   utf8             1.2.3      2023-01-31 [1] RSPM (R 4.3.0)
   vctrs            0.6.3      2023-06-14 [1] RSPM (R 4.3.0)
   viridisLite      0.4.2      2023-05-02 [1] RSPM (R 4.3.0)
   withr            2.5.0      2022-03-03 [1] RSPM (R 4.3.0)
   xfun             0.40       2023-08-09 [1] RSPM (R 4.3.0)
   xtable           1.8-4      2019-04-21 [1] RSPM (R 4.3.0)
   yaml             2.3.7      2023-01-23 [1] RSPM (R 4.3.0)

 [1] /cloud/lib/x86_64-pc-linux-gnu-library/4.3
 [2] /opt/R/4.3.1/lib/R/library
</code></pre>
</details>
cpsievert commented 1 year ago

Thanks for reporting. Here's a more minimal reprex without {flexdashboard}. Also, since the error actually derives from Shiny's codebase, I'll be transferring the issue other there.

---
title: "thematic+renderPlot() in Rmd error"
output: rmarkdown::html_document
runtime: shiny
---

```{r}
thematic::thematic_shiny(bg = "auto", fg = "auto")
sliderInput("bins", "Number of bins:", 1, 50, 30)

renderPlot({
  plot(1:10)
})
cpsievert commented 1 year ago

Also, I'm sure we can avoid the error in this case, but I'm not sure whether we'll be able to get the "auto theming" behavior to work in the way you might be expecting. The good news, though, is that you can workaround the problem by creating an explicit plotOutput() container, like this:

---
title: "Suggested fix"
output: 
  flexdashboard::flex_dashboard:
      theme: 
        version: 5
        bootswatch: "darkly"
runtime: shiny
---

```{r}
thematic::thematic_shiny(bg = "auto", fg = "auto")
sliderInput("bins", "Number of bins:", 1, 50, 30)
plotOutput("p")
output$p <- renderPlot({
  plot(1:10)
})