R 使用闪亮的小部件选择datatable的特定行

R 使用闪亮的小部件选择datatable的特定行,r,shiny,dt,R,Shiny,Dt,我有下面的闪亮的应用程序,用户在其中单击一行并在其旁边获取其索引。是否可以使用pickerInput()选择所选行,该行包括除所选行以外的所有行名,当使用选项激活时,它将显示对应行并将其索引显示为文本 library(shiny) library(DT) library(shinyWidgets) shinyApp( ui = fluidPage( title = 'Select Table Rows', h1('A Server-side Table'),

我有下面的
闪亮的
应用程序,用户在其中单击一行并在其旁边获取其索引。是否可以使用
pickerInput()
选择所选行,该行包括除所选行以外的所有行名,当使用选项激活时,它将显示对应行并将其索引显示为文本

library(shiny)
library(DT)
library(shinyWidgets)

shinyApp(
  ui = fluidPage(
    title = 'Select Table Rows',
    
    h1('A Server-side Table'),
    
    fluidRow(
      column(9, DT::dataTableOutput('x3')),
      column(3, verbatimTextOutput('x4')),
      uiOutput("dfatt")
    )
    
  ),
  server = function(input, output, session) {

    # server-side processing
    mtcars2 = mtcars[, 1:8]
    output$x3 = DT::renderDataTable({datatable(selection = list(target = "row", mode = "single"),mtcars )})
    
    # print the selected indices
    output$x4 = renderPrint({
      s = input$x3_rows_selected
      if (length(s)) {
        cat('These rows were selected:\n\n')
        cat(s, sep = ', ')
      }
    })
    output$dfatt<-renderUI({
      if(is.null(input$x3_rows_selected)){
        pickerInput(
          inputId = "Id008",
          label = "Different attribute", 
          choices = c(unique(as.character(rownames(mtcars)))),
          multiple = F,
          selected = "Badge danger"
          
        )
      }
      else{
        cell <- input$x3_rows_selected
        pickerInput(
          inputId = "Id008",
          label = "Different attribute", 
          choices = c(unique(as.character(rownames(mtcars))))[-cell],
          multiple = F,
          selected = "Badge danger"
          
        )
      }
      
    })
})
库(闪亮)
图书馆(DT)
图书馆(shinyWidgets)
shinyApp(
ui=fluidPage(
标题='选择表行',
h1(“服务器端表”),
fluidRow(
列(9,DT::dataTableOutput('x3')),
列(3,逐字输出('x4')),
uiOutput(“dfatt”)
)
),
服务器=功能(输入、输出、会话){
#服务器端处理
mtcars2=mtcars[,1:8]
输出$x3=DT::renderDataTable({datatable(selection=list(target=“row”,mode=“single”),mtcars)})
#打印所选索引
输出$x4=renderPrint({
s=输入$x3\u行\u选中
若有(长度){
cat('已选择这些行:\n\n')
类别(s,sep=',')
}
})
输出$dfatt
库(闪亮)
图书馆(DT)
图书馆(shinyWidgets)

dat谢谢,这是有效的,但如何组合多个回调?
library(shiny)
library(DT)
library(shinyWidgets)

dat <- mtcars[1:6,]

callback <- JS(
  "Shiny.addCustomMessageHandler(",
  "  'selectRow',",
  "  function(index) {",
  "    table.row(index - 1).select();",
  "  }",
  ");"
)

ui <- fluidPage(
  br(),
  DTOutput("dtable"),
  br(),
  fluidRow(
    column(
      4,
      pickerInput(
        "rowname",
        label = "Choose a row",
        choices = setNames(1:nrow(dat), rownames(dat))
      ) 
    ),
    column(
      3,
      textOutput("selectedRow")
    )
  )
)

server <- function(input, output, session) {
  
  output[["dtable"]] <- renderDT({
    datatable(
      dat, 
      extensions = "Select",
      selection = "none",
      callback = callback,
      options = list(
        columnDefs = list(
          list(className = "dt-center", targets = "_all")
        ),
        select = list(style = "single")
      )
    )
  }, server = FALSE)
  
  output[["selectedRow"]] <- renderText({
    i <- input[["dtable_rows_selected"]]
    paste0(
      "Selected row: ", 
      ifelse(is.null(i), "none", i)
    )
  })
  
  observeEvent(input[["rowname"]], {
    session$sendCustomMessage("selectRow", input[["rowname"]])
  })
  
}


shinyApp(ui, server)