使Shiny应用程序中的侧边栏面板(sidebarPanel)随着主面板(mainPanel)自动滚动

6
我在shiny中创建了一个sidebarLayout,它允许在mainPanel溢出时滚动。sidebarPanel的位置保持固定,因此当您向下滚动mainPanel时,它会消失。但是,我希望它能够随着mainPanel一起滚动,这样用户就不需要再向上滚动来更改设置了。我该如何做到这一点?
基本设置:
ui = fluidPage(

  tags$style(type='text/css', 'body { overflow-y: scroll; }'),

  titlePanel(
    # ...
  ),

  sidebarLayout(

    sidebarPanel(
      # ...
      width = 3),

    mainPanel(
      # ...

      width = 9 )

  ))

server = function(input,output,session) {

  # ...

}

shinyApp(ui=ui, server=server)
1个回答

16
你可以在你的 sidebarPanel 中添加 style = "position:fixed;width:inherit;",但会失去元素的填充,并且宽度将恰好为页面的 1/4(25%),例如,如果你想要侧边栏和主面板之间有更多的空间,请设置为 22%。
示例:
library("shiny")

ui <- fluidPage(

  titlePanel(
    "Fixed sidebar panel"
  ),

  sidebarLayout(

    sidebarPanel(
      style = "position:fixed;width:inherit;",
      "Inputs",
      width = 3),


    mainPanel(

      lapply(
        X = 1:20,
        FUN = function(i) {
          plotOutput(outputId = paste("plot", i, sep = "-"))
        }
      ),

      width = 9 )

  ))

server <- function(input, output, session) {

  lapply(
    X = 1:20,
    FUN = function(i) {
      output[[paste("plot", i, sep = "-")]] <- renderPlot({plot(rnorm(10))})
    }
  )

}

shinyApp(ui = ui, server = server)

style = "position:fixed;width:22%;" 用于调整填充为22%,如解决方案中所述。 - user989762
这似乎会停止shiny的显示响应性——即,当缩小页面宽度时,图表从未切换到侧边栏下方。 - dca

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接