R Shiny Datatable 中的嵌入式输入 - javascript 问题
Embedded inputs in R Shiny Datatable - javascript issue
我有一个 R/Shiny 应用程序,其中包含来自 DT 包的数据表。
在这个帖子 之后,我已经能够将操作按钮嵌入到我的数据表中的列中,这会触发一组相应的观察者。
但是,当对数据表进行分页时,我的操作按钮只会对第一页上的那些按钮正常工作。后续页面上的按钮不起作用。即使我使用列排序对数据重新排序,情况仍然如此,因为初始渲染时第 2+ 页上的任何按钮即使重新排序到第 1 页也将不起作用。
我预计问题出在回调参数如何使用 javascript(不幸的是我无法理解)来正确呈现操作按钮。谁能建议如何让操作按钮在后续页面上工作?
这是我的最小代表,使用 mtcars 数据:
library(shiny)
library(DT)
ui <- fluidPage(
titlePanel("reprex1")
,fluidRow(
dataTableOutput("dt1")
)
)
server <- function(input, output) {
output$dt1 <- renderDataTable({
mtlocal <- mtcars
for(n in 1:nrow(mtlocal)){
mtlocal$actionbutton[[n]] <- as.character(
actionButton(
paste0("buttonpress",n), label = paste0("buttonpress",n)
)
)
}
datatable(
mtlocal
,escape = FALSE
,selection = "none"
,callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-container');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
}, server = FALSE)
lapply(
1:nrow(mtcars),function(x){
observeEvent(
input[[paste0("buttonpress",x)]],{
showModal(
modalDialog(
h2(paste0("You clicked on button ",x,"!"))
)
)
}
)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
callback
只执行一次,然后table重绘时Shiny.bind/unbind
丢失。您必须使用选项 preDrawCallback
和 drawCallback
:
datatable(
mtlocal
, escape = FALSE
, selection = "none"
, options = list(
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
)
)
我有一个 R/Shiny 应用程序,其中包含来自 DT 包的数据表。
在这个帖子
但是,当对数据表进行分页时,我的操作按钮只会对第一页上的那些按钮正常工作。后续页面上的按钮不起作用。即使我使用列排序对数据重新排序,情况仍然如此,因为初始渲染时第 2+ 页上的任何按钮即使重新排序到第 1 页也将不起作用。
我预计问题出在回调参数如何使用 javascript(不幸的是我无法理解)来正确呈现操作按钮。谁能建议如何让操作按钮在后续页面上工作?
这是我的最小代表,使用 mtcars 数据:
library(shiny)
library(DT)
ui <- fluidPage(
titlePanel("reprex1")
,fluidRow(
dataTableOutput("dt1")
)
)
server <- function(input, output) {
output$dt1 <- renderDataTable({
mtlocal <- mtcars
for(n in 1:nrow(mtlocal)){
mtlocal$actionbutton[[n]] <- as.character(
actionButton(
paste0("buttonpress",n), label = paste0("buttonpress",n)
)
)
}
datatable(
mtlocal
,escape = FALSE
,selection = "none"
,callback = JS("table.rows().every(function(i, tab, row) {
var $this = $(this.node());
$this.attr('id', this.data()[0]);
$this.addClass('shiny-input-container');
});
Shiny.unbindAll(table.table().node());
Shiny.bindAll(table.table().node());")
)
}, server = FALSE)
lapply(
1:nrow(mtcars),function(x){
observeEvent(
input[[paste0("buttonpress",x)]],{
showModal(
modalDialog(
h2(paste0("You clicked on button ",x,"!"))
)
)
}
)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
callback
只执行一次,然后table重绘时Shiny.bind/unbind
丢失。您必须使用选项 preDrawCallback
和 drawCallback
:
datatable(
mtlocal
, escape = FALSE
, selection = "none"
, options = list(
preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
)
)