在闪亮应用程序的 DT::datatable 中添加、删除和编辑行

Posted

技术标签:

【中文标题】在闪亮应用程序的 DT::datatable 中添加、删除和编辑行【英文标题】:Add, remove and edit rows in a DT::datatable of a shiny app 【发布时间】:2022-01-08 03:58:03 【问题描述】:

我有下面的闪亮应用程序,我可以根据闪亮的小部件选择通过按Add 添加新行,我可以通过按Delete 选择和删除一行,我想与它们结合功能单击一行,然后在按下Edit 后通过左侧边栏中的相关小部件更改该行的选定列的值。例如,如果我单击第二行,然后将 Security Type 小部件从 Stock 更改为 Load Fund,则第二行的 Security Type 列应变为 Load Fund

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(tibble)
Input <- structure(list(`Security Type` = c("Stock", "Stock", "Load Fund"), Ticker = c("XOM", "NFLX", "AMCPX"), `Purchase Date` = structure(c(
  16070,
  17084, 17084
), class = "Date"), `Sale Date` = structure(c(
  18627,
  NA, 18545
), class = "Date"), `Amount Invested` = c(
  "$10,000",
  "$8,000", "$10,000"
)), class = c(
  "spec_tbl_df", "tbl_df", "tbl",
  "data.frame"
), row.names = c(NA, -3L))
shinyApp(
  ui = tags$body(class = "skin-blue sidebar-mini control-sidebar-open", dashboardPage(
    options = list(sidebarExpandOnHover = TRUE),
    header = dashboardHeader(title = "Investment Advisor Monitoring - Insider Trading", titleWidth = 450),
    sidebar = dashboardSidebar(
      minified = F, collapsed = F,
      selectInput(
        "sectype", "Security Type",
        c(unique(Input$`Security Type`))
      ),
      selectInput(
        "sectick", "Ticker",
        c(unique(Input$Ticker))
      ),
      dateInput("PurDate", "Purchase Date", value = as.Date("2013-12-31")),
      dateInput("selDate", "Sale Date", value = as.Date("2019-01-31")),
      selectInput(
        "aminv", "Amount Invested",
        c(unique(Input$`Amount Invested`))
      ),
      actionButton("add", "Add"),
      actionButton("edit", "Edit"),
      
      actionButton("deleteRows", "Delete Rows")
      
    ),
    body = dashboardBody(
      h3("Results"),
      tabsetPanel(
        id = "tabs",
        tabPanel(
          "InsiderTraining",
          dataTableOutput("TBL1")
        )
      )
    ),
    controlbar = dashboardControlbar(width = 300),
    title = "DashboardPage"
  )),
  server = function(input, output) 
    # Init with some example data
    data <- reactiveVal(Input)
    rv <- reactiveValues(df = Input, row_selected = NULL)
    
    observeEvent(
      input$add,
      
        # start with current data
        data() %>%
          add_row(
            `Security Type` = isolate(input$sectype),
            Ticker = isolate(input$sectick),
            `Purchase Date` = isolate(input$PurDate),
            `Sale Date` = isolate(input$selDate),
            `Amount Invested` = isolate(input$aminv)
          ) %>%
          # update data value
          data()
      
    )
    observeEvent(input$deleteRows,
      
      if (!is.null(input$TBL1_rows_selected)) 
        data(data()[-as.numeric(input$TBL1_rows_selected),])
        
      
    )
    observeEvent(input$edit,
      
      if (!is.null(input$TBL1_rows_selected)) 
        cols_to_edit <- c('sectype', 'sectick', 'PurDate', 'selDate', 'aminv')
        colnms <- c('Security Type', 'Ticker', 'Purchase Date', 'Sale Date', 'Amount Invested')
        "remember the row selected"
        rv$row_selected <- input$TBL1_rows_selected
        
        walk2(cols_to_edit, colnms, ~rv$df[input$TBL1_rows_selected, ..2] <<- input[[..1]]) 
        
      
      
    )
    output$TBL1 <- renderDataTable(
      data(),selection="single"
    )
  
)

【问题讨论】:

您要继续使用 reactiveVal data() 来存储表格吗? 【参考方案1】:

这是一种使用reactiveValues的方法。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
library(tidyverse)

Input <- structure(list(`Security Type` = c("Stock", "Stock", "Load Fund"), Ticker = c("XOM", "NFLX", "AMCPX"), `Purchase Date` = structure(c(
  16070,
  17084, 17084
), class = "Date"), `Sale Date` = structure(c(
  18627,
  NA, 18545
), class = "Date"), `Amount Invested` = c(
  "$10,000",
  "$8,000", "$10,000"
)), class = c(
  "spec_tbl_df", "tbl_df", "tbl",
  "data.frame"
), row.names = c(NA, -3L))


shinyApp(
  ui = tags$body(class = "skin-blue sidebar-mini control-sidebar-open", dashboardPage(
    options = list(sidebarExpandOnHover = TRUE),
    header = dashboardHeader(title = "Investment Advisor Monitoring - Insider Trading", titleWidth = 450),
    sidebar = dashboardSidebar(
      minified = F, collapsed = F,
      selectInput(
        "sectype", "Security Type",
        c(unique(Input$`Security Type`))
      ),
      selectInput(
        "sectick", "Ticker",
        c(unique(Input$Ticker))
      ),
      dateInput("PurDate", "Purchase Date", value = as.Date("2013-12-31")),
      dateInput("selDate", "Sale Date", value = as.Date("2019-01-31")),
      selectInput(
        "aminv", "Amount Invested",
        c(unique(Input$`Amount Invested`))
      ),
      actionButton("add", "Add"),
      actionButton("edit", "Edit"),
      
      actionButton("deleteRows", "Delete Rows")
      
    ),
    body = dashboardBody(
      h3("Results"),
      tabsetPanel(
        id = "tabs",
        tabPanel(
          "InsiderTraining",
          dataTableOutput("TBL1")
        )
      )
    ),
    controlbar = dashboardControlbar(width = 300),
    title = "DashboardPage"
  )), ###### SERVER
  server = function(input, output) 
    # Init with some example data
    #data <- reactiveVal(Input)
    rv <- reactiveValues(df = Input, row_selected = NULL) 
    
    observeEvent(
      input$add,
      
        # start with current data
        rv$df <- rv$df %>%
          add_row(
            `Security Type` = isolate(input$sectype),
            Ticker = isolate(input$sectick),
            `Purchase Date` = isolate(input$PurDate),
            `Sale Date` = isolate(input$selDate),
            `Amount Invested` = isolate(input$aminv)
          )#  %>%
          # update data value
          #data()
          
        
      
    )
    observeEvent(input$deleteRows,
      
      if (!is.null(input$TBL1_rows_selected)) 
        #data(data()[-as.numeric(input$TBL1_rows_selected),])
        rv$df <- rv$df[-as.numeric(input$TBL1_rows_selected), ]
      
    )
    
    observeEvent(input$edit,
      
      if (!is.null(input$TBL1_rows_selected)) 
        cols_to_edit <- c('sectype', 'sectick', 'PurDate', 'selDate', 'aminv')
        colnms <- c('Security Type', 'Ticker', 'Purchase Date', 'Sale Date', 'Amount Invested')
        "remember the row selected"
        rv$row_selected <- input$TBL1_rows_selected
        
        walk2(cols_to_edit, colnms, ~rv$df[input$TBL1_rows_selected, ..2] <<- input[[..1]]) 
        
      
      
    )
    output$TBL1 <- renderDataTable(
      rv$df,selection="single"
    )
  
)

【讨论】:

我有一个基于您的代码的 Q ***.com/questions/70218070/…

以上是关于在闪亮应用程序的 DT::datatable 中添加、删除和编辑行的主要内容,如果未能解决你的问题,请参考以下文章

闪亮仪表板中 DT::datatable 中的因子下拉过滤器不起作用

DT dataTable中的列总数有光泽

Shiny:合并 DT::datatable 中的单元格

在保持位置的同时更新 Shiny DataTable 的行

DT::DataTables 中第一列的黑色背景和白色字体

DT::datatable 的条纹