Learn R Programming

highlightHTML (version 0.2.5)

table_id_inject: Table hash addition for markup

Description

A helper function to include a hashtag id code within a summary table. The summary table most commonly will take the form of a data frame object. For example, a descriptive summary table coming from the summarise function from the dplyr package. Can also specify a count table using the table function.

Usage

table_id_inject(table, id, conditions, variable = NULL, num_digits = NULL)

Arguments

table

A summary table object, most commonly will be a data.frame, but can also be a count table using the table function.

id

A vector of css id(s) to include

conditions

A character vector of conditions to include id. Must be same length as id. See details and examples for more information on how to specify the conditions.

variable

An optional list of column names to specify search of conditions. More than one variable can be specified in each element of the list. The list must be the same length as the conditions or id arguments.

num_digits

A numeric value to specify the number of decimal values to include in the final output.

Details

The conditions argument takes the following operators for numeric variables: >, >=, <, <=, ==. For character variables, only == can be used to specify the text string to match on. Care needs to be made to wrap ensure the text string is wrapped in quotations. See the examples for more details on this.

This function can also be part of a chain using the %>% operator from magrittr. See the examples for more details.

Examples

Run this code
# NOT RUN {
library(dplyr)
library(highlightHTML)

mtcars %>% 
  group_by(cyl) %>% 
  summarise(avg_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
  data.frame() %>% 
  table_id_inject(id = c('#bgred', '#bgblue', '#bggreen'), 
     conditions = c('< 2', '> 16', '== 15.1'))
  

mtcars %>% 
  group_by(cyl) %>% 
  summarise(avg_mpg = mean(mpg), sd_mpg = sd(mpg)) %>%
  data.frame() %>%
  table_id_inject(id = c('#bgred', '#bgblue'),
     conditions = c('<= 2', '< 16'), 
     variable = list(c('sd_mpg'), c('avg_mpg')))  
  
# text example
storms %>%
  group_by(status) %>%
  summarise(avg_wind = mean(wind)) %>%
  data.frame() %>%
  table_id_inject(id = c('#bgred'),
     conditions = c('== "tropical depression"'))
     
     
# Table object
table(mtcars$cyl, mtcars$disp) %>%
  table_id_inject(id = c('#bgred'), 
                conditions = c('>= 3'))
  
# }

Run the code above in your browser using DataLab