I have a square plot (aspect ratio 1:1) that I'm trying to scale to be the full width of mainPanel
. Due to the height
parameter being 400px
by default, the plot comes out as a 400x400 image in the middle of the panel:
I read that you can alter the height
parameter of plotOutput
to be "100%"
or "auto"
to have the panel use the natural dimensions of the image. However, plotOutput
then tries to retrieve the height parameter from renderPlot
, which itself is getting its height
value from plotOutput
resulting in a "Catch-22" scenario and a plot of height 0px
.
What I'm trying to do is set the height
value of renderPlot
(or plotOutput
) to be equal to the width
of mainPanel
, but I'm unsure how to access this value. Here's my code so far:
library( shiny )
library( ggplot2 )
server <- function(input, output)
{
output$plot1 <- renderPlot({
X <- data.frame( x=rnorm(input$n), y=rnorm( input$n ) )
ggplot( X, aes(x=x, y=y) ) + geom_point() + coord_fixed()
}, height=400 # how to get this value to be mainPanel width?
)
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("n", "Number of Samples", min=10, max=1000, value=100) ),
mainPanel( plotOutput("plot1", height="auto") )
)
)
shinyApp(ui = ui, server = server)
Any thoughts?