R语言 如何在Shiny中重置选定行

w46czmvw  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(147)

我有一个小rshiny应用程序,在其中我可以选择行在datatable和获得值从第一列。
但是如何快速删除选中的行和值,而不用再次单击该行呢?
另外,如果你知道在这段代码中有什么可以改进的地方,那么就写,我刚开始用R编码

# Define UI
ui <- fluidPage(
  dataTableOutput('main_information'),
  fluidRow(
    column(8,verbatimTextOutput('selected_rows', placeholder = TRUE)),
    fluidRow(
      column(4,actionButton("reset", "RESET"))
    )
  )
)

# Define server function
server <- function(input, output,session) {
  getScoreTable<-reactive({
    db <- dbConnect(SQLite(), "path")
    data <- dbGetQuery(
      conn = db,
      statement = 
        '...'
    )
  })
  
  output$main_information <- renderDataTable(
    getScoreTable(),
    options = list(
      pageLength = 5,
      lengthMenu = list(c(5,10, 25, 50, 100), 
                        c('5', '10', '25','50', '100'))
    )
  )
  
  s<-reactiveValues(data= NULL)
  
  output$selected_rows = renderPrint({
    s = input$main_information_rows_selected
    if (length(s)) {
      cat('These values were selected:\n\n')
      cat(getScoreTable()[s,1], sep = '\n')
    }else{
      cat('No value has been selected')
    }
  })
  
  
}

# Create Shiny object
shinyApp(ui = ui, server = server)
4ioopgfo

4ioopgfo1#

您可以使用自定义操作按钮:

library(DT)

js <- "
function ( e, dt, node, config ) {
  dt.rows().deselect();
}
"

datatable(
  iris,
  extensions = c("Buttons", "Select"),
  selection = "none",
  options = list(
    "dom"     = "Bfrtip",
    "select"  = TRUE,
    "buttons" = list(
      list(
        "extend" = "collection",
        "text"   = "DESELECT",
        "action" = JS(js)
      )
    )
  )
)

这个例子运行良好。如果你在Shiny中有问题,请提供最少的可重复代码,不要使用SQL。

相关问题