如何解决RShiny:dataTableProxy:selectPage 未导航到任何页面:“页面索引超出范围”
在开始之前,我曾尝试咨询 this question、this question 和 dataTableProxy()
的文档。
我目前正在尝试创建一个基本的 RShiny 应用程序,该应用程序在加载时自动导航到给定页面并选择给定行(稍后我计划让应用程序基于 GET 查询字符串执行此操作,但是我目前使用这个基本版本作为起点)。有问题的表格有 32 行和 4 页。与我咨询的问题形成对比的是,它们是通过事件触发器来实现的,而我想在文档加载时这样做。
使用 selectRows()
和 selectPage()
选择行和页面非常简单。 selectRows()
提供了预期的结果;但是,selectPage()
似乎对我不起作用,会在浏览器控制台中抛出 'page index out of range'。在我的示例代码中,我选择第 25 行并导航到页面索引 2。即使尝试选择页面索引 0 也会引发 'out of range' 错误。
我在下面提供了我的应用程序代码。我想知道我是否要在表格分页之前导航到某个页面?
非常感谢任何输入。
编辑:我有办法既选择行又转到页面,如下所示。但是,即使我转到页面索引 2,仍然显示页面索引 0 的内容。所以我必须点击到另一个页面,然后回到页面索引 2 才能看到我选择的行。
~卡伦
library(DT)
library(shiny)
library(shinyjs)
ui <- fluidPage(
tags$head(tags$script(src="datatables.min.js")),fluidRow(
DT::dataTableOutput("mtcar_table")
)
)
server <- function(input,output,session){
which_page <<- 0
which_row <<- 0
observe({
which_row <<- 25
which_page <<- floor(which_row/10)
output$mtcar_table = DT::renderDataTable({
# I kNow here I can normalize
# by the table state's rows per page attribute
# but want to just get this base exmaple
# to work first
return(
mtcars
)
},options = list(
initComplete = JS(
"function(settings,json){","$(this.api().table().page(2).draw('page'));","console.log(this); return this;}"
)
)
)
# making sure I'm still recording the row
# and page number
print(which_row)
print(which_page)
# manipulating the table here to navigate
# to the right page and row.
# I constantly get "page index out of range"
# even if I put down page {0,1}. I'm wondering
# if I'm trying to navigate to the page before
# the pagination actually takes place?
dataTableProxy("mtcar_table") %>%
selectRows(which_row)
})
}
shinyApp(ui,server)
解决方法
加载后,您可以像这样使用通过 options
传递给 selection
的 ...
和 renderDataTable
参数:
library(DT)
library(shiny)
ui <- fluidPage(
tags$head(tags$script(src="datatables.min.js")),fluidRow(
DT::dataTableOutput("mtcar_table")
)
)
server <- function(input,output,session){
output$mtcar_table <- renderDataTable({
mtcars
},options = list(displayStart = 20),selection = list(mode = 'multiple',selected = c(25),target = 'row') )
}
shinyApp(ui,server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。