-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from Crunch-io/issue_069
WIP: New summarization methods for crunchtabs toplines
- Loading branch information
Showing
30 changed files
with
1,071 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#' Generate a codebook item | ||
#' | ||
#' A passthrough function that creates a table header appropriate to the class | ||
#' of the data object being passed. Expected classes are: | ||
#' | ||
#' * CategoricalVariable | ||
#' * CategoricalArrayVariable | ||
#' * MultipleResponseVariable | ||
#' * NumericVariable | ||
#' * TextVariable | ||
#' * DatetimeVariable | ||
#' | ||
#' Importantly, this also controls the relative widths of the columns. | ||
#' | ||
#' @md | ||
#' @param x An object of one of the types listed | ||
#' @export | ||
codebookItem <- function(x, ...) { | ||
UseMethod("codebookItem", x) | ||
} | ||
|
||
#' @rdname codebookItem | ||
codebookItem.default <- function(x) { | ||
wrong_class_error(x, c("CategoricalVariable", "CategoricalArrayVariable", "MultipleResponseVariable", "TextVariable", "NumericVariable", "DatetimeVariable"), "codebookItem") | ||
} | ||
|
||
|
||
#' Extract basic question information | ||
#' | ||
#' Extracts the following: | ||
#' | ||
#' * body | ||
#' * alias | ||
#' * description or question text | ||
#' * notes or filter text | ||
#' * id | ||
#' | ||
#' @md | ||
#' @export | ||
codebookItemTxt <- function(x) { | ||
txt <- x@tuple@body | ||
l <- list() | ||
l$title <- txt$alias | ||
l$alias <- txt$alias | ||
l$name <- txt$name | ||
l$id <- txt$id | ||
l$filter_text <- txt$notes | ||
l$question <- txt$description | ||
l | ||
} | ||
|
||
#' | ||
codebookItemSubVars <- function (x) { | ||
sv <- subvariables(x) | ||
als <- unname(unlist(lapply(sv@index, getElement, "alias"))) | ||
resp <- unname(unlist(lapply(sv@index, getElement, "name"))) | ||
sv <- data.frame(`Sub Alias` = als, Name = resp) | ||
sv_responses <- categories(x) | ||
|
||
list( | ||
key = sv, | ||
key2 = setNames(sv_responses, c("Response", "Value")) | ||
) | ||
|
||
} | ||
|
||
|
||
#' @describeIn codebookItem Prepares a codebookItem for a CategoricalVariable | ||
#' @export | ||
codebookItem.CategoricalVariable <- function(x) { | ||
txt <- codebookItemTxt(x) | ||
cats <- categories(x) | ||
responses <- do.call(rbind, cats@.Data) | ||
responses <- as.data.frame(responses[!unlist(responses[, "missing"]), ]) | ||
responses <- lapply(responses, unlist) | ||
if (all(is.null(responses$numeric_value))) | ||
responses$numeric_value = responses$id | ||
responses <- as.data.frame(lapply(responses, unlist)[c("name", "numeric_value")]) | ||
names(responses) <- c("Response", "Value") | ||
|
||
latexTop <- sprintf(" | ||
\\thispagestyle{fancy} | ||
\\lhead{%s} | ||
\\setlength{\\extrarowheight}{20pt} | ||
\\begin{tabular*}{7in}{p{2.5in}p{4.5in}} | ||
Question & %s \\\\ | ||
Name & %s \\\\ | ||
Alias & %s \\\\ | ||
ID & %s \\\\ | ||
Filtering Notes & %s \\\\ | ||
\\end{tabular*} | ||
", | ||
txt$meta$name, | ||
txt$meta$question, | ||
txt$meta$name, | ||
txt$meta$alias, | ||
txt$meta$id, | ||
ifelse(txt$meta$filter_text == "", "None", txt$meta$filter_text) | ||
) | ||
latexTop <- gsub(" ", "", latexTop) | ||
|
||
latexResponse | ||
|
||
latexTop | ||
} | ||
|
||
#' @describeIn codebookItem Prepares a codebookitem for a CategoricalArrayVariable | ||
#' @export | ||
codebookItem.CategoricalArrayVariable <- function(x) { | ||
txt = codebookItemTxt(x) | ||
subvars = codebookItemSubVars(x) | ||
|
||
|
||
} | ||
|
||
#' @describeIn codebookItem Prepares a codebookitem for a MultipleResponseVariable | ||
#' @export | ||
codebookItem.MultipleResponseVariable <- function(x) { | ||
|
||
} | ||
|
||
#' @describeIn codebookItem Prepares a codebookitem for a NumericVariable | ||
#' @export | ||
codebookItem.NumericVariable <- function(x) { | ||
tmp <- c(summary(c_var), SD = sd(c_var, na.rm = TRUE)) | ||
tmp <- tibble::rownames_to_column(as.data.frame(tmp)) | ||
colnames(tmp) <- c("Summary", "Value") | ||
tmp$Value <- round(tmp$Value, 2) | ||
} | ||
|
||
#' @describeIn codebookItem Prepares a codebookitem for a TextVariable | ||
#' @export | ||
codebookItem.TextVariable <- function(x) { | ||
|
||
} | ||
|
||
#' @describeIn codebookItem Prepares a codebookitem for a DatetimeVariable | ||
#' @export | ||
codebookItem.DatetimeVariable <- function(x) { | ||
|
||
} | ||
|
||
#' Generate LaTeX CodeBooks | ||
#' | ||
#' \code{writeCodebook} produces publication-quality LaTeX reports | ||
#' | ||
#' @param ds A crunch dataset | ||
#' @param ds | ||
#' | ||
#' @param ... Additional arguments passed to writeLatx | ||
#' | ||
#' @importFrom utils installed.packages | ||
#' @export | ||
writeCodebook <- function(...) { | ||
|
||
writeLatex(...) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.