Variable Table

Nick Huntington-Klein

2019-12-09

The vtable package serves the purpose of outputting automatic variable documentation that can be easily viewed while continuing to work with data.

vtable contains three main functions: vtable() (or vt()), labeltable(), and dftoHTML(). vtable() takes a dataset and outputs a formatted variable documentation file. This serves several purposes.

First, it allows for an easy generation of a variable documentation file, without requiring that one has already been created and made accessible through help(data), or dealing with creating and finding R help documentation files.

Second, it produces a list of variables (and, if provided, their labels) that can be easily viewed while working with the data, preventing repeated calls to head(), and making it much easier to work with confusingly-named variables.

Third, the variable documentation file can be opened in a browser (with option out='browser', saving to file and opening directly, or by opening in the RStudio Viewer pane and clicking ‘Show in New Window’) where it can be easily searched with standard Find-in-Page functions like Ctrl/Cmd-F, allowsing you to search for the variable or variable label you want.

labeltable() is designed to take a single variable and show the values it is associated with. This can also be used to generate data documentation if desired, or can just be an easy way to look at label values, or learn more about the data you’re working with.

If that variable has value labels from the sjlabelled or haven packages, it will show how the values in the data correspond to the value labels.

Alternately, you can include other variables as well, and labeltable() will show, for each value of the variable you’re interested in, the values that those other variables take. This can be handy, for example, if you used some variables to create a numeric ID and want to remember what original values correspond to each ID. It can also act as sort of a cross-tabulation.

dftoHTML() is a helper function used by vtable() and labeltable(). It takes any data frame or matrix with column names and outputs HTML table code for that data.

Other helper functions are nuniq() (short for length(unique(x))), countNA() (short for sum(is.na(x))), and propNA (short for mean(is.na(x))), which offer convenient shortcuts for these common compound functions for use with vtable’s summ option.


The vtable() function

vtable() (or vt() for short) syntax follows the following outline:

vtable(data, 
    out=NA,
    file=NA, 
    labels=NA, 
    class=TRUE, 
    values=TRUE,
    missing=FALSE,
    index=FALSE,
    factor.limit=5,
    char.values=FALSE,
    data.title=NA,
    desc=NA, 
    col.width=NA,
    summ=NA,
    lush=FALSE)

The goal of vtable() is to take a data set data and output an HTML file with documentation concerning each of the variables in data. There are several options as to what will be included in the documentation file, and each of these options are explained below. Throughout, most code will be included without output because output is typically rendered in a browser or in the Viewer pane of RStudio, not the console:

data

The data argument can take any data frame, data table, tibble, or matrix, as long as it has a valid set of variable names stored in the colnames() attribute. The goals of vtable() is to produce documentation of each of the variables in this data set and display that documentation, one variable per row on the output vtable.

If data has embedded variable or value labels, as the data set efc does below, vtable() will extract and use them automatically.

out

The out option determines what will be done with the resulting variable documentation file. There are several options for out:

Option Result
browser Loads variable documentation in web browser.
viewer Loads variable documentation in Viewer pane (RStudio only).
htmlreturn Returns HTML code for variable documentation file.
return Returns variable documentation table in data frame format.

By default, vtable will select ‘viewer’ if running in RStudio, and ‘browser’ otherwise.

file

The file argument will write the variable documentation file to an HTML file and save it. Will automatically append ‘html’ filetype if the filename does not include a period.

labels

The labels argument will attach variable labels to the variables in data. If variable labels are embedded in data and those labels are what you want, the labels argument is unnecessary. Set labels='omit' if there are embedded labels but you do not want them in the table.

labels can be used in any one of three formats.

labels as a one-row data set

labels can be set to a one-row data set in which the column names are the variable names in data and the first row is the variable names. The labels argument can take any data type including data frame, data table, tibble, or matrix, as long as it has a valid set of variable names stored in the colnames() attribute.

This approach does not require that every variable name in data has a matching label.

class

The class flag will either report or not report the class of each variable in the resulting variable table. By default this is set to TRUE.

values

The values flag will either report or not report the values that each variable takes. Numeric variables will report a range, logicals will report ‘TRUE FALSE’, and factor variables will report the first factor.limit (default 5) factors listed.

If the variable is numeric but has value labels applied by the sjlabelled package, vtable() will find them and report the numeric-label crosswalk.

missing

The missing flag, set to TRUE, will report the number of missing values in each variable. Defaults to FALSE.

index

The index flag will either report or not report the index number of each variable. Defaults to FALSE.

factor.limit

If values is set to TRUE, then factor.limit limits the number of factors displayed on the variable table. factor.limit is by default 5, to cut down on clutter. The table will include the phrase “and more” to indicate that some factors have been cut off.

Setting factor.limit=0 will include all factors. If values=FALSE, factor.limit does nothing.

char.values

If values is set to TRUE, then char.values = TRUE instructs vtable to list the values that character variables take, as though they were factors. If you only want some of the character variables to have their values listed, use a character vector to indicate which variables.

data.title

data.title will include a data title in the variable documentation file. If not set manually, this will default to the variable name for data.

desc

desc will include a description of the data set in the variable documentation file. This will by default include information on the number of observations and the number of columns. To remove this, set desc='omit', or include any description and then include ‘omit’ as the last four characters.

col.width

vtable() will select default column widths for the variable table depending on which measures (name, class, label, values, summ) are included. col.width, as a vector of percentage column widths on the 0-100 scale, will override these defaults.

summ

summ will calculate summary statistics for all variables that report valid output on the given summary statistics functions. summ is very flexible. It takes a character vector in which each element is of the form function(x), where function(x) is any function that takes a vector and returns a single numeric value. For example, summ=c('mean(x)','median(x)','mean(log(x))') would calculate the mean, median, and mean of the log for each variable.

summ treats as special two vtable functions: propNA(x) and countNA(x), which give the proportion and count of NA values in the variable, respectively. These two functions are always reported first, and are the only functions that include NA values in their calculations.

lush

The default vtable settings may not be to your liking, and in particular you may prefer more information. Setting lush = TRUE is an easy way to get more information. It will force char.values and missing to TRUE, and will also set a default summ value of c('mean(x)', 'sd(x)', 'nuniq(x)').


The labeltable() function

vtable() syntax follows the following outline:

labeltable(var,
    ...,
    out=NA,
    file=NA, 
    desc=NA)

labeltable() is a function that shows the values that correspond to var. This could be value label values, or it could be the values found in the data for the ... variables.

#Include a single labelled variable to show how the values of that variable correspond to its value labels.
library(sjlabelled)
data(efc)
labeltable(efc$e15relat)

#Include more than one variable to show, for each value of the first, what values of the others are present in the data.
data(mtcars)
labeltable(mtcars$cyl,mtcars$carb,mtcars$am)

out

The out option determines what will be done with the resulting label table file. There are several options for out:

Option Result
browser Loads variable documentation in web browser.
viewer Loads variable documentation in Viewer pane (RStudio only).
htmlreturn Returns HTML code for variable documentation file.
return Returns variable documentation table in data frame format.

file

The file argument will write the variable documentation file to an HTML file and save it. Will automatically append ‘html’ filetype if the filename does not include a period.

desc

desc will include a description of the data set in the variable documentation file, which may be useful for documentation purposes.


The dftoHTML() function

dftoHTML() syntax follows the following outline:

dftoHTML(data,out=NA,file=NA,col.width=NA,row.names=FALSE)

dftoHTML() largely exists to serve vtable(). What it does is takes a data set data and returns an HTML table with the contents of that data.

Outside of its use in vtable(), dftoHTML() can also be used to keep a view of the data file open while working on the data, avoiding repeated calls to head() or similar, or switching back and forth between code tabs and data view tabs.


data

dftoHTML() will accept any data set with a colnames() attribute.

out

The out option determines what will be done with the resulting variable documentation file. There are several options for out:

Option Result
browser Loads HTML version of data in web browser.
viewer Loads HTML version of data in Viewer pane (RStudio only).
htmlreturn Returns HTML code for data.

By default, vtable will select ‘viewer’ if running in RStudio, and ‘browser’ otherwise.

file

The file argument will write the HTML version of data to an HTML file and save it. Will automatically append ‘html’ filetype if the filename does not include a period.

col.width

dftoHTML() will select, by default, equal column widths for all columns in data. col.width, as a vector of percentage column widths on the 0-100 scale, will override these defaults.

row.names

The row.names flag determines whether the row names of the data are included as the first column in the output table.


Shortcut Helper Functions

vtable includes three shortcut functions. These are generally intended for use with vtable’s summ option because nested functions don’t look very nice in a vtable. But you probably commonly use these nested functions on your own, so they have been exported for use.

countNA

countNA(x) returns sum(is.na(x)), the number of missing observations in the vector.

propNA

propNA(x) returns mean(is.na(x)), the proportion of missing observations in the vector.

nuniq

nuniq(x) returns length(unique(x)), the number of unique values in the vector.