【问题标题】:Use href infobox as actionbutton使用 href 信息框作为操作按钮
【发布时间】:2016-03-28 13:23:27
【问题描述】:

我正在用Rshiny 构建一个App

我有几个infoBox,我想在单击infoBox 时使用href 选项弹出一个窗口。

我使用 shinyBS 作为弹出选项。 这是我尝试过的:

valueBox(value=entry_01, icon = icon("users","fa-lg",lib="font-awesome"),href=shinyInput(actionLink,id='button_01',len=1,class="btn btn-default action-button",label=""),
        width=NULL,color = "light-blue",subtitle = ""
)

但我发现href 选项可以完美地工作,如果我们想链接到外部网站,比如href = "http://stackoverflow.com/" 但我不知道如何链接到应用程序的内部链接。

编辑

我进行此编辑是因为我找到了一个解决方案,通过在 valueBox 输出列表中添加两个变量,使框可点击并使闪亮认为它是一个操作按钮。
- 班级action-button
- id 允许我们使用观察或观察事件来检测何时单击值框。

这是一个可复制的例子

require(shiny)
require(shinydashboard)


header <- dashboardHeader(title="ReproductibleExample")
sidebar <- dashboardSidebar(disable=T)
body <- dashboardBody(valueBoxOutput("box_01"),
                      textOutput("print"))

ui <- dashboardPage(header, sidebar, body)


server<-shinyServer(function(input, output,session) {

  output$box_01 <- renderValueBox({
  entry_01<-20
  box1<-valueBox(value=entry_01
                 ,icon = icon("users",lib="font-awesome")
                 ,width=NULL
                 ,color = "blue"
                 ,href="#"
                 ,subtitle=HTML("<b>Test click on valueBox</b>")
                 )
    box1$children[[1]]$attribs$class<-"action-button"
    box1$children[[1]]$attribs$id<-"button_box_01"
    return(box1)
  })

  output$print<-renderText({
    print(input$button_box_01)
  })
})



shinyApp(ui,server)

【问题讨论】:

    标签: r shiny infobox shinydashboard action-button


    【解决方案1】:

    我决定改变方法。我现在在值框的 substile 元素中包含了一个 actionbutton(或 actionLink),并创建了一个链接到此 actionButton 的 bsModal 元素。
    如果您不熟悉 ShinyBS 包,它允许制作弹出框, tooltip 等功能,不包括 HTML 或 java。

    我遵循@Mikko Martila 的建议Shiny: adding addPopover to actionLink,这是一个可复制的示例,向您展示我的问题:

    library("shiny")
    library("shinydashboard")
    library("shinyBS")
    
    header <- dashboardHeader(title = "reporductible example")
    
    body <- dashboardBody(valueBoxOutput("box_01"),
                          bsModal("modal", "foo", trigger = "", "bar"))
    sidebar <- dashboardSidebar()
    ui <- dashboardPage(header,sidebar,body,skin="green")
    server = function(input, output, session) {
      # ----- First info box synthesis menu
      output$box_01 <- renderValueBox({
        entry_01 <- "BlaBla"
        valueBox(value=entry_01, icon = icon("users",lib="font-awesome"),
                        width=NULL,color = "blue",subtitle = HTML("<b>my substitle</b> <button id=\"button\" type=\"button\" class=\"btn btn-default action-button\">Show modal</button>")
        )
      })
    
      observeEvent(input$button, {
        toggleModal(session, "modal", "open")
      })
    }
    
    runApp(list(ui = ui, server = server))
    

    我使用HTML() 选项将我的按钮添加到值框的副标题中。

    这不是我真正想要的,但它确实有效。

    您可以使用 actionLink(看起来更好)通过使用这样的字幕来做到这一点:

    subtitle=HTML("<b>my subtitle</b><a id=\"button_box_05\" href=\"#\" class=\"action-button\">
         <i class=\"fa fa-question-circle\"></i>
    
           </a>")
    

    【讨论】:

    • 这对模态非常有效,但您是否通过单击valueBox 设法显示shinyBS::addPopover
    【解决方案2】:

    我遇到了同样的问题,浏览了这个链接,就让它工作了,没有添加单独的按钮,就像这样。 希望这对寻求解决类似问题的人有所帮助

    require(shiny)
    require(shinydashboard)
    require(shinyBS)
    
    
    header <- dashboardHeader(title="ReproductibleExample")
    sidebar <- dashboardSidebar(disable=T)
    body <- dashboardBody(valueBoxOutput("box_01"),
                          textOutput("print"),bsModal("mod","title","btn"))
    
    ui <- dashboardPage(header, sidebar, body)
    
    
    server<-shinyServer(function(input, output,session) {
    
      output$box_01 <- renderValueBox({
      entry_01<-20
      box1<-valueBox(value=entry_01
                     ,icon = icon("users",lib="font-awesome")
                     ,width=NULL
                     ,color = "blue"
                     ,href="#"
                     ,subtitle=HTML("<b>Test click on valueBox</b>")
                     )
        box1$children[[1]]$attribs$class<-"action-button"
        box1$children[[1]]$attribs$id<-"button_box_01"
        return(box1)
      })
     observeEvent(input$button_box_01, {
      toggleModal(session,"mod","open")
      output$print<-renderText({
        print(input$button_box_01)
      })})
    })
    
    shinyApp(ui,server)
    

    【讨论】:

    • 当我尝试这个时,我的observeEvent 没有被触发,知道为什么吗?
    【解决方案3】:

    我只知道坏变种

    1) 添加功能tags$script(HTML("function clickFunction(link){ Shiny.onInputChange('linkClicked',link); }"))

    2) 编辑 valueBox 的 href 子项

    aa=valueBox(value="22", icon = icon("users","fa-lg",lib="font-awesome"),href="www", width=NULL,color = "light-blue",subtitle = "" ) aa$children[[1]]=a(href="#","onclick"=paste0("clickFunction('","click","'); return false;"),aa$children[[1]]$children)

    3)observeEvent(input$linkClicked,{..})

    【讨论】:

    • 我一步一步尝试,当我添加childrend的编辑时,我有这个错误Error in tagAssert(vbox, type = "div") : Expected tag to be of type div
    • 你能展示一下吗? aa=valueBox(value="22", icon = icon("users","fa-lg",lib="font-awesome"),href="www", width=NULL,color = "light-blue",subtitle = "" )print(aa)
    • 嗯..不知道,也显示print(aa$children[[1]]$children)
    • print(box_01$children[[1]]$children) [[1]]
      41.5K

      usagers en situation d'impayé

    猜你喜欢
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多