2017-02-17 38 views
0

我想根據兩個上層輸入來子集一組複選框選項(狀態),一個「全選」和另一個子集(區域)選擇。問題是我 想要預先選擇Region1,但還沒有找到顯示其狀態的方法,因爲與selectall更新有衝突。由於審美原因,我也不希望 將「全選」輸入與子集輸入合併。從兩個衝突的上層輸入中選擇checkBoxGroupInputs的子集

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; 
           } 
           .multicol2 { 

            -webkit-column-count: 2; /* Chrome, Safari, Opera */ 
            -moz-column-count: 2; /* Firefox */ 
            column-count: 2; 
            -moz-column-fill: auto; 
            -column-fill: auto; 
           } 
           "))), 
titlePanel("Panel"), 
sidebarLayout(  
    sidebarPanel(
     selectInput("species", "Select species:", 
        choices=examplesubset$species) 
    ) , 
    mainPanel(
     fluidRow(
     column(3, 
     uiOutput("checkboxesui"), 
     uiOutput("checkboxesuiall"), 
     uiOutput("checkboxesuiregion") 
    )))) 
) 

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, ",")) 
      )) 
    }) 

    output$checkboxesuiall<-renderUI({ 
    checkboxInput("allcheckboxes", "Select all", FALSE) 
    }) 


    output$checkboxesuiregion<-renderUI({ 
    tags$div(align = 'left', 
      class = 'multicol2', 
      checkboxGroupInput("regionscheckboxes", "Regions", 
             choices=levels(regions$region) 
             , selected="Region1" 
      ) 
    ) 
    }) 

    selectedregion<-reactive({ 
    sel<- regions[which(regions$region %in% input$regionscheckboxes),] 
    }) 

    observeEvent(input$allcheckboxes,{ 
    if(input$allcheckboxes == TRUE) 
    { 
     updateCheckboxGroupInput(session, "regionscheckboxes", "Regions", 
           choices=levels(regions$region) 
           , selected=levels(regions$region) 
    ) 
    } 
    else 
    { 
     updateCheckboxGroupInput(session, "regionscheckboxes", "Regions", 
           choices=levels(regions$region) 
           , selected=c() 
    ) 
    } 
    }) 

} 
shinyApp(ui = ui, server = server) 

回答

0

您可以使用一個布爾值來說明這是你第一次在代碼中得到:

server<-function(input, output,session) { 
    firsttime<<- TRUE 
    ... 
    observeEvent(input$allcheckboxes,{ 
    if(input$allcheckboxes == TRUE) 
    { 
     updateCheckboxGroupInput(session, "regionscheckboxes", "Regions", 
           choices=levels(regions$region) 
           , selected=levels(regions$region)#"Cerrado"#levels(regions$region) 
    ) 
    } 
    else 
    { 
     if(firsttime) 
     updateCheckboxGroupInput(session, "regionscheckboxes", "Regions", 
           choices=levels(regions$region) 
           , selected="Region1" 
           ) 
     else 
     updateCheckboxGroupInput(session, "regionscheckboxes", "Regions", 
           choices=levels(regions$region) 
     ) 
     firsttime <<- FALSE 
    } 
    }) 
}