Siemkowski Siemkowski - 2 months ago 63
R Question

Dynamic popover or tooltip in shinyBS

The idea

I have a

box()
in a shiny app. The
box()
includes a
title
argument (which in turn includes an
icon
) and a
selectInput()
element. On hoover over the
icon
I wanted to have a tooltip (using
tipify()
) or a popover (using
popify()
) which
title
or
content
argument (or both) would be generated depending on
selectInput()
input.

The problem

Neither
tipify()
nor
popify()
correcctly implement
textOutput()
as their
title
or
content
argument. They need a character string so I tried to use a
reactiveValues()
element as a function argument but it also failed.

The question

Can tooltip or popover content be made dynamic by just using
r
? How could this be done?

I suspect it can be done with
JavaScript
but I have little knowledge of it.

The code

Attempt 1 - failed - displays code not actual text

library("shiny")
library("shinydashboard")
library("shinyBS")

ui <- fluidPage(
box(
title = span("My box",
tipify(el = icon(name = "info-circle", lib = "font-awesome"), title = textOutput("TIP"))),
selectInput(
inputId = "SELECT",
label = NULL,
choices = c("Option1" = "Option1",
"Option2" = "Option2"
),
multiple = FALSE
)
)
)
server <- function(input, output, session){
output$TIP <- renderText({"Helo world!"})
}
shinyApp(ui, server)


Attempt 2 - failed - cannot create UI as TIP (
reactiveValues()
) is not yet defined

library("shiny")
library("shinydashboard")
library("shinyBS")

ui <- fluidPage(
box(
title = span("My box",
tipify(el = icon(name = "info-circle", lib = "font-awesome"), title = TIP$a)),
selectInput(
inputId = "SELECT",
label = NULL,
choices = c("Option1" = "Option1",
"Option2" = "Option2"
),
multiple = FALSE
)
)
)
server <- function(input, output, session){
TIP <- reactiveValues(a = "Hello world!")
}
shinyApp(ui, server)


Here is a similar question but it does not solve the problem described here.

Answer

What could be done is creating the title entirely in the server side. This way you have no problem making it dynamic. This could give you this kind of app:

library("shiny")
library("shinydashboard")
library("shinyBS")

ui <- fluidPage(
  box(
    title = uiOutput("title"),
    selectInput(
      inputId = "SELECT",
      label = NULL,
      choices = c("Option1" = "Option1",
                  "Option2" = "Option2"
      ),
      multiple = FALSE
    )
  )
)
server <- function(input, output, session){
  TIP <- reactiveValues()
  observe({
    TIP$a <- ifelse(input$SELECT =="Option1","Hello World","Hello Mars")
  })


  output$title <- renderUI({span("My box",
                   tipify(el = icon(name = "info-circle", lib = "font-awesome"), title = TIP$a))})


}
shinyApp(ui, server)

Hope it helps.

Comments