编辑后的数据表行焦点

时间:2018-07-18 07:10:15

标签: r shiny dt

我在一个闪亮的应用程序中使用了datatables包,并且它运行良好,但是当我编辑一个单元格/值时,焦点从当前行更改为第一行。该表很长,因此必须回滚到正在编辑的行是一个问题。有没有选择特定行的方法?那么在重新加载表格后,我可以返回到正在编辑的行。

这是数据表以闪亮方式呈现的代码。有什么建议吗?

output$book_table <- DT::renderDT(RVTables$book %>% 
                                  filter(deal == as.numeric(input$deal_choice)),
                                  selection = 'single',
                                  editable = TRUE,
                                  rownames = FALSE,
                                  options = list(
                                  autoWidth = TRUE,
                                  ordering = FALSE,
                                  pageLength = 12,
                                  scrollX = TRUE,
                                  scrollY = TRUE,
                                  bLengthChange = FALSE,
                                  searching = FALSE
                                  )
)

修改

我找到了选择行的方法,但是我无法动态更新它。

row_edit<-3
    output$book_table <- DT::renderDT(RVTables$book %>% 
                                      filter(deal == as.numeric(input$deal_choice)),
                                      selection = list(mode='single',selected=row_edit)
                                      editable = TRUE,
                                      rownames = FALSE,
                                      options = list(
                                      autoWidth = TRUE,
                                      ordering = FALSE,
                                      pageLength = 12,
                                      scrollX = TRUE,
                                      scrollY = TRUE,
                                      bLengthChange = FALSE,
                                      searching = FALSE
                                      )
    )

并在编辑事件中使用全局分配也无效:

row_edit<<- 22

编辑
这是一个示例应用程序,用于显示我要执行的操作。 例如,编辑第42行,看看高亮显示的下一行如何回到第一页,如果您要多次编辑单个值以查看效果,这很烦人。

library(shiny)
library(shinyjs)
library(tidyverse)
library(DT) #load after shiny

data_input <- data.frame(ID=seq(1,50),
                   weight=sample(50:65,50,replace = TRUE),
                   height=sample(150:225,50,replace = TRUE)
)

#shiny---
shinyApp(

  #ui----
  ui= fluidPage(
    fluidRow(
      br(),
      column(2,
             actionButton("increase_weight","increase weight"),
             uiOutput("show_row_selected"),
             uiOutput("last_row_selected")
      ),
      column(4,DT::DTOutput("data_table"))

    )
  ),


  server=function(input,output,session){

    #save data frame as a reactive value
    RV <- reactiveValues(
      data=data_input
    )

    #try to save last row selected----
    row_select <- reactive({

      #case when code below fails, use row 3 to illustrate code
      3

      #uncomment code below and run to see error
      # case_when(
      #   #valid row selected
      #   length(input$data_table_rows_selected)>0 ~ as.numeric(input$data_table_rows_selected),
      #   #after update, row object is empty to use last selected row
      #   #this is now recursive and app fails - "evaluation nested too deeply: infinite recursion...."
      #   length(input$data_table_rows_selected)==0 ~ row_select()
      # )

    })

    #render data frame for output
        output$data_table <-  DT::renderDT(RV$data,
                                       selection = list(mode="single",selected=row_select()),
                                       editable = TRUE,
                                       rownames = FALSE,
                                       options=list(
                                         autoWidth=TRUE,
                                         scrollX = TRUE,
                                         ordering=FALSE,
                                         pageLength=12,
                                         scrollY = TRUE,
                                         bLengthChange= FALSE,
                                         searching=FALSE
                                       )
    )

        #edit a single cell----
        observeEvent(input$data_table_cell_edit, {
          info <-  input$data_table_cell_edit
          edit_row <-  info$row
          edit_col <-  info$col+1    # column index offset by 1
          edit_value <-  info$value

          #find leg to be edited
          ID <- edit_row
          RV$data[ID,edit_col] <-as.numeric(edit_value)

        })


        #increase weight by one----
        observeEvent(input$increase_weight, {
          edit_row <- input$data_table_rows_selected

          RV$data[edit_row,"weight"] <- RV$data[edit_row,"weight"]+1
        })

        #show current row selected----
        output$show_row_selected <- renderText({
          paste0("row selected:  ",as.character(as.numeric(input$data_table_rows_selected)))
        })

        #show last row selected----
        output$last_row_selected <- renderText({
          paste0("row selected:  ",as.character(row_select()))
        })

  })

1 个答案:

答案 0 :(得分:0)

this问题的帮助下,这是一些有效的代码。

{
"id": 89,
"accountNo": "000000089",
 "status": {
"id": 300,
"code": "clientStatusType.active",
"value": "Active"
 },
 "subStatus": {
"active": false,
"mandatory": false
 },
"active": true,
"activationDate": [
  2018,
  8,
 13
 ],
 "firstname": "Albertina",
 "lastname": "Ben-Patterson",
 "displayName": "Albertina Ben-Patterson",
 "mobileNo": "0201234598",
 "gender": {
  "active": false,
  "mandatory": false
 },
 "clientType": {
 "active": false,
 "mandatory": false
  },
  "clientClassification": {
"active": false,
"mandatory": false
 },
"isStaff": false,
 "officeId": 10,
 "officeName": "Greater Accra Region",
 "timeline": {
 "submittedOnDate": [
  2018,
  8,
  13
 ],
"submittedByUsername": "admin",
"submittedByFirstname": "App",
"submittedByLastname": "Administrator",
"activatedOnDate": [
  2018,
  8,
  13
],
"activatedByUsername": "admin",
"activatedByFirstname": "App",
"activatedByLastname": "Administrator"
},
"savingsAccountId": 8,
"groups": [],
"clientNonPersonDetails": {
 "constitution": {
  "active": false,
  "mandatory": false
 },
  "mainBusinessLine": {
  "active": false,
  "mandatory": false
 }
}
}