Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using scroll bars for big plots in R Shiny app

I would like to add a scrollbar for a plot in Shiny app, but only the vertical scroll bar appear while horizontal scroll bars do not appear. I attached a small shiny app with minimum elements here to demonstrate the problem.

cat("\014")
unlink(".RData")
rm(list=ls(all.names = TRUE))

  # A basic shiny app with a plotOutput
  shinyApp(
    ui = fluidPage(
      sidebarLayout(
        sidebarPanel(
        ),
        mainPanel(
          column(6,(div(style='width:200px;overflow-x: scroll;height:200px;overflow-y: scroll;',
                      uiOutput("plot"))) )

        )
      )
    ),
    server = function(input, output) {
      output$plot <- renderUI({
       output$plot2 <- renderPlot(plot(cars))
       plotOutput('plot2')
      })
    }
  )
like image 490
Xiaowei Ma Avatar asked Sep 07 '25 00:09

Xiaowei Ma


1 Answers

The default renderPlot(width="auto") causes it to inherit the width from the default plotOutput(width="100%"). This means the plot is drawn to the size of the div, which here you give to be 200px, therefore no overflow is required. Overflow will be active if you explicitly specify either the width of renderPlot(width=300) or of plotOutput(width="300px") (note the former is integer, latter is character).

shinyApp(
  ui = fluidPage(
    sidebarLayout(
      sidebarPanel(
      ),
      mainPanel(
        column(6,(div(style='width:200px;overflow-x: scroll;height:200px;overflow-y: scroll;',
                      uiOutput("plot"))) )
        
      )
    )
  ),
  server = function(input, output) {
    output$plot <- renderUI({
      output$plot2 <- renderPlot(plot(cars),width=300) # either will
      plotOutput('plot2',width='300px')                # work
    })
  }
)
like image 151
Brooks Ambrose Avatar answered Sep 09 '25 19:09

Brooks Ambrose