Align checkBoxGroupInput vertically and horizontally

前端 未结 1 452
别跟我提以往
别跟我提以往 2021-01-12 05:06

In a similar post (How to align a group of checkboxGroupInput in R Shiny) checkboxes are aligned only vertically (as in my example) or only horizontally (R Shiny display che

相关标签:
1条回答
  • 2021-01-12 05:28

    The following code should do the trick. You needed to apply the CSS columns a div below where you had them and then remove padding from above and below the check boxes, I also changed the column fill to balanced:

    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)
    

    The other option is to use CSS flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    This would solve the ordering issues you described, but you would need to dink with the sizes of the shiny-options-group div to get everything to fit the way you want, depending on your content. It is probably easier to just reorder your checkbox options so they display the way you want.

    0 讨论(0)
提交回复
热议问题