[英]Change pageType in DT pagination in R-Shiny module
Currently, I am designing an R-Shiny module that produces a DT
table that includes paging.目前,我正在设计一个 R-Shiny 模块,该模块生成一个包含分页的DT
表。 By default, DT
default paging type is "simple_numbers"
.默认情况下, DT
默认分页类型为"simple_numbers"
。 My goal is to change the default paging type to "simple"
(see DataTable pagingType Documentation ).我的目标是将默认分页类型更改为"simple"
(请参阅DataTable pagingType 文档)。
While I have not found anything specific to R-Shiny, there have been some SO posts that I found related to my task ( this one being the most helpful ), but, unfortunately, I really have made no progress.虽然我没有找到任何特定于 R-Shiny 的内容,但我发现了一些与我的任务相关的 SO 帖子( 这是最有帮助的),但不幸的是,我确实没有取得任何进展。
My current attempts have been:我目前的尝试是:
shiny::tags$script(
shiny::HTML(paste0('
$(document).ready(function() {
$("#', ns(name), '").dataTable({
"pagingType": "simple",
"sPaginationType": "simple"
});
});')))
But received the following error:但收到以下错误:
jquery.min.js:2 jQuery.Deferred exception: $(...).dataTable is not a function TypeError: $(...).dataTable is not a function
DT::datatable()
object callback
option:我已将其包含在DT::datatable()
对象callback
选项中: callback = DT::JS(paste0('
// * Examine the table
tableObject = table.settings()[0];
console.log(table.settings());
// * Pagination Type
tableObject.sPaginationType = "simple";
'))
However, while nothing errors in the inspector console, the pagination type does not change.然而,虽然检查器控制台中没有任何错误,但分页类型不会改变。
So, at this point, I am a bit perplexed and wondering if someone could help me bridge the gap between the documentation and SO post and my current logic.所以,在这一点上,我有点困惑,想知道是否有人可以帮助我弥合文档和 SO 帖子与我当前逻辑之间的差距。
To help answer this question, I have created a very basic reproducible example of each of my attempts below with pre-loaded data from R.为了帮助回答这个问题,我使用来自 R 的预加载数据创建了一个非常基本的可重复示例,用于说明我在下面的每次尝试。
Attempt 1:尝试 1:
table_ui = function(id, name = "table") {
# * Create a namespace function using the provided id
ns = shiny::NS(id)
# * Build HTML shiny taglist for table view
shiny::tagList(
shiny::tags$script(
shiny::HTML(paste0('
$(document).ready(function() {
$("#', ns(name), '").dataTable({
"pagingType": "simple",
"sPaginationType": "simple"
});
});'))),
DT::dataTableOutput(outputId = ns(name))
)
}
table_server = function(input, output, session, name = "table") {
# * Extract Data ----
data_df = shiny::reactive({ datasets::mtcars })
# * Produce HTML Table ----
# * NOTE: Transform "data_df()" into HTML table
output[[name]] = DT::renderDataTable({
# * Build HTML table
DT::datatable(
data_df(),
rownames = FALSE,
options = list(
paging = TRUE,
pageLength = 5,
dom = "<f<t>ip>"
)
)
})
# * Return
return(data_df)
}
# * Create simple app
# * Define UI
ui = shiny::fluidPage(
table_ui(id = "test", name = "report"),
)
# * Define Server
server = function(input, output, session) {
# * Extract text input
shiny::callModule(
module = table_server,
id = "test",
session = session,
name = "report")
}
# * Build App
shiny::shinyApp(ui = ui, server = server)
Attempt 2:尝试 2:
table_ui = function(id, name = "table") {
# * Create a namespace function using the provided id
ns = shiny::NS(id)
# * Build HTML shiny taglist for table view
shiny::tagList(
DT::dataTableOutput(outputId = ns(name))
)
}
table_server = function(input, output, session, name = "table") {
# * Extract Data ----
data_df = shiny::reactive({ datasets::mtcars })
# * Produce HTML Table ----
# * NOTE: Transform "data_df()" into HTML table
output[[name]] = DT::renderDataTable({
# * Build HTML table
DT::datatable(
data_df(),
rownames = FALSE,
options = list(
paging = TRUE,
pageLength = 5,
dom = "<f<t>ip>"
),
# ** JS Support ----
# * NOTE: Define JS to modify table
callback = DT::JS(paste0('
// * Examine the table
tableObject = table.settings()[0];
console.log(table.settings());
// * Pagination Type
tableObject.sPaginationType = "simple";
'))
)
})
# * Return
return(data_df)
}
# * Create simple app
# * Define UI
ui = shiny::fluidPage(
table_ui(id = "test", name = "report"),
)
# * Define Server
server = function(input, output, session) {
# * Extract text input
shiny::callModule(
module = table_server,
id = "test",
session = session,
name = "report")
}
# * Build App
shiny::shinyApp(ui = ui, server = server)
You don't need to resort to JavaScript.您不需要求助于 JavaScript。 Just add pagingType = "simple"
in the list of options.只需在选项列表中添加pagingType = "simple"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.