vendredi 17 février 2017

Is there a way to align horizontally checkboxgroupinputs in shinyapp

In a similar post How to align a group of checkboxGroupInput in R Shiny checkboxes are aligned only vertically. I wonder if there is a way to accomplish this horizontally too.

library(shiny)
regions <- read.table(text="
                      region states
                      Region1 A,B,C,D,E
                      Region2 F,G,H,I,J
                      Region3 K,L,M
                      Region4 N,O,P
                      Region5 Q,R,S,T
                      Region6 U,V,W,X,Y,Z" ,  header=TRUE, stringsAsFactors=FALSE)
regions$region<-as.factor(regions$region)

examplesubset<-read.table(text="
                          species states
                          speciesOne A,M,P,A,R,T
                          speciesTwo A,B,C,M,P,E,I,N,S
                          speciesThree G,M,T,F" ,  header=TRUE, stringsAsFactors=FALSE)
examplesubset$species<-as.factor(examplesubset$species)

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("species", "Select species:", 
                  choices=examplesubset$species)
    ) ,
    mainPanel(
      fluidRow(
        column(3,
               uiOutput("checkboxesui"),
        ))))
  )

server<-function(input, output,session) {
  speciesfromselectedgenus<-reactive({
    sp<-examplesubset[examplesubset$species==input$species,]#"
    sp<-droplevels(sp)
  })
  statesfromspeciesfromselectedgenus<- reactive({
    j<-as.factor(unique(unlist(strsplit(speciesfromselectedgenus()$states, ",", fixed = TRUE) ) ) )
    j<-droplevels(j)
  })
  output$checkboxesui<-renderUI({
    tags$div(align = 'left',
             class = 'multicol',
             checkboxGroupInput("statescheckboxes", "States",
                                choices=levels(statesfromspeciesfromselectedgenus()) 
                                , selected=unlist(strsplit(selectedregion()$states, ",") )
             ))
  })
  selectedregion<-reactive({
    sel<- regions[which(regions$region %in%  input$regionscheckboxes),]
  })
}
shinyApp(ui = ui, server = server)




Aucun commentaire:

Enregistrer un commentaire