在闪亮的应用程序中显示由复选框选择的数据表的行数

Display the number of rows of a datatable that are selected by tickboxes in a shiny app

我有一个简单闪亮的应用程序。

#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 = 1,
                 min=1
    )
  })



  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 = "os", selector = "td:first-child")),
    selection=list(mode="single")


  )

}

我需要做的是在侧边栏面板的 numericInput() 中显示数据 table 中 select 的行数。此号码已显示在 table 下,但我也想将其显示在 numericInput() 下。如果我想 select 多个项目,我需要按住 Mac 上的命令键。在 Windows 机器上,我认为它应该是控制键。或者使用 shift 键 select 多个相邻项目。

这是一个使用带有 shiny 后端的 RMarkdown 文档的最小示例,以显示如何获取 selected 行数。

---
title: "Untitled"
output: html_document
runtime: shiny
---

```{r echo=FALSE}
library(DT)
library(tidyverse)
dataTableOutput("irisTable")
output$irisTable <- 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")
        ))

p("Selected rows are...")
renderText(input$irisTable_rows_selected)
```

请注意,与我对 的回答相比,我已将 select.style 行为更改为 select = list(style = "multi", selector = "td:first-child");这允许您通过单击行(而不是必须按住 Command/Ctrl 键)来 select 多个条目。