Jan Stanstrup
Jan Stanstrup

Reputation: 1232

Shiny: Select single row in DT with checkbox

In the example here there is an example of how to select rows with a checkbox: R Shiny, how to make datatable react to checkboxes in datatable

That works fine. But I need to only be able to select a single row.


I got close but there are two problems:

  1. when a box is ticked the reactive is trigger twice. I don't understand why. But then again I don't understand what activates the reactive since I don't see the input directly inside the reactive...
  2. If I click on the same box twice the selection is not really updated.


Any clue appreciated.

What I got so far. I also have a feeling I am over complicating things.

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput('x1'),
    verbatimTextOutput('x2')
  ),

  server = function(input, output, session) {
    # create a character vector of shiny inputs
    shinyInput = function(FUN, len, id, value, ...) {
      if (length(value) == 1) value <- rep(value, len)
      inputs = character(len)
      for (i in seq_len(len)) {
        inputs[i] = as.character(FUN(paste0(id, i), label = NULL, value = value[i]))
      }
      inputs
    }

    # obtain the values of inputs
    shinyValue = function(id, len) {
      unlist(lapply(seq_len(len), function(i) {
        value = input[[paste0(id, i)]]
        if (is.null(value)) FALSE else value
      }))
    }

    n = 6
    df = data.frame(
      cb = shinyInput(checkboxInput, n, 'cb_', value = FALSE, width='1px'),
      month = month.abb[1:n],
      YN = rep(FALSE, n),
      ID = seq_len(n),
      stringsAsFactors = FALSE)

    df_old <- df


    loopData = reactive({


      checked <- shinyValue('cb_', n)

      changed <- which((checked-df_old$YN)!=0)

      print(checked)
      print(changed)

      if(length(changed)==0){ df 
        }else{


      df$cb <<- shinyInput(checkboxInput, n, 'cb_', value = rep(FALSE, n), width='1px')
      df$YN <<- FALSE

      df$YN[changed] <<- checked[changed]
      df$cb[changed] <<- shinyInput(checkboxInput, length(changed), 'cb_', value = df$YN[changed], width='1px')


      df_old <<- df
      df
        }

    })

    output$x1 = DT::renderDataTable(
      isolate(loopData()),
      escape = FALSE, selection = 'none',
      options = list(
        dom = 't', paging = FALSE, ordering = FALSE,
        preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
        drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
      ))

    proxy = dataTableProxy('x1')

    observe({
      replaceData(proxy, loopData(), resetPaging = FALSE)
    })

    output$x2 = renderPrint({
      data.frame(Like = shinyValue('cb_', n))
    })
  }
)



EDIT: I adapted @Stéphane Laurent's solution to my use, but there is a slight vertical alignment issue.
enter image description here

Upvotes: 1

Views: 1212

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84699

But I need to only be able to select a single row.

In this case I would not use checkboxes, but radio buttons instead.

Is it OK like this:

library(shiny)
library(DT)

n <- 6
dat <- data.frame(
  Select = sprintf(
    '<input type="radio" name="rdbtn" value="%s"/>', 1:n
  ),
  YN = rep(FALSE, n),
  ID = 1:n,
  stringsAsFactors = FALSE
)

callback <- c(
  "$('input[name=rdbtn]').on('click', function(){",
  "  var value = $('input[name=rdbtn]:checked').val();",
  "  Shiny.setInputValue('rdbtn', value);",
  "});"
)

shinyApp(
  ui = fluidPage(
    title = "Radio buttons in a table",
    DTOutput("foo"),
    h3("Selected row:"),
    verbatimTextOutput("sel")
  ),
  server = function(input, output, session) {
    output[["foo"]] <- renderDT(
      dat, escape = FALSE, selection = 'none', server = FALSE,
      options = list(dom = 't', paging = FALSE, ordering = FALSE),
      callback = JS(callback)
    )
    output[["sel"]] <- renderPrint({
      input[["rdbtn"]]
    })
  }
)

EDIT

Here is a possibility using checkboxes with the Select extension:

library(shiny)
library(DT)

dat <- iris[1:6,]

callback <- c(
  "table.on('select', function(e, dt, type, indexes){",
  "  if(type === 'row'){",
  "    Shiny.setInputValue('selectedRow', indexes);",
  "  }",
  "});"
)

ui <- fluidPage(
  br(),
  DTOutput("tbl"),
  br(),
  h3("Selected row:"),
  verbatimTextOutput("selectedRow")
)

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

  output[["tbl"]] <- renderDT({
    datatable(dat, extensions = "Select", callback = JS(callback), 
              options = list(
                columnDefs = list(
                  list(targets = 0, orderable = FALSE, className = "select-checkbox")
                ),
                select = list(
                  style = "single", selector = "td:first-child"
                )
              )
    )
  })

  output[["selectedRow"]] <- renderPrint({
    input[["selectedRow"]] + 1
  })

}

shinyApp(ui, server)

Upvotes: 1

Related Questions