user
user

Reputation: 592

Select a row in data Table

I have a data table same as the following picture and when I click on a random row, the app will be crashed and I receive an error message of below:

Data Table

Warning in widgetFunc() :
  renderDataTable ignores ... arguments when expr yields a datatable object; see ?renderDataTable
Warning: Unhandled error in observer: (list) object cannot be coerced to type 'logical'
observeEvent(input$responses_rows_selected)
ERROR: [on_request_read] connection reset by peer

I am not sure what this error message means.

server.r

CreateDefaultRecord <- function() {
  mydefault <- CastData(list(id = "0", name = "", used_shiny = FALSE, r_num_years = 4))
  return (mydefault)
}


 CastData <- function(data) {
   datar <- data.frame(name = data["name"], 
                       used_shiny = as.logical(data["used_shiny"]), 
                       r_num_years = as.integer(data["r_num_years"]),
                       stringsAsFactors = FALSE)

   rownames(datar) <- data["id"]
   return (datar)
 }

 UpdateInputs <- function(data, session) {
   updateTextInput(session, "id", value = unname(rownames(data)))
   updateTextInput(session, "name", value = unname(data["name"]))
   updateCheckboxInput(session, "used_shiny", value = as.logical(data["used_shiny"]))
   updateSliderInput(session, "r_num_years", value = as.integer(data["r_num_years"]))
 }

         # Select row in table -> show details in inputs
      observeEvent(input$responses_rows_selected, {
        if (length(input$responses_rows_selected) > 0) {
          data <- ReadData()[input$responses_rows_selected, ]
          UpdateInputs(data, session)
        }

      })

  # display table 

      output$responses <- DT::renderDataTable({

        #update after submit is clicked
        input$submit
        #update after delete is clicked
        input$delete
        DT::datatable(ReadData(), options = list(pageLength=3),colnames = c('ID' = 1), class = 'cell-border stripe')
      }, server = FALSE, selection = "single",
      colnames = unname(GetTableMetadata()$fields)[-1]
      ) 

Upvotes: 0

Views: 421

Answers (2)

user
user

Reputation: 592

The Problem is because of multiple selection of rows in a datatable. I just add

selection = "single"

in the DT::datatable()

 DT::datatable(ReadData(),extensions = 'Responsive', options = list(pageLength=3),colnames = c('ID' = 1), class = 'cell-border stripe', selection = "single")

Upvotes: 1

Vongo
Vongo

Reputation: 1434

Can you check the type of input$responses_rows_selected ? In your data <- ReadData()[input$responses_rows_selected, ] , input$responses_rows_selected is neither a logical or numeric vector telling R how to subset your table. Maybe Shiny is just treating input$responses_rows_selected as a character vector (as it often does), or quite surprisingly, as the error mentions ((list) object cannot be coerced to type 'logical'), as a list.

Upvotes: 1

Related Questions