在 R Shiny 应用程序中使用滚动条绘制大图

Xia*_* Ma 4 plot r scrollbar horizontal-scrolling shiny

我想在 Shiny 应用程序中为绘图添加滚动条,但只出现垂直滚动条,而不会出现水平滚动条。我在这里附上了一个带有最少元素的闪亮小应用程序来演示该问题。

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')
      })
    }
  )
Run Code Online (Sandbox Code Playgroud)

Bro*_*ose 8

默认值renderPlot(width="auto")导致它继承默认值的宽度plotOutput(width="100%")。这意味着绘图将按照 div 的大小绘制,此处指定为 200px,因此不需要溢出。renderPlot(width=300)如果您明确指定 或的宽度plotOutput(width="300px")(请注意前者是整数,后者是字符),则溢出将处于活动状态。

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
    })
  }
)
Run Code Online (Sandbox Code Playgroud)