【问题标题】:Shiny: Add Popover to Column Name in Datatable闪亮:将弹出框添加到数据表中的列名
【发布时间】:2021-12-01 08:41:25
【问题描述】:

我正在尝试将按钮添加到 dabletable 中的列名,并在将鼠标悬停在该按钮上时添加 bsPopover。我可以在数据表之外成功创建弹出框和按钮,并且可以将按钮添加到数据表中。但是让弹出框在数据表中工作已被证明是不成功的。我选择“悬停”作为触发器,以便单击保留列排序功能。任何帮助或指导表示赞赏。请参阅下面的代表:

library(shiny)
library(shinyBS)
library(DT)

ui <- fluidPage(
  titlePanel('Making a Popover Work in DataTable'),
  mainPanel(
    fluidRow(
      #popover button
      p(bsButton("workingPop",
                 label = "",
                 icon = icon("question"),
                 style = "info",
                 size = "extra-small")
      ),        
      #popover content
      bsPopover(id = "workingPop", title = "This Popover Works",
                content = "It works very well",
                placement = "right",
                trigger = "hover",
                options = list(container = "body")
      )),
    fluidRow(dataTableOutput('myTable'),
             bsPopover(id="notWorking", title = "This one does not work",
                       content = "I'd like to give information about hp: it means horsepower. I want a popover, because my real example has lot's of text.",
                       placement = "top",
                       trigger = "hover",
                       options = list(container = "body")))
  )
)

server <- function(input, output, session) {
  output$myTable <- DT::renderDataTable({
    datatable(mtcars %>%
                rename("hp <button type='button' id='notWorking' class='btn action-button btn-info btn-xs shiny-bound-input'>
  <i class='fa fa-question' role='presentation' aria-label='question icon'></i>
    </button>"=hp),
              rownames=TRUE,
              selection='none',
              escape=FALSE)
  })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny datatable


    【解决方案1】:

    请考虑使用 {shinyBs} 的替代品。

    我建议你试试我的包 {spsComps},它有类似的bsPopover 功能,但你可以做更多的事情,比如颜色、不透明度、字体大小、粗细等。

    shinyBs 已经超过 5 年没有更新了,我相信你知道这意味着什么。不是我想为我的包裹做广告,所以说一些关于shinyBs 的坏话。我开发这些功能是因为我在其他包中看不到它们,或者它们没有不断更新包。

    这是您的示例的用例:

    library(shiny)
    library(spsComps)
    library(DT)
    library(dplyr)
    # define the question button in a button since we need to uses multiple times
    infoBtn <- function(id) {
        actionButton(id,
                     label = "",
                     icon = icon("question"),
                     style = "info",
                     size = "extra-small",
                     class='btn action-button btn-info btn-xs shiny-bound-input'
        )
    }
    ui <- fluidPage(
        titlePanel('Making a Popover Work in DataTable'),
        mainPanel(
            fluidRow(
                #popover button
                infoBtn('workingPop') %>% 
                    bsPopover(title = "This Popover Works",
                          content = "It works very well",
                          placement = "right",
                          trigger = "hover"
                    )
            ),
            fluidRow(dataTableOutput('myTable'))
        )
    )
    
    server <- function(input, output, session) {
        output$myTable <- DT::renderDataTable({
            # construct the title and convert to text
            hp_text <- tags$span(
                "hp", 
                infoBtn('notWorking') %>% 
                    bsPopover(title = "This one does not work",
                              content = "I'd like to give information about hp: it means horsepower. I want a popover, because my real example has lot's of text.",
                              placement = "top",
                              trigger = "hover")
            ) %>% 
                as.character()
            # use !! and := to inject variable as text
            datatable(mtcars %>% rename(!!hp_text:=hp),
                      rownames=TRUE,
                      selection='none',
                      escape=FALSE)
        })
    }
    
    shinyApp(ui = ui, server = server)
    
    

    您可以使用 spsComps 执行的其他 popOver 实用程序:

    demos你可以探索spsComps和docs你可以阅读。

    【讨论】:

    • 这很好用,谢谢!我也很欣赏有关功能的建议,因为我在整个应用程序中多次使用此按钮。我所做的唯一更改是 tags$span for p() 因为我通过这里应用的 css 应用段落样式。一个通用的 span 标签解决了它。
    • 很高兴这有帮助
    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 2018-09-16
    • 2017-02-19
    • 2018-07-06
    • 2016-08-31
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多