我正在尝试使用 shinyBS 函数将带有附加信息的弹出窗口添加到数据表的各个单元格(因为我希望它们是正确的弹出窗口,而不仅仅是“数据表标题属性”)。 ShinyBS::addpopover() 需要元素的元素 ID 来附加弹出窗口。我已经完成了将弹出窗口附加到整个数据表的工作,现在下一步我试图在行级别添加弹出窗口(在移动到单元格级别之前)但我被卡住了。
到目前为止,我的解决方案在很大程度上基于此线程:Insert popify for radiobuttons options
当前问题:使用 rowCallback JS 函数,数据表中的每一行现在都有自己的 ID (tableRow_x),但 ShinyBS::addpopover() 似乎无法识别这些 ID。我怀疑可以在 addpopover() 的 id 参数中添加一些东西来让它在数据表中找到 id,但我一直无法弄清楚是什么。
代表:
注意:在 rstudio 弹出浏览器中运行 shiny 时,有必要在弹出窗口开始显示之前先单击浏览器中的任意位置。
library(shinyBS)
library(shiny)
library(DT)
library(shinyjs) ## needed to tamper with the HTML
ui <- fluidPage(
useShinyjs(),
# need to include at least one bs element in ui
bsTooltip(
"foo",
"This tooltip goes nowhere - it's there to make the tooltips defined with addPopover on the server side work"
) ,
DTOutput("table")
)
server <- function(input, output, session) {
# once the UI is loaded, call shinyBS::addPopover to attach popover to it
session$onFlushed(function() {
addPopover(session = session,
id = "DataTables_Table_0",
title = "information",
content = "this is the popover on id DataTables_Table_0"
)
addPopover(session = session,
id = "tableRow_3",
title = "row information",
content = "this is the popover on id tableRow_3")
})
output$table <-
renderDataTable({
datatable(data = iris,
options = list(
rowCallback = JS(
"function( nRow, aData) {",
"$(nRow).attr('id', 'tableRow_' +aData[0]);",
"}"
)
))
})
}
# Run the application
shinyApp(ui = ui, server = server)
`
它适用于
server=FALSE
和rowId
选项而不是rowCallback
:
output$table <-
renderDT({
datatable(
data = iris,
options = list(
rowId = JS("function(data){return 'tableRow_' + data[0];}")
)
)
}, server = FALSE)
没试过
rowCallback
.
我想在我离开 reprex 领域之前再提供一个更新(以供将来参考)。
下面的版本有一个修改过的 rowCallback 函数来为每行第四列的单元格分配 ID。在 addpopover() 调用中,选项“container = body”是至关重要的,以免弹出窗口弄乱数据表布局(由于将弹出窗口 div 插入数据表)。
ui <- fluidPage(
useShinyjs(),
## need to include at least one bs element in ui
bsTooltip(
"foo",
"This tooltip goes nowhere - it's there to make the tooltips defined with addPopover on the server side work"
) ,
DTOutput("table")
)
server <- function(input, output, session) {
## once the UI is loaded, call shinyBS::addPopover to attach popover to it
session$onFlushed(function() {
addPopover(session,
id = "DataTables_Table_0",
title = "information",
content = "this is the popover on id DataTables_Table_0"
)
addPopover(session,
id = "cellrow8col4",
title = "row information",
trigger = 'hover',
content = "this is the popover on id cellrow8col4",
options = list( container='body')) # container= 'body' makes it so that the popover div doesn't scoot over the next column/mess with the datatable lay-out.
})
output$table <-
renderDataTable({
datatable(data = iris[1:10,],
options = list(
rowCallback = JS(
"function( nRow, aData) {",
"$node_4 = this.api().row(nRow).nodes().to$().find('td:eq(4)')",
"$node_4.attr('id', 'cellrow' +aData[0]+'col'+4)",
"}"
)
))
}, server = FALSE) #server = F is crucial for addpopover to find the new IDs
}
# Run the application
shinyApp(ui = ui, server = server)