在数据表中选择一行

时间:2015-11-26 00:00:49

标签: r shiny shinydashboard

我有一个与下图相同的数据表,当我点击随机行时,应用程序将崩溃并收到以下错误消息:

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

我不确定此错误消息的含义。

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]
      ) 

2 个答案:

答案 0 :(得分:1)

您可以查看input$responses_rows_selected的类型吗? 在data <- ReadData()[input$responses_rows_selected, ]中,input$responses_rows_selected既不是逻辑数字向量,也不是数字向量,告诉R如何对表进行子集化。 也许Shiny只是将input$responses_rows_selected视为一个字符向量(就像它经常那样),或者非常令人惊讶地,因为错误提到((list) object cannot be coerced to type 'logical'),作为列表。

答案 1 :(得分:1)

问题是因为数据表中有多个行选择。 我只是添加

selection = "single"
DT :: datatable()

中的

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