【问题标题】:add popovers to shiny app?将弹出窗口添加到闪亮的应用程序?
【发布时间】:2016-08-31 07:15:26
【问题描述】:

我想在小部件的标题旁边添加一个 (?),以便用户可以悬停或单击它并获取额外信息和他们可以单击的链接。

这就是我现在拥有的:

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyBS)
# Header
header <- dashboardHeader()
# Sidebar
sidebar <- dashboardSidebar(fileInput("chosenfile", label = h4("File input"), 
                                      accept = ".csv"),
                            bsButton("q1", label = "", icon = icon("question"),
                                     style = "info", size = "extra-small"),
                            bsPopover(id = "q1", title = "Tidy data",
                                      content = paste0("You should read the ", 
                                                       a("tidy data paper", 
                                                         href = "http://vita.had.co.nz/papers/tidy-data.pdf",
                                                         target="_blank")),
                                      placement = "right", 
                                      trigger = "click", 
                                      options = list(container = "body")
                                      )
                            )
# Body
body <- dashboardBody()
# ui
ui <- dashboardPage(header, sidebar, body)
# server
server <- function(input, output) {

}
# run
shinyApp(ui, server)

但这远非完美。例如,(?) 的位置不在“文件输入”旁边,要关闭弹出框,您必须再次单击问号,而不是在弹出框中有 (x)。

【问题讨论】:

  • 您将不得不用我认为的一些html和js代码修改标签的h4。这个问题可能会有所帮助(最后一个帖子):github.com/ebailey78/shinyBS/issues/26
  • 谢谢@Gopala。唉,我不懂 JS :(

标签: r twitter-bootstrap shiny shinybs


【解决方案1】:

这个答案可能不是你最初想要的,但它仍然可以为你工作。

你说你想要标签旁边的工具提示问号,所以我把它放在标签里。使用正确的对齐方式。 其次,您希望在再次单击按钮之前不打开工具提示,因为这很烦人。那么弹出选项“焦点”可能适合您。

## app.R ##
library(shiny)
library(shinydashboard)
library(shinyBS)
# Header
header <- dashboardHeader()
# Sidebar
sidebar <- dashboardSidebar(
  fileInput("chosenfile", 
    label = h4("File input ",
              tags$style(type = "text/css", "#q1 {vertical-align: top;}"),
              bsButton("q1", label = "", icon = icon("question"), style = "info", size = "extra-small")
            ),
    accept = ".csv"),
  bsPopover(id = "q1", title = "Tidy data",
    content = paste0("You should read the ", 
                a("tidy data paper", 
                  href = "http://vita.had.co.nz/papers/tidy-data.pdf",
                  target="_blank")
                ),
    placement = "right", 
    trigger = "focus", 
    options = list(container = "body")
  )
)
# Body
body <- dashboardBody()
# ui
ui <- dashboardPage(header, sidebar, body)
# server
server <- function(input, output) {}
# run
shinyApp(ui, server)

【讨论】:

    【解决方案2】:

    我对 JS 也不太了解,但 this post 在“设计”闪亮应用方面帮助了我很多。

    在同一行中显示小部件的一种方法是将它们中的每一个放在带有“style:inline-block”的 div 中。因为fileInput太大,(?)一直被移到下一行,所以可以通过'width: somepercetage%'或'width: somepixels px'强行判断fileInput占用多少空间。

    按照这些想法,代码将如下所示:

    div(
      div(
        # edit1
        style="width:80%; display:inline-block; vertical-align: middle;",
        fileInput("chosenfile", label = h4("File input"), 
                  accept = ".csv")
      ),
      div(
        # edit2
        style="display:inline-block; vertical-align: middle;",
        bsButton("q1", label = "", icon = icon("question"),
                 style = "info"),
        bsPopover(id = "q1", title = "Tidy data",
                  content = paste0("You should read the ", 
                                   a("tidy data paper", 
                                   href = "http://vita.had.co.nz/papers/tidy-data.pdf",
                                     target="_blank")),
                  placement = "right", 
                  trigger = "click", 
                  options = list(container = "body")
        )
      )
    )
    

    【讨论】:

    • 你的解决方案上的问号很奇怪,screenshot.net/1r4yjs5
    • 在 Rstudio Viewer 中一切正常,但在浏览器中确实变得一团糟。我已经在 (?) 的 div 中使用 'vertical-align: middle' 编辑了我的答案,现在应该看起来不错。
    • 你也可以在底部对齐两个 div。这是其他可能性的链接:w3schools.com/cssref/pr_pos_vertical-align.asp
    • 非常接近我所希望的,但问号的位置很尴尬。有没有办法把它移到File input 旁边。 @Gopala 提到了一些关于使用 HTML 替换 h4 的事情,但我不知道如何:(。谢谢!
    • 我在几个浏览器中使用垂直对齐测试了代码,但这种情况不再发生......我已经添加了 Chrome 测试的 prt 屏幕。你的看起来不一样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2017-09-28
    • 2015-06-15
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多