闪亮的动态 DT 前 select 行
Pre-select rows of a dynamic DT in shiny
这个问题是 的动态版本。
我在闪亮的应用程序中有一个 DT,它在初始化时可能是空的。我想预先 select 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())))
)
}
)
它 select 正确,但开头有 Warning: Error in seq_len: argument must be coercible to non-negative integer
错误。我认为那是因为 seq_len
不能接受 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()))
}
)
但是这不起作用。我尝试了另一个使用修改后的 seq_len
的版本,但它也不起作用。
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())))
)
}
)
如何消除第一个版本中的错误,或使第二个、第三个版本正常工作?
要允许对选定的评估做出反应,您需要从 renderDataTable
:
中调用 datatable
output$x1 = renderDataTable(
datatable( data(),
selection = list(mode = 'multiple', selected = all_rows())),
server = FALSE)
这个问题是
我在闪亮的应用程序中有一个 DT,它在初始化时可能是空的。我想预先 select 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())))
)
}
)
它 select 正确,但开头有 Warning: Error in seq_len: argument must be coercible to non-negative integer
错误。我认为那是因为 seq_len
不能接受 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()))
}
)
但是这不起作用。我尝试了另一个使用修改后的 seq_len
的版本,但它也不起作用。
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())))
)
}
)
如何消除第一个版本中的错误,或使第二个、第三个版本正常工作?
要允许对选定的评估做出反应,您需要从 renderDataTable
:
datatable
output$x1 = renderDataTable(
datatable( data(),
selection = list(mode = 'multiple', selected = all_rows())),
server = FALSE)