插入单选按钮的弹出框

问题描述 投票:2回答:1

我为单选按钮做了一个popify。但是我想在我的radiobuttom选项中分开popify。例如,对于“ filter1”,我想为“所有属性”插入一个popify,为“排除属性”选项插入另一个popify。这可能吗 ??可执行代码如下。

library(shinyBS)
library(shiny)

ui <- fluidPage(


  titlePanel("Old Faithful Geyser Data"),

  sidebarLayout(
    sidebarPanel(


      popify(  
        radioButtons("filter1", h3("Select properties"),
                     choices = list("All properties" = 1, 
                                    "Exclude properties" = 2),
                     selected = 1),
        title= "Select Proprierties",
        content = paste0("Filter 1 refers to.....")),

        radioButtons("filter2", h3("Select farms"),
                     choices = list("All farms" = 1, 
                                    "Exclude farms" = 2),
                     selected = 1),
       sliderInput("bins",
                    "Number of bins:",
                    min = 1,
                    max = 20,
                    value = 30)
           ),

    mainPanel(
      plotOutput("distPlot")
    )
  )
)

server <- function(input, output) {

  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
}

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

非常感谢!

r shiny
1个回答
1
投票

查看popify,我们发现它仅适用于完整的shiny元素,但是您不想将其添加到整个元素,而是将其添加到其子HTML元素。

addPopover因此似乎是更好的选择。但是,我们看到该函数在id给定的HTML元素上添加了弹出框。问题是您要拥有弹出框的行没有id,并且addPopover函数不允许通过除id之外的其他方式指定元素。

因此,我们必须解决:

  1. 使用JS将id添加到单选按钮行中(为此我使用shinyjs
  2. 使用在id中创建的addPopover使用此>
。为了使addPopover运行,您需要至少包含一个shinyBS组件。从帮助文件:

您的用户界面中必须至少有一个“ shinyBS”组件 应用程序,以便加载必要的依赖项。因为 其中,“ addTooltip”和“ addPopover”如果不能 您应用中唯一的ShinyBS组件。

library(shinyBS)
library(shiny)
library(shinyjs) ## needed to tamper with the HTML

ui <- fluidPage(
   useShinyjs(),

   titlePanel("Old Faithful Geyser Data"),

   sidebarLayout(
      sidebarPanel( 
         radioButtons("filter1", h3("Select properties"),
                      choices = list("All properties" = 1, 
                                     "Exclude properties" = 2),
                      selected = 1),
         title= "Select Proprierties",
         radioButtons("filter2", h3("Select farms"),
                      choices = list("All farms" = 1, 
                                     "Exclude farms" = 2),
                      selected = 1),
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 20,
                     value = 30),
         ## need to include at least one bs element, adapt
         bsTooltip("bins", "The wait times will be broken into this many equally spaced bins",
                   "right", options = list(container = "body")) 

      ),

      mainPanel(
         plotOutput("distPlot")
      )
   )
)

## use JS to add an id attribute to the elements where you want to add the popover
add_id_js <- paste0(
   "$('#filter1').find('.radio > label').attr('id', function(i) {",
   "return 'filter1_row_' + i})")

server <- function(input, output, session) {
  ## once the UI is loaded, call JS function and attach popover to it
   session$onFlushed(function() {
      runjs(add_id_js)
      addPopover(session, "filter1_row_0", "My Popover", "Content")

   })


   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2]
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })
}

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

Popover

© www.soinside.com 2019 - 2024. All rights reserved.