首页 > 解决方案 > 添加css“style = position:fixed;”时sidebarPanel缩小

问题描述

我需要有一个固定的侧边栏面板,但我在添加时遇到了问题style = 'position: fixed;'。当我添加样式时,sidebarPanel 会缩小。下面的代码(带style = 'position: fixed;')。该图像显示了没有和使用style = 'position: fixed;'如何使 sibarPanel 不收缩的结果?

# User interface
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(width = 2, 
                 style='position: fixed;',
                 uiOutput("countryList")),
    mainPanel(
      plotOutput("distPlot1"),
      plotOutput("distPlot2"),
      plotOutput("distPlot3"))))
# Server logic  
server <- function(input, output) {
  output$countryList <- renderUI({
    selectizeInput("countryName", "Country",
                   choices = c('Austria', 'The United Kingdom'))})
  output$distPlot1 <- renderPlot({hist(rnorm(100))})
  output$distPlot2 <- renderPlot({hist(rnorm(200))})
  output$distPlot3 <- renderPlot({hist(rnorm(300))})}

shinyApp(ui, server)

带有和不带有 <code>style = 'position: fixed;'</code> 的图像

标签: rshiny

解决方案


我认为这是这样的:

library(shiny)
# User interface
ui <- fluidPage(
  div(class = "row",
      div(class = "col-sm-4", 
          tags$form(class = "well col-sm-4", `data-spy` = "affix", # this is the sidebar
              uiOutput("countryList")
          )
      ), 
      div(class = "col-sm-8", # this is the main panel
          plotOutput("distPlot1"),
          plotOutput("distPlot2"),
          plotOutput("distPlot3")
      )
  )
)

# Server logic  
server <- function(input, output) {
  output$countryList <- renderUI({
    selectizeInput("countryName", "Country",
                   choices = c('Austria', 'The United Kingdom'))})
  output$distPlot1 <- renderPlot({hist(rnorm(100))})
  output$distPlot2 <- renderPlot({hist(rnorm(200))})
  output$distPlot3 <- renderPlot({hist(rnorm(300))})}

shinyApp(ui, server)

推荐阅读