DT数据表中所选行的计数未正确更新

时间:2018-07-16 13:38:04

标签: r shiny dt

我有一个简单的闪亮应用

#ui.r
navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(
               uiOutput("tex2")

             ),
             mainPanel(
               DT::dataTableOutput("hot3")

             )
           )))
#server.r
library(shiny)
library(DT)
library(tidyverse)
server <- function(input, output,session) {
  output$tex2<-renderUI({
    numericInput("text2","Rows selected",
                 value = input$hot3_rows_selected,
                 min=0
    )
  })



  output$hot3 <-DT::renderDataTable(
      iris %>% rowid_to_column("Row") %>% mutate(Row = ""),
      rownames = FALSE,
      extensions = "Select",
      options = list(
        columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
        select = list(style = "multi", selector = "td:first-child")
      ))
}

我有一个numericInput(),它通常应显示数据表中所选行的数量,但是如您所见,它仅显示所选的第一行,而不显示它们的数量。

1 个答案:

答案 0 :(得分:1)

要显示所选的行数,您需要使用input$hot3_rows_selected的长度(改为使用length(input$hot3_rows_selected))。

library(shiny)
ui <- navbarPage(
    "Application",
    tabPanel("General",
        sidebarLayout(
            sidebarPanel(uiOutput("tex2")),
            mainPanel(DT::dataTableOutput("hot3"))
        )
    )
)
server <- function(input, output,session) {

    library(tidyverse)
    output$tex2 <- renderUI({
        numericInput("text2", "Rows selected",
                     value = length(input$hot3_rows_selected),
                     min = 0)
    })

    output$hot3 <- DT::renderDataTable(
        iris %>% 
            rowid_to_column("Row") %>% 
            mutate(Row = ""),
        rownames = FALSE,
        extensions = "Select",
        options = list(
            columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
            select = list(style = "multi", selector = "td:first-child")
        )
    )
}

shinyApp(ui, server)

enter image description here