预先选择动态DT中闪亮的行 [英] Pre-select rows of a dynamic DT in shiny
的动态版本。 a>。
我在闪亮的应用程序中有一个DT,初始化时可能为空。我想预先选择DT中的所有行。我的第一次尝试是这样的:
library(shiny)
library(DT)
ShinyApp(
ui = fluidPage(
fluidRow(
radioButtons( select,,c( none, iris)),
DT :: dataTableOutput('x1' )
)
),
服务器=函数(输入,输出,会话){
data<-react({
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())))
)
}
)
选择正确,但是有是的错误警告:seq_len中的错误:参数开头必须强制为非负整数
。我认为这是因为 seq_len
不能接受 NULL
输入。有趣的是,初始化之后,来回切换不会产生新的错误。
我尝试使用此版本对行向量使用反应性值,对空输入使用空结果:
库(发光)
库(DT)
ShinyApp(
ui = fluidPage (
fluidRow(
radioButtons( select,,c( none, iris)),
DT :: dataTableOutput('x1')
)
),
服务器=函数(输入,输出,会话){
数据<-反应性({
if(input $ select == none){
return(NULL)
} else if(input $ select == iris){
return(iris)
}
})
all_rows< -反应性({
df<-data()
if(is.null(df)){
return(seq_len(0))
}否则{
return(seq_len(nrow(df)))
}
})
output $ x1 = DT :: renderDataTable(
data(),server = FALSE,
选择= lis t(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')
)
) ,
服务器=函数(输入,输出,会话){
数据<-反应式({
if(input $ select == none){
return(NULL )
}否则,如果(input $ select == iris){
return(iris)
}
})
output $ x1 = DT :: renderDataTable (
data(),server = FALSE,
selection = list(mode ='multiple',selected = seq_len_null_check(nrow(data())))
)
}
)
如何删除第一个v中的错误还是要使用第二,第三版?
要对选定的评估进行响应,您需要调用数据表
来自 renderDataTable
:
output $ x1 = renderDataTable(
datatable(data(),
selection = list(mode ='multiple',selected = all_rows())),
server = FALSE)
This question is the more dynamic version of this.
I have a DT in shiny app that could be empty at initialization. I'd like to pre-select all rows in DT. My first try is like this:
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())))
)
}
)
It did selection right but there is an error of Warning: Error in seq_len: argument must be coercible to non-negative integer
in the beginning. I think that's because seq_len
cannot take an NULL
input. Interestingly after the initialization, switching back and forth doesn't generate new errors.
I tried this version to use an reactive value for rows vector, with empty result for empty input:
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()))
}
)
However this doesn't work. I tried another version which used a modified seq_len
but it also doesn't work.
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())))
)
}
)
How can I remove the error in first version, or make the 2nd, 3rd version work?
To allow reactive on selected evaluation, you need to call datatable
from within renderDataTable
:
output$x1 = renderDataTable(
datatable( data(),
selection = list(mode = 'multiple', selected = all_rows())),
server = FALSE)
这篇关于预先选择动态DT中闪亮的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!