Skip to content

Commit

Permalink
Merge pull request #266 from Crunch-io/codebook_fix
Browse files Browse the repository at this point in the history
Adjustments to accomodate siunitx updates
  • Loading branch information
1beb authored Oct 20, 2022
2 parents 75b07ce + d767b94 commit 53dde18
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 13 deletions.
3 changes: 2 additions & 1 deletion 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) and codebooks 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: 2.0.0
Version: 2.0.1
Authors@R: c(
person("Persephone", "Tsebelis", role="aut"),
person("Kamil", "Sedrowicz", role="aut"),
Expand All @@ -28,6 +28,7 @@ Imports:
methods,
openxlsx,
rlang,
stringr,
tinytex
Suggests:
arrow,
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ S3method(tableHeader,CrossTabVar)
S3method(tableHeader,ToplineCategoricalArray)
S3method(tableHeader,ToplineVar)
S3method(tableHeader,default)
export(adjustCrunchAlias)
export(adjustCrunchDescription)
export(adjustCrunchName)
export(banner)
export(catArrayToCategoricals)
export(codeBookItemBody)
Expand Down Expand Up @@ -106,6 +109,7 @@ importFrom(stats,median)
importFrom(stats,pnorm)
importFrom(stats,quantile)
importFrom(stats,weighted.mean)
importFrom(stringr,str_extract)
importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,installed.packages)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## crunchtabs 2.0.1

- Adjustments for siunitx latex package updates
- Adding code for codebook adjustments where identifiers (crunch alias, crunch name) are long
- Fix negative signs on table labels for categorical array and multiple response variables

## crunchtabs 2.0.0

- Rounding in variables summaries changed to match Crunch method of rounding, rounding half to up rather than rounding half to even. This ensures summaries shown in reports match Crunch variable summaries.
Expand Down
38 changes: 38 additions & 0 deletions R/adjustIdentifiers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#'
#' @export
adjustCrunchName <- function(nm) {
nm <- gsub("Level of agreement (5-point scale): ","", nm, fixed = T)
nm <- gsub('"', '', nm)
if(nchar(nm) > 40) {
nm <- substr(nm, 1, 37)
nm <- paste0(nm, "...")
}
nm
}

#' @export
adjustCrunchAlias <- function(alias) {
if(nchar(alias) > 35) {
alias <- substr(alias, 1, 32)
alias <- paste0(alias, "...")
}
alias
}

#' @export
adjustCrunchDescription <- function(nm, alias, description) {
nm <- gsub("Level of agreement (5-point scale): ","", nm, fixed = T)
nm <- gsub('"', '', nm)

if(nchar(nm) > 40 & !grepl(nm, description)) {
description <- paste0(nm, description)
}

if(nchar(alias) > 35 & description != "") {
description <- paste0(description, " \n(", alias,")")
} else if(description == "") {
description <- paste0("(", alias, ")")
}

description
}
5 changes: 3 additions & 2 deletions R/codeBookItemBody.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,15 @@ codeBookItemBody.CategoricalVariable <- function(x, meta = NULL, ...) { # nolint
}

#' @describeIn codeBookItemBody Creates item body for CategoricalArrayVariable
#' @importFrom stringr str_extract
#' @export
codeBookItemBody.CategoricalArrayVariable <- function(x, meta = NULL, ...) { # nolint
k <- codeBookSummary(x)
k[, 1] <- texEscape(k[, 1])
k[, 2] <- texEscape(k[, 2])

code_labels <- texEscape(gsub("[0-9]+ ", "", names(k))[-c(1, 2)])
code_numbers <- trimws(sub("\\D*(\\d+).*", "\\1", names(k)))[-c(1, 2)]
code_labels <- texEscape(gsub("^-?[0-9]+ ", "", names(k))[-c(1, 2)])
code_numbers <- trimws(stringr::str_extract(names(k), "^-?[0-9]*"))[-c(1, 2)]

rownames(k) <- NULL

Expand Down
12 changes: 7 additions & 5 deletions R/codeBookLatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#' @export
codeBookItemTxtHeader <- function(x, ...) {
txt <- list()
txt$name <- crunch::name(x)
txt$alias <- crunch::alias(x)
txt$name <- adjustCrunchName(crunch::name(x))
txt$alias <- adjustCrunchAlias(crunch::alias(x))



tex <- "\\textbf{%s}\\hfill\\textbf{\\ttfamily{%s}}\n\n{\\small %s}\n\n"

Expand Down Expand Up @@ -65,9 +67,9 @@ codeBookItemTxtHeader <- function(x, ...) {
#' @export
codeBookItemTxtDescription <- function(x, ...) {
txt <- list()
txt$description <- crunch::description(x)
txt$description <- adjustCrunchDescription(crunch::name(x), crunch::alias(x), crunch::description(x))
txt$notes <- crunch::notes(x)
txt$alias <- crunch::alias(x)
txt$alias <- adjustCrunchAlias(crunch::alias(x))
txt$alias_toc <- ifelse(
nchar(txt$alias) > 20,
paste0(substr(txt$alias, 1, 22), "..."),
Expand Down Expand Up @@ -210,7 +212,7 @@ scolumnAlign <- function(k, alignment) {
if (maxnchar > 6) {
alignment[i] <- sprintf("S[table-format=%s]", maxnchar)
} else {
alignment[i] <- c("J", "K", "d", "M", "N", "O", "L", "M", "N")[maxnchar]
alignment[i] <- c("J", "K", "d", "M", "N", "O", "L", "P", "Q")[maxnchar]
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions inst/codebook_latex_wrap.tex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
\usepackage{longtable}
\usepackage{siunitx}
\sisetup{
round-mode = places, % Rounds numbers
% round-mode = places, % Rounds numbers
round-precision = 2, % to 2 places
table-format = 3
}
Expand All @@ -36,8 +36,8 @@
\newcolumntype{N}{S[table-format=5]}
\newcolumntype{O}{S[table-format=6]}
\newcolumntype{L}{S[table-format=7]}
\newcolumntype{M}{S[table-format=8]}
\newcolumntype{N}{S[table-format=9]}
\newcolumntype{P}{S[table-format=8]}
\newcolumntype{Q}{S[table-format=9]}
% default is 3 so it's not included above
\usepackage{float}
\usepackage{marginnote}
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-codeBookItemTxtDescription.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test_that("codeBookItemTxtDescription NumericVariable", {

expect_equal(
res,
"\\vskip 0.10in\n\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{ndogs}} Number of dogs}\n\\vskip 0.10in" # nolint
"\\vskip 0.10in\n(ndogs)\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{ndogs}} Number of dogs}\n\\vskip 0.10in" # nolint
)
})

Expand All @@ -43,7 +43,7 @@ test_that("codeBookItemTxtDescription DatetimeVariable", {

expect_equal(
res,
"\\vskip 0.10in\n\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{wave}} Wave}\n\\vskip 0.10in" # nolint
"\\vskip 0.10in\n(wave)\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{wave}} Wave}\n\\vskip 0.10in" # nolint
)
})

Expand Down

0 comments on commit 53dde18

Please sign in to comment.