Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a bar chart in datatable?

Tags:

r

dt

shiny

I want to display a bar chart in data table. This link shows what I want to do using javascript. I have no knowledge of javascript or html so could you tell me if it is possible to achieve this using shiny?

The code in the site is as follows:

HTML code:

<table id="dTable" class="table table-striped table-bordered" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Languages</th>
            <th>Positive</th>
            <th>Neutral</th>
            <th>Negative</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>English</td>
            <td>50</td>
            <td>20</td>
            <td>25</td>
        </tr>
        <tr>
            <td>German</td>
            <td>25</td>
            <td>10</td>
            <td>12</td>
        </tr>
        <tr>
            <td>French</td>
            <td>20</td>
            <td>20</td>
            <td>17</td>
        </tr>
        <tr>
            <td>Spanish</td>
            <td>22</td>
            <td>4</td>
            <td>10</td>
        </tr>
    </tbody>
</table>

JAVASCRIPT:

$(function(){
    $("#dTable").dataTable({
        "columns": [
                {
                    "title":"Languages"
                },
                {
                    "title":"Votes",
                    "render": function(data, type, row, meta){
                        return parseInt(row[1], 10) + parseInt(row[2], 10) + parseInt(row[3], 10)
                    }
                },
                {
                    "visible":false
                },
                {
                    "title": "Positive/Neutral/Negative",
                    "sortable":false,
                    "render": function(data, type, row, meta){
                        return $("<div></div>", {
                            "class": "bar-chart-bar"
                        }).append(function(){
                            var bars = [];
                            for(var i = 1; i < row.length; i++){
                                bars.push($("<div></div>",{
                                    "class": "bar " + "bar" + i
                                }).css({
                                    "width": row[i] + "%"
                                }))
                            }
                            return bars;
                        }).prop("outerHTML")
                    }
                }
        ]
    });
});
like image 285
SBista Avatar asked Sep 05 '25 01:09

SBista


1 Answers

This is how you can implement the provided Javascript example in R shiny.

Basically, you need to add the JS function in the render option of columnDefs while performing renderDataTable.

Below is the sample code -

# Prepare the Sample data
test_data <-
  data.table(
    Languages = c('English', 'German', 'French', 'Spanish'),
    Positive = c(50, 25, 20, 22),
    Neutral = c(20, 10, 20, 4),
    Negative = c(25, 12, 17, 10)
  )

# Define the Shiny UI and Custom CSS Elements
ui <- fluidPage(tags$head(tags$style(
  HTML(
    "
      .bar-chart-bar {
          background-color: #e8e8e8;
          display: block;
          position:relative;
          width: 100%;
          height: 20px;
      }
      .bar {
          float: left;
          height: 100%;
      }
      .bar1 {
          background-color: green;
      }
      .bar2 {
          background-color: yellow;
      }
      .bar3 {
          background-color: red;
      }
    "
  )
)), DT::dataTableOutput("test_table"))

# Rendering the DataTable in Shiny Server

server <- function(input, output) {
  output$test_table <- DT::renderDataTable({
    dt <-  DT::datatable(
      as.data.frame(test_data),
      rownames = FALSE,
      options = list(columnDefs = list(list(
        targets = -1,
        render =
          JS(
            "function(data, type, row, meta){
                        return $('<div></div>', {
                            'class': 'bar-chart-bar'
                        }).append(function(){
                            var bars = [];
                            for(var i = 1; i < row.length; i++){
                                bars.push($('<div></div>',{
                                    'class': 'bar ' + 'bar' + i
                                }).css({
                                    'width': row[i] + '%'
                                }))
                            }
                            return bars;
                        }).prop('outerHTML')
                    }"
          )
      )))
    )
  })
}

# Run the App
shinyApp(ui, server)
like image 187
Shubham Avatar answered Sep 07 '25 19:09

Shubham