Home > database >  Resize shiny app to fit every screen size
Resize shiny app to fit every screen size

Time:02-05

Consider the toy app below. I built this app using a 22 inch monitor but when I open it with my 14inch screen laptop the graphics are too big and I am able to see only half of it. Question: Is there a way to make the app fit different screen sizes and make the graphics and tables resize to fit the screen size?

library(shiny)
library(shinyjs)

mytest <- c("first","second")
# Define UI for application that draws a histogram
ui <- fluidPage(
  useShinyjs(), # to initialise shinyjs
  
  # Application title
  titlePanel("disable"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "test",
                  label = "Test",
                  choices = mytest,
                  selected = "first"),
      disabled( # start as disabled
        checkboxInput("checkbox","check this box", FALSE))
    ),
    
    mainPanel(
      plotOutput("distPlot")
    )))


server <- function(input, output) {
  
  output$distPlot <- renderPlot({
    switch(input$test,
      "first" = { 
        plot(rnorm(100))
        disable("checkbox")
      },
      {
        enable("checkbox")
        if(input[["checkbox"]] == FALSE){
          "second" = plot(rnorm(1000), col="blue")
        } else{
          "second" = plot(rnorm(10000), col="red")   
        }
      }
    )
  })
}

shinyApp(ui = ui, server = server)

CodePudding user response:

The plotOutput function has a height argument and a width argument. You can use the CSS relative units vh and vw for these arguments: percentage of the height of the viewport and percentage of the width of the viewport. For example height = "50vh" to take 50% of the viewport.

  • Related