在按下actionButton后在表格中添加行时,删除行在闪亮的应用程序中不起作用

Posted

技术标签:

【中文标题】在按下actionButton后在表格中添加行时,删除行在闪亮的应用程序中不起作用【英文标题】:While adding rows in a table works after pressing actionButton deleting rows does not in a shiny app 【发布时间】:2022-01-07 22:47:33 【问题描述】:

在下面闪亮的应用程序中,每次按下Add 按钮时,我都可以在表格中添加一个新行,但是当我按下相关按钮删除行时,我的应用程序崩溃了。

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("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"
    )
  
)

【问题讨论】:

【参考方案1】:

因为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() 函数。

【讨论】:

以上是关于在按下actionButton后在表格中添加行时,删除行在闪亮的应用程序中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

当用户点击加号按钮插入行时 UITableView 响应

模块化和 SelectInput 使 actionButton 重复

Vue.js - 在悬停时添加删除按钮并在按下按钮时删除它

如何在按下的数字上显示文本字段上星号的确切数量,而不是在按下输入时添加额外的星号?

Oracle Apex 5.1 在使用进程按下按钮后运行 PL/SQL

iPhone如何在按下音量键时隐藏音量覆盖