Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling shiny plots to window height

Tags:

r

shiny

I want to scale a shiny plot to the height of the window. This related SO question only uses absolute height specifications in pixels, when a height = 100% would be preferable. I note in the documentation that absolutePanel can achieve this with its top, bottom, left, right arguments, but then you lose the side panel, and in any case the plot (while scaling to width) seems to ignore available height.

I'm guessing this relates to the html quirk that means you need to get the height with javascript innerHeight variable. But I'm unclear how to implement a solution in shiny to get ui.R to utilise this. Grateful for any pointers.

A basic app model for development:

ui.R

library(shiny) shinyServer(   function(input, output) {     output$myplot <- renderPlot({       hist(rnorm(1000))     })   } ) 

server.R

library(shiny) pageWithSidebar(   headerPanel("window height check"),   sidebarPanel(),   mainPanel(     plotOutput("myplot")   ) ) 
like image 292
geotheory Avatar asked Nov 06 '14 14:11

geotheory


1 Answers

Use CSS3. Declare your height in viewport units http://caniuse.com/#feat=viewport-units . You should be able to declare them using the height argument in plotOutput however shiny::validateCssUnit doesnt recognise them so you can instead declare them in a style header:

library(shiny) runApp(   list(server= function(input, output) {     output$myplot <- renderPlot({       hist(rnorm(1000))     })   }   , ui = pageWithSidebar(     headerPanel("window height check"),     sidebarPanel(       tags$head(tags$style("#myplot{height:100vh !important;}"))     ),     mainPanel(       plotOutput("myplot")     )   )   ) ) 

This wont work in the shiny browser but should work correctly in a main browser.

enter image description here

like image 180
jdharrison Avatar answered Sep 25 '22 21:09

jdharrison