Shiny: No useful way for a plot to fill its available space.

Created on 27 Jan 2015  路  7Comments  路  Source: rstudio/shiny

I'm working my way up to my first real Shiny app, but I've hit my first real snag. The default plotOutput() height of a fixed 400px isn't really acceptable, not are any of the alternatives I've tried.

Right now, setting height equal to auto or 100% will just break the plot and stop it from rendering. A somewhat cryptic reply was given to the related issue #650, and it proposed a (rather nasty-seeming) workaround. Their workaround still doesn't give a nicely scaled plot, since it forces a 1:1 aspect ratio which doesn't allow the plot to expand to fill the viewing area. In fact, it quickly picks up a scroll bar if your window is very wide at all.

Is there no clean way to get a plot to fill up it's allotted area on the page automatically?

Below is a minimally reproducible example:

library(shiny)

server <- shinyServer(function(input, output, session) {
  output$sillyplot <- renderPlot({
    plot(1:input$slide.me)
  }
  ,height=function() session$clientData$output_sillyplot_width #! (Option1) <- This option forces 1:1 aspect ratio and quickly leads to scroll bars.
  )
})

ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    'Trivial Test'
    ,sliderInput('slide.me','Slide Me!',1,10,5)
  )
  ,mainPanel(
    'Gah'
    ,plotOutput(
      'sillyplot'
      #,height='auto' #! (Option2) <- This option breaks the plot! (When not overridden by Option1 above.)
    )
  )
))

shinyApp(ui, server)

Most helpful comment

I was looking for solutions on setting a fixed aspect ratio for a plot and found this issue. While it's not the prettiest solution, you can set a general aspect ratio using a slight modification of the first approach.

height = function() k * session$clientData$output_sillyplot_width

for example, i can force a 4:3 aspect ratio using

height = function() 0.75 * session$clientData$output_sillyplot_width

It would be nice to implement @jcheng5's suggestion, though. Or maybe an aspectRatio argument.

All 7 comments

The root of the problem is that there's not a good general way to have something take 100% of vertical space with HTML/CSS.

Alright, I'll accept that. I'll close this issue, but someone might think about updating the docs explaining why height='auto' is not a great idea.

It's not impossible to fill up a browser window with a plot--but it does require you to build your entire UI around that constraint. Here's an ancient example:

https://gist.github.com/jcheng5/4633110

shiny::runGist("https://gist.github.com/jcheng5/4633110")

I've added some better information about using height with plotOutput.

Great, thank you very much.

You know... the renderPlot width/height parameters take numeric values only. It would be kind of cool if you could have either (but not both) take a string like say "1.5X", so height = "1.5X" would mean, whatever the width is, multiply that by 1.5.

I was looking for solutions on setting a fixed aspect ratio for a plot and found this issue. While it's not the prettiest solution, you can set a general aspect ratio using a slight modification of the first approach.

height = function() k * session$clientData$output_sillyplot_width

for example, i can force a 4:3 aspect ratio using

height = function() 0.75 * session$clientData$output_sillyplot_width

It would be nice to implement @jcheng5's suggestion, though. Or maybe an aspectRatio argument.

Was this page helpful?
0 / 5 - 0 ratings