library(ENRanalytics)
library(ggplot2)
library(scales)
library(dplyr)

datatable_enr function

Overview

datatable_enr(data,
              options = list(),
              sorting = FALSE,
              desc_column = NULL,
              tooltip = "",
              ...)

Use cases

The new function datatable_enr is the extention of DT package. It provides fixed ENRanalytics layout, and also, it facilitates the process of creating good-looking tables. We present several examples.

Below you can see a simple case with only one parameter - data.

datatable_enr(data = mtcars)


You can add sorting option in columns, by default, this option is disabled.

datatable_enr(mtcars, sorting = T)


You can also add description of the table by using tooltip argument.

datatable_enr(mtcars, tooltip = "This is the description of table.")

Besides, by using desc_column argument, you can differentiate first columns from others. It is useful especially when your table consists of information about titles.

datatable_enr(mtcars, desc_column = 1:3, tooltip = "This is the description of table.")

In original datatable function (DT package), you can add several arguments in options argument. In order to allow you to customize a table, you can add whatever arguments in options which you would include in the original datatable function.

datatable_enr(mtcars,
              desc_column = 1:3, 
              extensions = "Scroller",
              tooltip = "This is the description of table.")

Additionally, datatable_enr function has ... argument in which you can add whaever argument you would include in the original datatable function, for example, extensions argument.

mtcars %>% 
  bind_cols(mtcars) %>% 
  head(5) %>% 
  datatable_enr(tooltip = "This is a description.",
              options = list(fixedColumns = list(leftColumns = 3)),
              desc_column = 1:3,
              extensions = c('Scroller','FixedColumns'))

get_html_data + datatable_enr function

Overview

get_html_data(data, 
              column = NULL, 
              direction = 'col', #possible options: 'col', 'row', and 'area'
              formatter = 'simple_formatter') # any formatter you build


Use cases

The get_html_data function (based on formattable package) produces dataset with html tags which then, allows you to color cells within tables. Using with datatable_enr, it generates table you can include in your report. Below, the simple example of combining two functions is created. mtcars dataset is an input and three columns (4:6) were modified to add color cells. direction argument allows you to decide in which way the cells will be coloured. area option indicates that all values from all chosen columns will be taken into account. col option colors cells in each column separately, likewise row in each row. By default, the function uses simple_formatter which colors numeric values, meaning, the bigger value, the darker cell. Of course, you case build your own formatter (more details in formattable's documentation).

get_html_data(mtcars, 
              column = 4:6, 
              direction = 'area') %>% 
  datatable_enr(tooltip = "This is a description.")


Additionally, formatter argument can changed using built-in formatter or whatever formatter you build. It depends on your needs. You can also add column = -1 in order to include all columns exept the first one.

df <- cbind(data.frame(id = 1:5),
            do.call(cbind, lapply(1:8, function(x) sample(-50:50, 5))))


get_html_data(df, 
              column = -1, 
              direction = 'area', 
              formatter = 'sign_formatter') %>% 
  datatable_enr(tooltip = "This is a description.")


It is another example of using direction = 'row'.

df <- cbind(data.frame(id = 1:5),
            do.call(cbind, lapply(1:8, function(x) sample(1:100, 5))))


get_html_data(df, 
              column = -1, 
              direction = 'row', 
              formatter = 'simple_formatter') %>%
  datatable_enr()


By default, get_html_data function uses one formatter, for all chosen columns (column). However, it is possible to use as many formatters as you want. All you have to do, is to add a vector or list of formatters. Be careful, the number of columns and number of formatters have to be same.

df <- data.frame(id = 1:5,
            simple_format = sample(-50:50, 5),
            sign_format = sample(1:100, 5))


get_html_data(df, 
              column = 2:3, 
              direction = 'col', 
              formatter = c('sign_formatter', 'simple_formatter')) %>%
  datatable_enr(tooltip = "This is a description.")


If you want to include percent values, it is critical to use formattable::percent in original dataset first.

df <- data.frame(id = 1:5,
                 x = runif(5) %>% formattable::percent(0),
                 y = runif(5) %>% formattable::percent(0))


df %>% 
  get_html_data(column = -1) %>% 
  datatable_enr()


You can also use more options from datatable_enr if necessary.

mtcars[,1:8] %>% 
  tibble::rownames_to_column() %>% 
  get_html_data(column = -1, direction = 'col') %>% 
  datatable_enr(tooltip = "This is a description.", desc_column = 1, 
              extensions = "Scroller",
              options = list(scrollY = 300))

Warnings & Errors

get_html_data(mtcars) %>%
  datatable_enr()
get_html_data(diamonds %>% head(),
                   column = 1:2,
                   direction = 'area') %>%
  datatable_enr()
get_html_data(mtcars,
                   column = 1:2,
                   direction = 'xxx') %>%
  datatable_enr()


AMUFacultyOfEnglish/ENRanalytics documentation built on May 5, 2019, 11:36 a.m.