Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you add an explanation to shiny datatable column? [duplicate]

I'm writing a shiny app with the help of datatables and I would like to make small boxes that will explain what each column means. Ideally, I would like them to appear when you move the cursor over the name of the column.

Here is an app:

ui.R

library(shiny)

data(iris)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      fluidRow(
        column(12,
               wellPanel(
                 radioButtons("species", label="Species:", 
                              choices=levels(iris$Species),
                              selected=levels(iris$Species)[1]

                 )
               )
        )
      )
    ),
    mainPanel(
      fluidRow(column(12,
                      dataTableOutput("table1")

      )
      )
    )
  )
)
)

server.R

library(shiny)
library(dplyr)
library(tidyr)

data(iris)  


shinyServer(function(input, output) {

  iris1 <- reactive({
    iris %>% 
      filter(Species %in% input$species)
  })

  output$table1 <- renderDataTable({
    iris1()
  })
})

And I would like to have sth like this, when you move the courser over the Species column name: enter image description here

Is it even possible? Please help.

like image 920
potockan Avatar asked Oct 19 '22 01:10

potockan


1 Answers

Thanks to this I managed to make an output I wanted:

ui.R - without any changes

server.R

library(shiny)
library(dplyr)
library(tidyr)

data(iris)  

shinyServer(function(input, output) {

  iris1 <- reactive({
    iris %>% 
      filter(Species %in% input$species)
  })

  output$table1 <- renderDataTable(iris1(), callback = htmlwidgets::JS("
        var tips = ['Row Names', 'The Sepal Length', 'The Sepal Width',
                    'The Petal Length', 'The Petal Width'],
            header = table.columns().header();
        for (var i = 0; i < tips.length; i++) {
          $(header[i]).attr('title', tips[i]);
        }
"))
})

It makes explanation boxes defined in JavaScript variable tips.

like image 144
potockan Avatar answered Oct 21 '22 17:10

potockan