Closed aronatkins closed 1 year ago
The height
argument of renderPlot()
controls the height of the generated image, so you are indeed creating an image that is twice as wide as it is tall.
But the image itself appears inside the container created by plotOutput()
, which has a height controlled by height
in plotOutput()
. The default height of that container is 400px
. Even though the image is smaller than 400px tall, the plot output container still reserves 400px of vertical space. This accounts for both the empty space and the overlapping "below" text.
In the image below, I added a red border around the plot output container (using tags$style(".shiny-plot-output { border: 1px solid red }")
).
Try setting height = "auto"
in plotOutput()
to have the plot output container be driven by the contents within it. Your UI code would look like this:
ui <- fluidPage(
titlePanel("height-restricted"),
sidebarLayout(
sidebarPanel(
h4("above"),
plotOutput("side", height = "auto"),
h4("below")
),
mainPanel(
h4("above"),
plotOutput("main", height = "auto"),
h4("below")
)
)
)
Ah. I see. I need BOTH the height computation in R and height = "auto"
in the UI definition.
Here's what I ended up with, which is closer to the real app:
# https://github.com/rstudio/shiny/issues/3798
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("height-restricted"),
sidebarLayout(
sidebarPanel(
h4("above"),
plotOutput("side", height = "auto"),
plotOutput("second", height = "auto"),
h4("below")
),
mainPanel(
"the contents of this panel do not matter."
)
)
)
server <- function(input, output, session) {
output$side <- renderPlot({
ggplot(mtcars, aes(mpg, cyl)) + geom_point()
}, height = function() {
# 1:1
session$clientData$output_side_width
})
output$second <- renderPlot({
ggplot(mtcars, aes(mpg, cyl)) + geom_point()
}, height = function() {
# 2:1
session$clientData$output_second_width / 2
})
}
shinyApp(ui = ui, server = server)
If I remove the height adjustment in R, the following error occurs:
Warning: Error in graphics::plot.new: figure margins too large
130: graphics::plot.new
127: startPNG
126: drawPlot
112: <reactive:plotObj>
96: drawReactive
83: renderFunc
82: output$second
1: shiny::runApp
Closing; I incompletely followed the advice in https://github.com/rstudio/shiny/issues/650#issuecomment-62443654
System details
Browser Version:
Output of
sessionInfo()
:Example application or steps to reproduce the problem
Describe the problem in detail
This example is using the height-modifying approach recommended in https://github.com/rstudio/shiny/issues/650#issuecomment-62443654
I expected a plot that is half as tall as it is wide, with no vertical space beneath, and the trailing text correctly placed.
In the RStudio IDE, the two plots show space beneath.
In Firefox, space is only below the plot in the sidebar.
In Safari, space is only below the sidebar plot, but the
h4
beneath the main plot has no vertical spacing.In Chrome, space is below the sidebar plot, and the
h4
beneath the main plot overlaps with the plot.