R Shiny observeEvent issues

前端 未结 1 1300
情话喂你
情话喂你 2021-01-24 16:49

I am trying to delete the rows from a dataframe when they have been selected in the datatable and someone presses the \"Delete Rows\" switch. input$click_rows_selected gives the

相关标签:
1条回答
  • 2021-01-24 17:22

    The following code should help you toward a solution. Please note that the practice to have nested observe should be in general avoided.

    I've added updateCheckboxGroupInput as I thought it made sense in the context of the example.

    library(shiny)
    
    values <- reactiveValues(df = iris)
    
    ui <- fluidPage( 
    
      sidebarLayout(
    
        sidebarPanel(
          checkboxGroupInput('inputId', label=NULL, choices = colnames(df), selected = NULL,
               inline = FALSE, width = NULL, choiceNames = NULL, choiceValues = NULL),
          actionButton("deleterows", "push to delete") 
        ),
    
        mainPanel(tableOutput("contents")
        )
      ))
    
    server <- function(input,output,session){
    
      observeEvent(input$deleterows,{
        cols <- setdiff(colnames(values$df), input$inputId)
                        values$df <- values$df[c(cols)]
    
                        updateCheckboxGroupInput(session, 'inputId', label = NULL, choices = colnames(values$df),
                          selected = NULL, inline = FALSE, choiceNames = NULL,
                          choiceValues = NULL)
     })
    
    output$contents <- renderTable({
            values$df 
      })
    
    }
    
    shinyApp(ui,server)
    
    0 讨论(0)
提交回复
热议问题