Skip to content

Instantly share code, notes, and snippets.

@btupper
Created August 28, 2019 13:48
Show Gist options
  • Save btupper/bc93f9222e9811331fd7ce93d4f163a6 to your computer and use it in GitHub Desktop.
Save btupper/bc93f9222e9811331fd7ce93d4f163a6 to your computer and use it in GitHub Desktop.
Simplified roxygen style documentation for data frames
#' Build out an empty itemization table for a data frame
#'
#' @param x data.frame or tibble
#' @return character string, but it is printed, too
itemize_table <- function(x){
klass <- sapply(x, function(x) class(x)[[1]])
varnames <- names(klass)
s <- c("\\itemize{",
sprintf(" \\item{%s %s }", varnames, klass),
"}")
cat(s, sep = "\n")
invisible(s)
}
#' Build out an empty enumeration table for a data frame
#'
#' @param x data.frame or tibble
#' @return character string, but it is printed, too
enumerate_table <- function(x){
klass <- sapply(x, function(x) class(x)[[1]])
varnames <- names(klass)
s <- c("\\enumerate{",
sprintf(" \\item{%s %s }", varnames, klass),
"}")
cat(s, sep = "\n")
invisible(s)
}
#' Build out an empty description table for a data frame
#'
#' @param x data.frame or tibble
#' @return character string, but it is printed, too
describe_table <- function(x){
klass <- sapply(x, function(x) class(x)[[1]])
varnames <- names(klass)
s <- c("\\describe{",
sprintf(" \\item{%s}{%s }", varnames, klass),
"}")
cat(s, sep = "\n")
invisible(s)
}
#' Build out empty table documentation for a data frame
#'
#' @param x data.frame or tibble
#' @param form charcater, one of 'itemize', "enumerate" or "describe"
#' @return character string, but it is printed, too
#' @examples
#' \dontrun{
#' > document_table(iris)
#' # \itemize{
#' # \item{Sepal.Length numeric }
#' # \item{Sepal.Width numeric }
#' # \item{Petal.Length numeric }
#' # \item{Petal.Width numeric }
#' # \item{Species factor }
#' # }
#' }
document_table <- function(x, form = c("itemize", "enumerate", "describe")[1]){
s <- switch(tolower(form[1]),
"itemize" = itemize_table(x),
"enumerate" = enumerate_table(x),
"describe" = describe_table(x),
stop("form not known:", form[1]))
invisible(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment