Select specific row of a datatable with a shiny widget

人走茶凉 提交于 2021-01-29 05:21:17

问题


I have the shiny app below in which the user clicks on a row and gets its index next to it. Is it possible to select the selected row with a pickerInput() which includes all the row names except of the selected one and when activated with a choice it will display the correspondent row and display its index as text.

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

回答1:


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)


来源:https://stackoverflow.com/questions/65729967/select-specific-row-of-a-datatable-with-a-shiny-widget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!