Shiny: adding addPopover to actionLink

六月ゝ 毕业季﹏ 提交于 2019-12-10 11:37:46

问题


I want to include a small "Help" actionLink (next to a "Render" actionButton) that acts as a popover (see here). Here's my code:

server.R:

shinyUI(pageWithSidebar(
  sidebarPanel( 
    actionButton("renderButton", "Render"),
    actionLink("link", "Help") ),
  mainPanel()
))

ui.R:

shinyServer(function(input, output, session) {
   # ... dealing with renderButton ...
   output$link <- renderUI({
   addPopover(session=session, id=output$link, title="", 
              content="Testing.", placement = "bottom",
              trigger = "click", options = NULL)
   })
})

Right now, the actionLink shows up on the sidebar, but clicking on it has no effect. Any tips? I think it may have to do with the id in addPopover, but I haven't found many examples to provide a framework. I found this, but I want to deal with the popover in server.R, not ui.R. Can it be done this way, or should I just make the popover in ui.R?


回答1:


From ?Tooltips_and_Popovers:

There must be at least one shinyBS component in the UI of your app in order for the necessary dependencies to be loaded. Because of this, addTooltip and addPopover will not work if they are the only shinyBS components in your app.

To get the pop over to work, you can change your actionButton into a bsButton, and modify server.R to only contain the call to addPopover. The id argument to addPopover also needs to be changed to refer to the id of the ui object you want the pop over to appear on, in your case "link", the id of the actionLink .

Here's the modified example code in a self-contained code chunk:

library(shiny)
library(shinyBS)

runApp(
  # Ui
  list(ui = pageWithSidebar(
    headerPanel("Test App"),

    sidebarPanel( 
      bsButton("renderButton", "Render"),
      actionLink("link", "Help") ),

    mainPanel("Hello World!")
  ),

  # Server
  server = function(input, output, session) {

    # ... dealing with renderButton ...        
    addPopover(session=session, id="link", title="", 
               content="Testing.", placement = "bottom",
               trigger = "click", options = NULL)

  })
)


来源:https://stackoverflow.com/questions/31388329/shiny-adding-addpopover-to-actionlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!