0

I need to add tooltips to the tabs "Plot", "Summary" and "table". For this purpose, I have used bsTooltip function. But it is not working here.

library(shiny)
library(shinyBS)
 ui <- fluidPage(
titlePanel("Tabsets"),
sidebarLayout(
conditionalPanel(
'input.dataset === "Plot"',
radioButtons("dist", "Distribution type:",
               c("Normal" = "norm",
                 "Uniform" = "unif",
                 "Log-normal" = "lnorm",
                 "Exponential" = "exp")),br(),
sliderInput("n",
              "Number of observations:",
              value = 500,
              min = 1,
              max = 1000)),
mainPanel(

  tabsetPanel(id = 'dataset',
              tabPanel("Plot", plotOutput("plot")),
              tabPanel("Summary", verbatimTextOutput("summary")),
              tabPanel("Table", tableOutput("table"))))),

   bsTooltip("Plot", "Information", placement = "bottom", trigger = "hover",
      options = NULL),
    bsTooltip("Table", "Table Summary", placement = "bottom", trigger = "hover",
      options = NULL),
    bsTooltip("Summary", "Summary", placement = "bottom", trigger = "hover",
      options = NULL),)


server <- function(input, output) 
     {
     d <- reactive({
dist <- switch(input$dist,
               norm = rnorm,
               unif = runif,
               lnorm = rlnorm,
               exp = rexp,
               rnorm)

dist(input$n)
     })
   output$plot <- renderPlot({
dist <- input$dist
n <- input$n

hist(d(),
     main = paste("r", dist, "(", n, ")", sep = ""),
     col = "#75AADB", border = "white")
     })

      output$summary <- renderPrint({
summary(d())})
    output$table <- renderTable({
d()})}
  shinyApp(ui, server)

If I change the bsTooltip ID to dataset it will work but not as expected

Is there any other way to do this ?

phalteman
  • 3,442
  • 1
  • 29
  • 46
ZenMac
  • 249
  • 1
  • 7
  • I think the problem is actually with the use of `tabsetPanel` and `tabPanel`, not with `condtionalPanel`. See the question and possible solutions here - perhaps one will work for you? https://stackoverflow.com/questions/44953873/add-tooltip-to-tabs-in-shiny – phalteman Jun 23 '21 at 17:20

2 Answers2

1

You may want to use the newer package {spsComps}. ShinyBS is not updated for more than 5 years.There are lots of other features from {spsComps}. Check the demo for details.

library(shiny)
library(spsComps)
library(magrittr)
ui <- fluidPage(
    titlePanel("Tabsets"),
    sidebarLayout(
        conditionalPanel(
            'input.dataset === "Plot"',
            radioButtons("dist", "Distribution type:",
                         c("Normal" = "norm",
                           "Uniform" = "unif",
                           "Log-normal" = "lnorm",
                           "Exponential" = "exp")),br(),
            sliderInput("n",
                        "Number of observations:",
                        value = 500,
                        min = 1,
                        max = 1000)),
        mainPanel(
            tabsetPanel(id = 'dataset',
                        tabPanel("Plot", 
                                 plotOutput("plot") %>% 
                                     bsTooltip("Information", placement = "bottom")
                        ),
                        tabPanel("Summary", 
                                 verbatimTextOutput("summary") %>% 
                                    bsTooltip("Table Summary", placement = "bottom")
                        ),
                        tabPanel("Table", 
                                 tableOutput("table") %>% 
                                    bsTooltip("Summary", placement = "bottom")
                        )
            )
        )
    )
)


server <- function(input, output) 
{
    d <- reactive({
        dist <- switch(input$dist,
                       norm = rnorm,
                       unif = runif,
                       lnorm = rlnorm,
                       exp = rexp,
                       rnorm)
        
        dist(input$n)
    })
    output$plot <- renderPlot({
        dist <- input$dist
        n <- input$n
        
        hist(d(),
             main = paste("r", dist, "(", n, ")", sep = ""),
             col = "#75AADB", border = "white")
    })
    
    output$summary <- renderPrint({
        summary(d())})
    output$table <- renderTable({
        d()})}
shinyApp(ui, server)
lz100
  • 6,990
  • 6
  • 29
1

I forgot where I found this code. But this code is worked for me.

library(shiny)

shinyApp(
  ui = navbarPage(
    tags$script(HTML('
           $( document ).on("shiny:sessioninitialized", function(event) {
                $(\'span[data-toggle="tooltip"]\').tooltip({
                    html: true
                });      
           });'
    )),
    
    tabsetPanel(
      tabPanel(span("Tab 1", title = "aaa",`data-toggle`="tooltip")),
      tabPanel(span("Tab 2",title="bbb",`data-toggle`="tooltip")),
      tabPanel(span("Tab 3",title="ccc",`data-toggle`="tooltip"))
      )
  ),
  
  server = function(input, output) {
    
  }
)
Takuro Ikeda
  • 158
  • 6