Shiny: plotOutput height scalling issue

Created on 8 Nov 2014  Â·  8Comments  Â·  Source: rstudio/shiny

I'm currently not able to scale well my plot.
Default height in plotOutput is fixed to 400px while I would expect to have 100% there to keep the aspect ratio of my plot.
Yet I'm not able to provide height="100%", see below:

runApp(list(
  ui =   fluidPage(
    h1("plotOutput - WORKS"),
    plotOutput("Oplot_myplot"),
    h1("plotOutput height=600px - WORKS"),
    plotOutput("Oplot_myplot2", height="600px"),
    h1("plotOutput height=100% - DOESN'T WORK"),
    plotOutput("Oplot_myplot3", height="100%")
  ),
  server = function(input, output, session) {
    output$Oplot_myplot <- renderPlot(plot(1:100))
    output$Oplot_myplot2 <- renderPlot(plot(1:100))
    output$Oplot_myplot3 <- renderPlot(plot(1:100))
  }
))

The plotOutput("x", height="100%") simply doesn't work.

The question is: how can I keep correct aspect ratio in my plot in shiny?

packageVersion("shiny")
[1] ‘0.10.2.1’

Most helpful comment

The height/width in plotOutput correspond with CSS height/width, and height="100%" generally doesn't do what you want it to in HTML/CSS.

You can get the behavior you want by dynamically specifying the height from the R side:

library(shiny)
runApp(list(
  ui = fluidPage(
    plotOutput("plot1", height="auto")
  ),
  server = function(input, output, session) {
    output$plot1 <- renderPlot({
      plot(cars)
    }, height = function() {
      session$clientData$output_plot1_width
    })
  }
))

All 8 comments

The height/width in plotOutput correspond with CSS height/width, and height="100%" generally doesn't do what you want it to in HTML/CSS.

You can get the behavior you want by dynamically specifying the height from the R side:

library(shiny)
runApp(list(
  ui = fluidPage(
    plotOutput("plot1", height="auto")
  ),
  server = function(input, output, session) {
    output$plot1 <- renderPlot({
      plot(cars)
    }, height = function() {
      session$clientData$output_plot1_width
    })
  }
))

thanks for solution, still the fixed height to 400px is not a good idea IMO.
Regards

This is not well documented anywhere else that I have found. Please consider putting this either in the tutorial, or as an example in plotOutput() and/or renderPlot().

Could you please explain that function a bit more? Simply trying to fit a wordcloud to a fluidRow box has been a nightmare.

session$clientData$output_plot1_width

what is session? What is clientData? What is output_plot1_width? I am confused as none of those are in your ui or server code.

session is one of the arguments to the server function, you can see it in my code snippet. That code should work if you just replace the "plot1" in output_plot1_width with your plotOutput's actual id.

This already helped a lot, even after more than four years - thank you!

If I get it right, it's only possible to access width and height of outputs, but not UI elements in general, e.g. the height of a wellPanel?

@sreifenb That's correct, only outputs have this built-in support for sending their height/width/visibility.

Hi @hadley, your solution works (server side defined height), thank you for that.
However, the tabbox I'm using doesn't scale in height and the plot overflows.
image

How can I fix this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HarlanH picture HarlanH  Â·  3Comments

howardcoleman picture howardcoleman  Â·  5Comments

dmpe picture dmpe  Â·  4Comments

hlherrera picture hlherrera  Â·  5Comments

nteetor picture nteetor  Â·  5Comments