【问题标题】:Align checkBoxGroupInput vertically and horizontally垂直和水平对齐 checkBoxGroupInput
【发布时间】:2017-03-12 00:09:18
【问题描述】:

在类似的帖子 (How to align a group of checkboxGroupInput in R Shiny) 中,复选框仅垂直对齐(如我的示例)或仅水平对齐(@9​​87654322@)。我想知道是否有办法在两种意义上都实现这一点(在列中)。

library(shiny)
examplesubset<-read.table(text="
                          elements locations
                          element_One A,M,P,R
                          element_Two A,B,C,M,P,E,I
                          element_Three G,M,T,F,O,H,A,B,C,D" ,  header=TRUE, stringsAsFactors=FALSE)
examplesubset$elements<-as.factor(examplesubset$elements)

ui<-fluidPage(    
  tags$head(tags$style(HTML("
                            .multicol {
                            -webkit-column-count: 3; /* Chrome, Safari, Opera */
                            -moz-column-count: 3;    /* Firefox */
                            column-count: 3;
                            -moz-column-fill: auto;
                            -column-fill: auto;
                            }
                            "))),
  titlePanel("Panel"),
  sidebarLayout(      
    sidebarPanel(
      selectInput("elements", "Select elements:", 
                  choices=examplesubset$elements)
    ) ,
    mainPanel(
      fluidRow(
        column(3,
               uiOutput("checkboxesui")
        ))))
  )

server<-function(input, output,session) {
  elementsselected<-reactive({
    sp<-examplesubset[examplesubset$elements==input$elements,]
    sp<-droplevels(sp)
  })
  locationsreactive<- reactive({
    j<-as.factor(unique(unlist(strsplit(elementsselected()$locations, ",", fixed = TRUE) ) ) )
    j<-droplevels(j)
  })
  output$checkboxesui<-renderUI({
    tags$div(align = 'left',
             class = 'multicol',
             checkboxGroupInput("locationscheckboxes", "locations",
                                choices=levels(locationsreactive()) 
                                , selected=c() )
             ) 
  })
}
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: css r shiny


    【解决方案1】:

    下面的代码应该可以解决问题。您需要将 CSS 列应用到您拥有它们的下方的 div 中,然后从复选框上方和下方删除填充,我还将列填充更改为平衡:

    library(shiny)
    examplesubset<-read.table(text="
                              elements locations
                              element_One A,M,P,A,R,T
                              element_Two A,B,C,M,P,E,I,N,S
                              element_Three G,M,T,F,S,V,P" ,  header=TRUE, stringsAsFactors=FALSE)
    examplesubset$elements<-as.factor(examplesubset$elements)
    
    ui<-fluidPage(    
      tags$head(tags$style(HTML("
                                .multicol .shiny-options-group{
                                -webkit-column-count: 3; /* Chrome, Safari, Opera */
                                -moz-column-count: 3;    /* Firefox */
                                column-count: 3;
                                -moz-column-fill: balanced;
                                -column-fill: balanced;
                                }
                                .checkbox{
                                margin-top: 0px !important;
                                -webkit-margin-after: 0px !important; 
                                }
                                "))),
      titlePanel("Panel"),
      sidebarLayout(      
        sidebarPanel(
          selectInput("elements", "Select elements:", 
                      choices=examplesubset$elements)
        ) ,
        mainPanel(
          fluidRow(
            column(3,
                   uiOutput("checkboxesui")
            ))))
      )
    
    server<-function(input, output,session) {
      elementsselected<-reactive({
        sp<-examplesubset[examplesubset$elements==input$elements,]
        sp<-droplevels(sp)
      })
      locationsreactive<- reactive({
        j<-as.factor(unique(unlist(strsplit(elementsselected()$locations, ",", fixed = TRUE) ) ) )
        j<-droplevels(j)
      })
      output$checkboxesui<-renderUI({
        tags$div(align = 'left',
                 class = 'multicol',
                 checkboxGroupInput("locationscheckboxes", "locations",
                                    choices=levels(locationsreactive()) 
                                    , selected=c() )
        ) 
      })
    }
    shinyApp(ui = ui, server = server)
    

    另一种选择是使用 CSS flexbox:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    这将解决您描述的排序问题,但您需要使用 shiny-options-group div 的大小来使所有内容都适合您想要的方式,具体取决于您的内容。重新排序您的复选框选项可能更容易,以便它们以您想要的方式显示。

    【讨论】:

    • 我可以在几个小时后在 chrome 中查看此内容,然后我将在带有 chrome 的计算机上编辑我的答案。我认为firefox和opera认为他们很聪明,只使用两列,因为只有四个项目。如果您添加一个或删除一个,它会返回到三列。
    • thx,网格填充顺序对列有一些偏好,而不是行。这就是为什么 4,7,10 元素等会有一些奇怪的行为。
    • 没问题,乐于助人
    • 您还需要其他什么来接受并关闭此答案吗?
    猜你喜欢
    • 2012-10-10
    • 2013-11-25
    • 2014-12-05
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多