虽然在 table 中添加行在按下 actionButton 删除行后有效但在闪亮的应用程序中却没有

While adding rows in a table works after pressing actionButton deleting rows does not in a shiny app

在下面闪亮的应用程序中,每次按下 Add 按钮时,我都能向 table 添加新行,但是当我按下相关按钮删除行时,我的应用程序崩溃了。

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(
  ",000",
  ",000", ",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("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)
    
    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),]
      }
    })
    output$TBL1 <- renderDataTable(
      data(),selection="single"
    )
  }
)

因为data对象是一个函数。这与尝试相同:

sum() <- 2

并且您收到相同的错误消息 - invalid (NULL) left side of assignment

而不是:

data() <- data()[-as.numeric(input$TBL1_rows_selected),]

你想要:

data(data()[-as.numeric(input$TBL1_rows_selected),])

您可以在使用 reactiveVal 时检查函数体,如下所示:

library(shiny)

val <- reactiveVal()

body(val)
#> {
#>     if (missing(x)) {
#>         rv$get()
#>     }
#>     else {
#>         force(x)
#>         rv$set(x)
#>     }
#> }

你可以看到,当你使用val()时,即缺少参数,你得到的是对象,但如果没有缺少参数,你设置了对象。你想设置 data() 这就是你需要将新参数(新数据)传递给 data() 函数的原因。