Skip to content

Commit

Permalink
major updates for codebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
1beb committed Jun 8, 2020
1 parent 70c0467 commit 9e47cc1
Show file tree
Hide file tree
Showing 112 changed files with 24,462 additions and 496 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: In order to generate custom survey reports, this package provides
'banners' (cross-tabulations) of datasets in the Crunch
(<https://crunch.io/>) web service. Reports can be written in 'PDF' format
using 'LaTeX' or in Microsoft Excel '.xlsx' files.
Version: 1.2.6
Version: 1.2.7
Authors@R: c(
person("Persephone", "Tsebelis", role="aut"),
person("Kamil", "Sedrowicz", role="aut"),
Expand All @@ -23,8 +23,7 @@ Imports:
openxlsx,
digest,
methods,
tinytex,
glue
tinytex
Suggests:
covr,
httptest (>= 2.0.0),
Expand Down
22 changes: 11 additions & 11 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ S3method(calculateIfLongtable,CrossTabVar)
S3method(calculateIfLongtable,ToplineCategoricalArray)
S3method(calculateIfLongtable,ToplineVar)
S3method(calculateIfLongtable,default)
S3method(codeBookItem,CategoricalArrayVariable)
S3method(codeBookItem,CategoricalVariable)
S3method(codeBookItem,DatetimeVariable)
S3method(codeBookItem,MultipleResponseVariable)
S3method(codeBookItem,NumericVariable)
S3method(codeBookItem,TextVariable)
S3method(codeBookItemBody,CategoricalArrayVariable)
S3method(codeBookItemBody,CategoricalVariable)
S3method(codeBookItemBody,DatetimeVariable)
S3method(codeBookItemBody,MultipleResponseVariable)
S3method(codeBookItemBody,NumericVariable)
S3method(codeBookItemBody,TextVariable)
S3method(codeBookItemBody,default)
S3method(codeBookSummary,CategoricalArrayVariable)
S3method(codeBookSummary,CategoricalVariable)
S3method(codeBookSummary,DatetimeVariable)
Expand All @@ -34,11 +35,9 @@ S3method(tableHeader,ToplineCategoricalArray)
S3method(tableHeader,ToplineVar)
S3method(tableHeader,default)
export(banner)
export(bcodeBookItemBody)
export(bcodeBookItemTxtDescription)
export(bwriteCodeBook)
export(codeBookItem)
export(codeBookItemTxt)
export(codeBookItemBody)
export(codeBookItemTxtDescription)
export(codeBookItemTxtHeader)
export(codeBookSummary)
export(crosstabs)
export(getName)
Expand All @@ -53,6 +52,7 @@ export(themeHuffPoToplines)
export(themeNew)
export(themeUKPolitical)
export(with_api_fixture)
export(writeCodeBook)
export(writeExcel)
export(writeLatex)
import(crunch)
Expand Down
65 changes: 43 additions & 22 deletions R/codeBookSummary.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ codeBookSummary.default <- function(x, ...) {
"TextVariable",
"NumericVariable",
"DatetimeVariable"),
"codebookItem")
"codeBookSummary")
}

#' @describeIn codeBookSummary Prepares a codeBookSummary data.frame for a CategoricalVariable
Expand Down Expand Up @@ -72,18 +72,13 @@ codeBookSummary.CategoricalVariable <- function(x, multiple = FALSE, ...) {
res <- merge(l, smry)

if (multiple) {
res[with(res, order(value)),]
res[with(res, order(id)),]
} else {
ln <- length(res$name)

matrix(c(
rep("", ln),
res$value,
res = matrix(c(
res$id,
res$name,
rep("", ln),
res$n,
rep("", ln)
), ncol = 6)
res$n
), ncol = 3)
}

}
Expand All @@ -98,14 +93,18 @@ codeBookSummary.MultipleResponseVariable <- function(x, ...) {
}

names(responses) <- subvars
nms <- c("", "", paste(responses[[1]]$value, responses[[1]]$name))

rws <- length(responses)
cols <- nrow(responses[[1]])
frame = unique(do.call(rbind, responses)[,c('name', 'id')])
nms <- c("", "", paste(frame$id, frame$name))
cols <- nrow(frame)
m <- matrix(rep(NA, (rws)*(cols + 2)), ncol = cols + 2, nrow = rws)


for (i in 1:rws) {
m[i,3:length(nms)] <- responses[[i]]$n
# We merge on a complete frame because responses
# can be missing from categories
responses_adj <- merge(frame, responses[[i]], all.x = TRUE)$n
m[i,3:(cols + 2)] <- responses_adj
}

r <- data.frame(m)
Expand All @@ -126,11 +125,11 @@ codeBookSummary.NumericVariable <- function(x, ...) {
maxima <- round(max(x, na.rm = T), 2)
missings <- sum(is.na(as.vector(x)))

type_row <- c("", "Type", "Numeric", "")
range_row <- c("", "Range", paste0("[", minima,", ", maxima,"]"), "")
type_row <- c("Type", "Numeric")
range_row <- c("Range", paste0("[", minima,", ", maxima,"]"))

if (missings > 0) {
missings_row <- c("", "Missing", missings, "")
missings_row <- c("Missing", missings)
r <- rbind(
type_row, missings_row, range_row
)
Expand All @@ -149,9 +148,9 @@ codeBookSummary.NumericVariable <- function(x, ...) {
#' @export
codeBookSummary.TextVariable <- function(x, ...) {

filled <- sum(as.vector(x) != "" | !is.na(as.vector(x)))
type_row <- c("", "Type", "Text", "")
filled <- c("", "Filled", filled, "")
filled <- sum(as.vector(x) != "" | !is.na(as.vector(x)), na.rm = TRUE)
type_row <- c("Type", "Text")
filled <- c("Filled", filled)

r <- rbind(type_row, filled)
rownames(r) <- NULL
Expand All @@ -160,4 +159,26 @@ codeBookSummary.TextVariable <- function(x, ...) {

#' @describeIn codeBookSummary Prepares a codeBookSummary data.frame for a DatetimeVaraible
#' @export
codeBookSummary.DatetimeVariable <- codeBookSummary.NumericVariable
codeBookSummary.DatetimeVariable <- function(x, ...) {
minima <- min(x, na.rm = T)
maxima <- max(x, na.rm = T)
missings <- sum(is.na(as.vector(x)))

type_row <- c("Type", "Datetime")
range_row <- c("Range", paste0("[", minima,", ", maxima,"]"))

if (missings > 0) {
missings_row <- c("Missing", missings)
r <- rbind(
type_row, missings_row, range_row
)
} else {
r <- rbind(
type_row, range_row
)
}

rownames(r) <- NULL
r

}
Loading

0 comments on commit 9e47cc1

Please sign in to comment.