简体   繁体   English

在Shiny R中重置DT :: renderDataTable()的行选择

[英]Reset row selection for DT::renderDataTable() in Shiny R

I reproduced an example shiny app written by Yihui Xie ( https://yihui.shinyapps.io/DT-rows/ ). 我再现了由Yihui Xie编写的一个闪亮的应用程序示例( https://yihui.shinyapps.io/DT-rows/ )。 The app uses DT::renderDataTable() which allows a row selection. 该应用程序使用DT::renderDataTable() ,允许行选择。

Everything works perfectly fine. 一切都很好。 I was however wondering if it's possible to reset the row selection (ie undo the click selection) ? 然而,我想知道是否可以重置行选择(即撤消点击选择)? I already tried it with an action button to reset s = input$x3_rows_selected (see script below). 我已经尝试了一个操作按钮来重置s = input$x3_rows_selected (参见下面的脚本)。

With my current script, s = input$x3_rows_selected does indeed get emptied, I can however not refill it. 使用我当前的脚本, s = input$x3_rows_selected确实会被清空,但我可以不重新填充它。 Also the selected rows are still clicked (shaded) 此外,所选行仍然被点击(阴影)

Does anyone has an idea? 有没有人有想法? Is there an option within DT::renderDataTable() to reset the selection? DT :: renderDataTable()中是否有一个选项来重置选择? Or does anyone has an idea for a workaround? 或者有没有人有解决方法的想法?

Thank you! 谢谢!

Example form https://yihui.shinyapps.io/DT-rows/ ) with my modification (action button): 我的修改(操作按钮)示例表单https://yihui.shinyapps.io/DT-rows/ ):

server.R server.R

library(shiny)
library(DT)

shinyServer(function(input, output, session) {


    # you must include row names for server-side tables
    # to be able to get the row
    # indices of the selected rows
    mtcars2 = mtcars[, 1:8]
    output$x3 = DT::renderDataTable(mtcars2, rownames = TRUE, server = TRUE)

    # print the selected indices

    selection <- reactive({
        if (input$resetSelection) 
            vector() else input$x3_rows_selected
    })

    output$x4 = renderPrint({

        if (length(selection())) {
            cat("These rows were selected:\n\n")
            output <- selection()
            cat(output, sep = "\n")
        }
    })

})

ui.R ui.R

library(shiny)
shinyUI(
    fluidPage(
        title = 'Select Table Rows',

        h1('A Server-side Table'),

        fluidRow(
            column(9, DT::dataTableOutput('x3')),
            column(3, verbatimTextOutput('x4'),
               actionButton('resetSelection',
                   label = "Click to reset row selection"
                             ) # end of action button

              ) #end of column
)))

In the current development version of DT (>= 0.1.16), you can use the method selectRows() to clear selections. DT当前开发版本 (> = 0.1.16)中,您可以使用方法selectRows()来清除选择。 Please see the section "Manipulate An Existing DataTables Instance" in the documentation . 请参阅文档中的“操作现有DataTables实例”部分。

Here is a possible solution, maybe not the best but it works. 这是一个可能的解决方案,可能不是最好的,但它的工作原理。 It is based on re-create the datatable each time the action button is clicked, so the selected rows are removed. 它基于每次单击操作按钮时重新创建数据表,因此将删除所选行。

library(shiny)
library(DT)
runApp(list(
    server = function(input, output, session) {
        mtcars2 = mtcars[, 1:8]
        output$x3 = DT::renderDataTable({
            # to create a new datatable each time the reset button is clicked
            input$resetSelection 
            mtcars2
            }, rownames = TRUE, server = TRUE
        )

        # print the selected indices
        selection <- reactive ({
                input$x3_rows_selected
        })

        output$x4 = renderPrint({
            if (length(selection())) {
                cat('These rows were selected:\n\n')
                output <- selection()
                cat(output, sep = '\n')
            }
        })
    },
    ui = shinyUI(fluidPage(
        title = 'Select Table Rows',
        h1('A Server-side Table'),
        fluidRow(
            column(9, DT::dataTableOutput('x3')),
            column(3, verbatimTextOutput('x4'),
                actionButton(  'resetSelection',label = "Click to reset row selection")
            ) #end of column
        )
    ))
))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM