闪亮

2017-02-06 204 views
1

动态DT的预先选择行这个问题的this更具活力的版本。闪亮

我有闪亮的应用程序一个DT,可能是在初始化空。我想预先选择DT中的所有行。我的第一次尝试是这样的:

library(shiny) 
library(DT) 
shinyApp(
    ui = fluidPage(
    fluidRow(
     radioButtons("select", "", c("none", "iris")), 
     DT::dataTableOutput('x1') 
    ) 
), 
    server = function(input, output, session) { 
    data <- reactive({ 
     if (input$select == "none") { 
     return(NULL) 
     } else if (input$select == "iris"){ 
     return(iris) 
     } 
    }) 
    output$x1 = DT::renderDataTable(
     data(), server = FALSE, 
     selection = list(mode = 'multiple', selected = seq_len(nrow(data()))) 
    ) 
    } 
) 

它没有选择的权利,但有是Warning: Error in seq_len: argument must be coercible to non-negative integer在一开始的错误。我认为这是因为​​不能采取NULL输入。有趣的是,在初始化之后,来回切换不会产生新的错误。

我想这个版本使用的行向量的反应值,与空输入空的结果:

library(shiny) 
library(DT) 
shinyApp(
    ui = fluidPage(
    fluidRow(
     radioButtons("select", "", c("none", "iris")), 
     DT::dataTableOutput('x1') 
    ) 
), 
    server = function(input, output, session) { 
    data <- reactive({ 
     if (input$select == "none") { 
     return(NULL) 
     } else if (input$select == "iris"){ 
     return(iris) 
     } 
    }) 
    all_rows <- reactive({ 
     df <- data() 
     if (is.null(df)) { 
     return(seq_len(0)) 
     } else { 
     return(seq_len(nrow(df))) 
     } 
    }) 
    output$x1 = DT::renderDataTable(
     data(), server = FALSE, 
     selection = list(mode = 'multiple', selected = all_rows())) 
    } 
) 

然而,这是行不通的。我尝试另一个版本,它使用修改​​,但它也不起作用。

library(shiny) 
library(DT) 
seq_len_null_check <- function(len){ 
    if (is.null(len)){ 
    return(integer(0)) 
    } else { 
    return(seq_len(len)) 
    } 
} 
shinyApp(
    ui = fluidPage(
    fluidRow(
     radioButtons("select", "", c("none", "iris")), 
     DT::dataTableOutput('x1') 
    ) 
), 
    server = function(input, output, session) { 
    data <- reactive({ 
     if (input$select == "none") { 
     return(NULL) 
     } else if (input$select == "iris"){ 
     return(iris) 
     } 
    }) 
    output$x1 = DT::renderDataTable(
     data(), server = FALSE, 
     selection = list(mode = 'multiple', selected = seq_len_null_check(nrow(data()))) 
    ) 
    } 
) 

如何删除第一个版本中的错误,或者使第二个,第三个版本工作?

+0

我发现使用'validate'可以删除的第一个错误。但是,当我在两种数据源之间切换时,DT始终选择第一个表的行数。所以当数据源更改DT更新但不会同时更改预选。 – dracodoc

回答

4

为了让所选择的评价反应,你需要调用datatablerenderDataTable内:

output$x1 = renderDataTable(
       datatable(data(), 
         selection = list(mode = 'multiple', selected = all_rows())), 
       server = FALSE) 
+0

谢谢!这解决了错误问题和通过数据问题更新预选。 – dracodoc