2017-05-24 145 views
1

我製作了兩個動作按鈕Select AllDeselect All。由於某種原因,Deselect All有效,但Select All不是「全選」動作按鈕不起作用

這是爲什麼?按照我的預期,Deselect All按鈕不會突出顯示所有行。但是,Select All按鈕不會執行任何操作。

input$selectAllinput$deselectAll正確更新(如TEMP選項卡中顯示)

任何人可以幫助?這是我的代碼。謝謝!

數據集:

colA <- c('A','B','C','D','E') 
colB <- c(1,2,3,4,5) 
rawdata <- as.data.frame(cbind(colA,colB)) 
View(rawdata) 

server.R

function(input, output, session) { 

    # Update summaryTable When users click 'Select All' 
    summaryTable <- eventReactive (input$selectAll,{ 
     print("SelectAll") 
     DT::datatable(rawdata, selection = list(target = 'row', selected = c(1:ncol(rawdata())))) 
    }) 

    # Update summaryTable When users click 'Deselect All' 
    summaryTable <- eventReactive (input$deselectAll,{ 
     print("deselectAll") 
     DT::datatable(rawdata, selection = list(target = 'row', selected = c(0))) 
    }) 


    # Default SummaryTable 
    output$inputVars <- DT::renderDataTable({ 

     if (input$selectAll==0 & input$deselectAll==0) { 
      print("Default") 
      DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE)) 
     } else { 
      summaryTable() 
     } 
    }) 

    output$temp <- renderPrint({ 
    print(input$selectAll) 
    print(input$deselectAll) 
    }) 

} 

ui.R

fluidPage(

    mainPanel(
     tabsetPanel(id = "allResults", 
     tabPanel(value='inputVars',title='Variable Selection', 
        verticalLayout(
         DT::dataTableOutput('inputVars'), 
         br(), 
         fluidRow(align="bottom", 
          column(2, actionButton("selectAll" , strong("Select All"))), 
          column(3, actionButton("deselectAll", strong("Deselect All"))) 
        ) 

       ) 
       ), 
     tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp")) 
    ) 
) 

) 
+0

ü要選擇的行或列? –

回答

0

這是你想要的嗎?請注意,我改變ncolnrow作爲SelecteAll爲宜與行

library(shiny) 
colA <- c('A','B','C','D','E') 
colB <- c(1,2,3,4,5) 
rawdata <- as.data.frame(cbind(colA,colB)) 

ui <- fluidPage(
    mainPanel(
    tabsetPanel(id = "allResults", 
       tabPanel(value='inputVars',title='Variable Selection', 
         verticalLayout(
          DT::dataTableOutput('inputVars'), 
          br(), 
          fluidRow(align="bottom", 
            column(2, actionButton("selectAll" , strong("Select All"))), 
            column(3, actionButton("deselectAll", strong("Deselect All"))) 
          ) 
         ) 
       ), 
       tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp")) 
    ) 
) 
) 

server <- function(input, output, session) { 

    var <- reactiveValues() 
    observeEvent(input$selectAll,{ 
    print("SelectAll") 
    var$selected <- 1:nrow(rawdata) 
    }) 

    observeEvent(input$deselectAll,{ 
    print("deselectAll") 
    var$selected <- 0 
    }) 

    # Default SummaryTable 
    output$inputVars <- DT::renderDataTable({ 
    if (input$selectAll==0 & input$deselectAll==0) { 
     print("Default") 
     DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE)) 
    } 
    else { 
     DT::datatable(rawdata, selection = list(target = 'row', selected = var$selected)) 
    } 
    }) 
} 

shinyApp(ui = ui, server = server) 

enter image description here