我应该如何创建一个 "clear" 按钮来消除 R shiny 中的行?

How I should create a "clear" button for eliminating rows in R shiny?

我指的是我之前的post:while updating the datatable in R shiny, how to make column inputs necessary fields using "validate" and "need" in R shiny

我正在制作一个 R shiny 应用程序,用户可以在其中通过单击“添加”按钮并将数据保存到 .xpt 文件来添加大量行。截至目前,一切正常。

我只是卡在下面:

然而,为了扩展这个应用程序,我在服务器功能中添加了“清除”按钮,以及'reactiveValues',这样当用户点击它时,所有以前的行(添加的行)都已删除(清除),应用程序已准备好接受新行。我仍然可以添加行,但 clear 选项不起作用并且不会给我任何错误。有没有人可以帮助我?

代码

library(shiny)
library(stringr)
library(shinydashboard)
library(tidyverse)
library(DT)
library("SASxport")

ui <- fluidPage(
  fluidRow(tabsetPanel(id='tabs', 
                       tabPanel("Tab1",
                                div(id = "form", 
                                    textInput("schoolId", label="SchoolId *" ),
                                    selectInput("userId", label="UserId", choices = c("UserA", "UserB", "UserC"),selected = "UserA"), 
                                    textInput("class", label = "class"), 
                                    selectInput("result", label="result", choices = c("PASS", "FAIL" )),
                                    #dateInput("resultdate", value = NA, label = "Date of the result / Remarks for fail"
                                              #, format = "yyyy-mm-dd" )
                                ),
                                actionButton("add", "Add"),
                                actionButton("clear", "Clear")
                       ), 
                       tabPanel("Tab2", 
                                tabPanel("View", 
                                         conditionalPanel("input.add != 0", 
                                                          DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
                                )
                       )
  )
  )
)

server <- function(input, output, session) {
  store <- reactiveValues()
  
  observeEvent(input$add,{
    new_entry <- data.frame(SCHOOLID=input$schoolId, USERID=input$userId
                            , CLASS= input$class
                            , RESULT=input$result
                            )
   # new_entry <- data.frame(SCHOOLID=input$schoolId, USERID=input$userId
                           # , CLASS= input$class
                            #, RESULT=input$result,
                            #RESULT_DATE = input$resultdate)
    
    if("value" %in% names(store)){
      store$value<-bind_rows(store$value, new_entry)
    } else {
      store$value<-new_entry
    }
    # If you want to reset the field values after each entry use the following two lines
    for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
    updateSelectInput(session, "userId", selected = "UserA")
    updateSelectInput(session, "result", selected = "PASS")
    # updateDateInput(session, "resultdate")
  })
  output$DT2 <- renderDT({
    store$value
  })
  

  
  output$downloadData <- downloadHandler(
    filename = paste0("mydata", ".xpt"),
    content = function(file){
      write.xport(store$value, file = file)
    }
  )
  new_frame <- reactive({
    store$value
  })
  
  #function allows to clear the rows
  values <- reactiveValues(df_data = new_frame)
  
  observeEvent(input$clear,{
    
    if (!is.null(input$table1_rows_selected)) {
      
      values$df_data <- values$df_data[-as.character(input$table1_rows_selected),]
    }
  })
  
  output$Tab2 <- renderDataTable({
    values$df_data
  })
  
}

shinyApp(ui, server)

这里的问题是在如何获取所选行以进行删除方面存在轻微疏忽。不是从 DT table 中获取选定的行,而是必须直接从 ui 元素中获取它们,即 DT2

此外,您可以直接在服务器中创建的设备标识符 table 上工作,而不是存储新的反应值

这是修改后的(相关的)服务器代码:

  #xxxxxxxx this   not needed 
  #values <- reactiveValues(df_data = new_frame)
  
  observeEvent(input$clear,{
   
    if (!length(input$DT2_rows_selected)==0) {
  #work directly on store variable      
      store$value<- store$value[-as.numeric(input$DT2_rows_selected),]
    }       
    
    
  })

我已经对此进行了测试并且有效。如果需要,可以 post 整个应用程序代码。