#' Get the names and labels of a labelled dataframe
#'
#' @param df a labelled dataframe
#'
#' @return A dataframe containing the names and labels of the dataset
get_names_and_labels <- function(df) {
lbls <- sapply(df, function(x) attr(x, "label"))
nms <- names(df)
df <- cbind(nms, lbls)
row.names(df) <- NULL
df
}
#' Autogenerate documentation from the AES survey response dataset
#'
#' @param df A labelled AES dataset
make_docs <- function(df) {
var_name <- deparse(substitute(df))
df <- get_names_and_labels(df)
cat("#' Title\n")
cat("#' \n")
cat("#' \\itemize{\n")
cat("#' \\item Total sample: \n")
cat("#' \\item Valid responses: \n")
cat("#' \\item Effective response rate: \n")
cat("#' }\n")
cat("#' \n")
cat("#' @details\n")
cat("#' \n")
cat("#' \n")
cat("#' \n")
cat("#' \\strong{Useful Links}\n")
cat("#' \\itemize{\n")
cat("#' \\item \\href{}{Original codebook} \n")
cat("#' \\item \\href{}{Original questionnaire} \n")
cat("#' }\n")
cat("#' \n")
cat("#' @source \\url{http://www.australianelectionstudy.org/voter_studies.html}\n")
cat("#' \n")
cat("#' @section Variables:\n")
cat("#' \n")
cat("#' Note: this documentation has been autogenerated from the dataset labels not the original codebook.\n")
cat("#' \n")
cat("#' \\describe{\n")
for (i in seq_along(df[,1])) {
out_string <- paste0("#' \\item{", df[[i,1]], "}", "{", df[[i,2]], "}", "\n")
cat(out_string)
}
cat("#' }\n")
cat(paste0('"', var_name, '"'), "\n")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.