Skip to content

No dynamic resizing of ggplots with bslib and withSpinner() #96

@fzenoni

Description

@fzenoni

When using withSpinner() together with bslib layout functions, plots created with ggplot2 will no longer automatically fill the height of the browser.

For instance, consider this Shiny app (link to Shinylive.io):

library(shiny)
library(bslib)
library(ggplot2)

# A simple ggplot
plot_gg <- function(title) {
  ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    ggtitle(title)
}

ui <- page_fillable(
  layout_columns(
    card(plotOutput("plot1", height = "200px")),
    card(plotOutput("plot2", height = "200px"))
  ),
  card(plotOutput("plot3", height = "200px"))
)

server <- function(input, output, session) {
  output$plot1 <- renderPlot(plot_gg("Plot 1"))
  output$plot2 <- renderPlot(plot_gg("Plot 2"))
  output$plot3 <- renderPlot(plot_gg("Plot 3"))
}

shinyApp(ui, server)

When the browser window is resized, the height of the plots is automatically adjusted to fill the entire height. This is the desired result.

If I now introduce withSpinner(), the code becomes as follows (link to Shinyapp.io), and the plots height are now predetermined and static.

library(shiny)
library(bslib)
library(ggplot2)
library(shinycssloaders)

# A simple ggplot
plot_gg <- function(title) {
  ggplot(mtcars, aes(x = wt, y = mpg)) +
    geom_point() +
    ggtitle(title)
}

ui <- page_fillable(
  layout_columns(
    card(withSpinner(plotOutput("plot1", height = "200px"))),
    card(withSpinner(plotOutput("plot2", height = "200px")))
  ),
  card(withSpinner(plotOutput("plot3", height = "200px")))
)

server <- function(input, output, session) {
  output$plot1 <- renderPlot(plot_gg("Plot 1"))
  output$plot2 <- renderPlot(plot_gg("Plot 2"))
  output$plot3 <- renderPlot(plot_gg("Plot 3"))
}

shinyApp(ui, server)

Is there any built-in way to fix this behavior?
I wonder if this is related to #76, but using fill = TRUE did not help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions