从shinyBS::addpopover()访问数据表行ID(使用rowCallback生成)

lb3vh1jj  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(112)

我尝试使用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)

'

laawzig2

laawzig21#

它使用server=FALSErowId选项,而不是rowCallback

output$table <-
    renderDT({
      datatable(
        data = iris,
        options = list(
          rowId = JS("function(data){return 'tableRow_' + data[0];}")
        )
      )
    }, server = FALSE)

未尝试使用rowCallback

gmxoilav

gmxoilav2#

我想我会给予一个更多的更新(供将来参考)之前,我离开reprex的领土。
下面的版本有一个修改过的rowCallback函数,用于为每行第四列中的单元格分配ID。Edit:还有一个更新--使用createdcell函数创建id标签,如Stéphane Laurent所建议的,在createdcell中,(从零开始)rowindex的JS函数加1,以匹配 Dataframe 行索引,从而在for循环中使用iris$Petal.Width[row]填充内容。
在addpopover()调用中,为了避免popover扰乱数据表的布局(因为popover div被插入数据表),选项“container = body”是至关重要的。

library(DT)
library(shinyBS)
library(shiny)
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,
              id = "DataTables_Table_0",
              title = "information",
              content = "this is the popover on id DataTables_Table_0"
    )
  
    for (row in c(1:nrow(iris))) { 
      addPopover(session,
               id = paste0('cell_', row, '_2'),
               title = "cell information",
               trigger = 'hover',
               content = paste('petal width =', iris$Petal.Width[row]),
               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(
                 columnDefs = list(
                    list(visible=FALSE, targets=3),
                    list(targets = "_all",
                         createdCell = DT::JS("
                                             function(td, cellData, rowData, row, col) {
                                                    $(td).attr('id', 'cell_'+(row+1)+'_'+col); 
                                             }
                                           ")
                              ))))
        }, server = FALSE) #server = F is crucial for addpopover to find the new IDs
  
}

# Run the application
shinyApp(ui = ui, server = server)

相关问题