首页 > 解决方案 > 具有持久数据表的可编辑闪亮 DT 不显示过滤记录

问题描述

什么不正常:在过滤数据时,当我编辑一些值时,它会显示“未找到匹配的记录”。理想情况下,我应该能够对过滤后的数据进行编辑,并且记录应该保持存在。 编辑时不过滤;然后它工作正常。但是在对过滤后的数据进行编辑时,它不起作用。 下面我附上了截图以使其更清晰。谢谢。

    library(shiny)  # v. 1.2.0
    library(DT)  # v. 0.5
    
    #page_length <- 2 # 5 elements should span 3 pages
    
    hardcoded_df <- read.table(text = "Fruit Color
                                       Apple Red
                                       Plum Purple
                                       Blueberry Duh
                                       Orange Carrot
                                       Crocodile Green
                                       Grapes Green",
                               header = TRUE,
                               stringsAsFactors = FALSE)
    
    ui <- fluidPage(
      DT::dataTableOutput('x1')
    )
    
    server <- function(input, output, session) {
      
    
    
      x <<- reactiveValues()
      x$df <<- hardcoded_df
      DT=isolate(x$df)
      
      
      #Code to convert Factor
      DT <- as.data.frame(unclass(DT))
    
      output$x1 = DT::renderDataTable(datatable(DT, plugins = "ellipsis",
                                                class = 'white-space: nowrap',
                                                filter = list(position = 'top', clear = TRUE, plain = TRUE) ,
                                                extensions =  c('Buttons','AutoFill','FixedHeader', 'KeyTable','ColReorder'), editable = list(
                                                  target = 'cell'
                                                ), selection = "single",escape = FALSE ,
                                                options = list(
                                                  keys = TRUE, colReorder = list(realtime = FALSE),
                                                  fixedHeader = TRUE, autoFill = list(focus = 'click', horizontal = FALSE) , 
                                                  autoWidth=TRUE, pageLength = 2 ,editable = TRUE,
                                                  lengthMenu = list(c(2, 50, -1), c('2', '50', 'All')),  dom = 'lBfrtip',buttons =                                                                                                                 list(
                                                    c('colvis','pdf','excel'),
                                                    list(
                                                      extend = "collection",
                                                      text = 'Show All',
                                                      action = DT::JS("function ( e, dt, node, config ) { dt.page.len(-1);
                                                       dt.ajax.reload();
        }"))))
                                              
      ))
      
      proxy = dataTableProxy('x1')
      
      data = reactiveValues()
      observeEvent(input$x1_cell_edit, {
        info = input$x1_cell_edit
        str(info)
        # str(input$x1_state)
        i = info$row
        j = info$col
        v = info$value
        
        View(x$df)
        # Without this line the table does not change but with it it jumps to row 1 after an edit.
        x$df[i, j] <- (DT::coerceValue(v, x$df[i, j]))
        DT::replaceData(proxy, x$df, resetPaging = FALSE)  # important
        # Now we need to scroll to row i somehow ... clearly this does not work. Help!
        #selectPage(proxy, ceiling(i / page_length))
        # selectRow(proxy, i)
      })
    }
    
    shinyApp(ui = ui, server = server)

下图显示-从颜色列中过滤“绿色”值 过滤

编辑 Fruit 列中的值 编辑水果列的值

收到无记录消息 没有记录

标签: rshinydt

解决方案


推荐阅读