3

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.

Community
  • 1
  • 1
Siemkowski
  • 1,351
  • 5
  • 15
  • 32

1 Answers1

3

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.

Romain
  • 440
  • 4
  • 17
  • Its strange but if you choise option1 and look at tooltip and then change on option2 nothing showing in toolip ( may be it is only in my R) – Batanichek Sep 09 '16 at 13:52
  • You have to wait for a while as the update is done with an `observe({})`. But it definitly work in my R. Could you retry and confirm plz? – Romain Sep 09 '16 at 13:58
  • The code does work and thus is a solution to the problem. Indeed it is relatively slow, which still leaves room for improvement. Thank you for your answer. – Siemkowski Sep 09 '16 at 14:45
  • I don't know if you can improve it a lot. If you make the icon dynamic `icon(name = ifelse(input$SELECT =="Option1","info-circle","ban")`, the result is almost instant. It seems that the text have more trouble being updated quickly. – Romain Sep 12 '16 at 11:15