R shiny: How to get square plot to use full width of panel? -
i have square plot (aspect ratio 1:1) i'm trying scale full width of mainpanel. due height parameter being 400px default, plot comes out 400x400 image in middle of panel:
i read can alter height parameter of plotoutput "100%" or "auto" have panel use natural dimensions of image. however, plotoutput tries retrieve height parameter renderplot, getting height value plotoutput resulting in "catch-22" scenario , plot of height 0px.
what i'm trying set height value of renderplot (or plotoutput) equal width of mainpanel, i'm unsure how access value. here's code 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 value 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?
based on this post:
library( shiny ) library( ggplot2 ) server <- function(input, output) { output$plot1 <- renderplot({ x <- data.frame( x=rnorm(10), y=rnorm( 10) ) ggplot( x, aes(x=x, y=y) ) + geom_point() + coord_fixed() } , height=reactive(ifelse(!is.null(input$innerwidth),input$innerwidth*3/5,0)) ) } ui <- fluidpage( sidebarlayout( sidebarpanel( tags$head(tags$script('$(document).on("shiny:connected", function(e) { shiny.oninputchange("innerwidth", window.innerwidth); }); $(window).resize(function(e) { shiny.oninputchange("innerwidth", window.innerwidth); }); ')), sliderinput("n", "number of samples", min=10, max=1000, value=100) ), mainpanel( plotoutput("plot1")) ) ) shinyapp(ui = ui, server = server) 
Comments
Post a Comment