【问题标题】:How to reduce the border width of sideBarPanel in Shiny?如何在 Shiny 中减小 sideBarPanel 的边框宽度?
【发布时间】:2017-06-04 15:22:35
【问题描述】:

我一辈子都找不到关于这个的论点或问题,所以我问:

如何修改sideBarPanel 周围间隙边框的大小(宽度或高度)?

我最好的解决方案是通过div 为单个元素使用边框值,但我想知道如何更改整个元素的边框?

我们的目标是减少边框间隙,以便在放大时为我的元素“留出更多空间”(而不是让它们被挤压)。

这是显示“问题”的示例代码:

library(shiny)

ui <- fluidPage(

  titlePanel(
    h1("NMS Comparing tool", style = "font-size: 15px")
  ),

  sidebarPanel(width = 3, 
               div(style = "font-size: 8px;", 
                   sliderInput(inputId = "groups", 
                               label = "No. of Groups",
                               value = 4, min = 2, max = 12)
               ),  

               fluidRow(
                 column(6,offset=0,
                        div(style = "height: 105px; padding: 0px 0px",  
                            plotOutput(outputId = "scree")
                        )
                 ),

                 column(6,offset=0,
                        div(style = "font-size: 8px;padding: 0px 48px 15px; height: 105px",  #padding is top, right, bottom, left??
                            checkboxGroupInput(inputId = "labels",
                                               label = "Labels",
                                               choices = c("SPEC","Plot-End","Plot-Start"),
                                               selected = c("SPEC","Plot-End","Plot-Start")
                            )    
                        )
                 )
               ),

               fluidRow(
                 column(6,offset=0,
                        div(style = "font-size: 8px; padding: 0px 0px",      
                            radioButtons(inputId = "data",
                                         label = "Data",
                                         choices = c("PSP Only","PSP + MAP"),
                                         selected = "PSP + MAP")
                        )    
                 ),

                 column(6,offset=0,
                        div(style = "font-size: 8px;padding: 0px 10px;",  
                            radioButtons(inputId = "freq",
                                         label = "Frequency",
                                         choices = c(0.025,0.05),
                                         selected = 0.05)
                        )
                 )
               ),

               fluidRow(
                 column(6,offset=0,
                        div(style = "font-size: 8px; padding: 0px 0px; ",                 
                            radioButtons(inputId = "arrows",
                                         label = "Vector Choice",
                                         choices = c("Cumulative Change","All Samples","Hurricane"),
                                         selected = "Cumulative Change")
                        )
                 ),

                 column(6,offset=0,
                        div(style = "font-size: 8px;padding: 0px 10px",      
                            selectInput(inputId = "size",
                                        label = "Tree Size",
                                        choices = c("All","Canopy","Subcanopy","Small"),
                                        selected = "All"),
                            tags$style(type = "text/css",
                                       #".select-dropdown-content {max-height: 4px; }")
                                       "#size {height: 4px; }")
                        )
                 )
               ),

               fluidRow(
                 tags$style(type="text/css", "#info {font-size: 10px;}"),
                 verbatimTextOutput("info")
               ),
               fluidRow(
                 tags$style(type="text/css", "#SPECinfo {font-size: 10px;}"),
                 verbatimTextOutput("SPECinfo")
               )
  ),

  mainPanel(width = 9,
            plotOutput(outputId = "nms", click = "plot_click",dblclick = "plot_dblclick")

  )
)

server <- function(input, output) {

  output$scree <- renderPlot({

    par(mar = c(1.5,1.4,0.1,0.1), mgp = c(0.5,0.01,0), tcl = -0.1)
    plot(runif(99),runif(99),cex.axis=0.5,cex.lab=0.5,cex=0.75)

  },height = 95, width = 95) 

  output$nms <- renderPlot({

    plot(runif(99),runif(99))

  })

}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: html css r shiny panel


    【解决方案1】:

    如果我理解正确你的意思,你只需要修改闪亮对象的 CSS。在这种情况下,整个 sideBarPanel 具有 html 类 well

    为了演示如何更改它,我将使用tableHTML::make_css。该函数允许我们在 ui 中使用 css 对象,而无需加载 .css 文件。这将使演示变得非常容易:

    如果我在您的 sideBarPanel 代码中添加 tags$style(make_css(list('.well', 'border-width', '10px'))),则保持其他所有内容相同:

     sidebarPanel(width = 3, 
                  div(style = "font-size: 8px;", 
                      sliderInput(inputId = "groups", 
                                  label = "No. of Groups",
                                  value = 4, min = 2, max = 12)
                  ),  
                  #tags style expects a css file
                  #which is what make_css creates
                  #the first element of the list is the html class to modify
                  #the second is the border-width
                  #the third is the value of the width
                  tags$style(make_css(list('.well', 'border-width', '10px'))),
                  fluidRow(
                   column(6,offset=0,
                          div(style = "height: 105px; padding: 0px 0px",  
                              plotOutput(outputId = "scree")
                          )
                   )
    

    可以看到边框变成了10px:

    这意味着为了减少它,或者删除它,你需要在css中指定0px

     sidebarPanel(width = 3, 
                  div(style = "font-size: 8px;", 
                      sliderInput(inputId = "groups", 
                                  label = "No. of Groups",
                                  value = 4, min = 2, max = 12)
                  ),  
                  tags$style(make_css(list('.well', 'border-width', '0px'))),
                  fluidRow(
                   column(6,offset=0,
                          div(style = "height: 105px; padding: 0px 0px",  
                              plotOutput(outputId = "scree")
                          )
                   )
    

    显示如下:

    【讨论】:

    • 谢谢! tags$style 代码的去向是否重要?
    • 不客气!通常没有。即使你把它放在titlePanelsidebarPanel 之间,它仍然可以工作。甚至在fluidPage之后。
    • 如果你想轻松地在闪亮的 CSS 中进行实验,有一个很好的教程 here 关于如何使用 make_css
    • 太棒了。我对这一切还是很陌生(5 天前开始使用 shiny/css/html 的东西),所以这些教程是我真正需要的!非常感谢!! :D
    • 太棒了!非常乐意提供帮助:)
    猜你喜欢
    • 2021-06-10
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2023-01-23
    • 2021-04-24
    • 2016-06-06
    相关资源
    最近更新 更多