Skip to content

Commit 53dde18

Browse files
authored
Merge pull request #266 from Crunch-io/codebook_fix
Adjustments to accomodate siunitx updates
2 parents 75b07ce + d767b94 commit 53dde18

8 files changed

+65
-13
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Description: In order to generate custom survey reports, this package provides
66
'banners' (cross-tabulations) and codebooks of datasets in the Crunch
77
(<https://crunch.io/>) web service. Reports can be written in 'PDF' format
88
using 'LaTeX' or in Microsoft Excel '.xlsx' files.
9-
Version: 2.0.0
9+
Version: 2.0.1
1010
Authors@R: c(
1111
person("Persephone", "Tsebelis", role="aut"),
1212
person("Kamil", "Sedrowicz", role="aut"),
@@ -28,6 +28,7 @@ Imports:
2828
methods,
2929
openxlsx,
3030
rlang,
31+
stringr,
3132
tinytex
3233
Suggests:
3334
arrow,

NAMESPACE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ S3method(tableHeader,CrossTabVar)
3939
S3method(tableHeader,ToplineCategoricalArray)
4040
S3method(tableHeader,ToplineVar)
4141
S3method(tableHeader,default)
42+
export(adjustCrunchAlias)
43+
export(adjustCrunchDescription)
44+
export(adjustCrunchName)
4245
export(banner)
4346
export(catArrayToCategoricals)
4447
export(codeBookItemBody)
@@ -106,6 +109,7 @@ importFrom(stats,median)
106109
importFrom(stats,pnorm)
107110
importFrom(stats,quantile)
108111
importFrom(stats,weighted.mean)
112+
importFrom(stringr,str_extract)
109113
importFrom(utils,capture.output)
110114
importFrom(utils,head)
111115
importFrom(utils,installed.packages)

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## crunchtabs 2.0.1
2+
3+
- Adjustments for siunitx latex package updates
4+
- Adding code for codebook adjustments where identifiers (crunch alias, crunch name) are long
5+
- Fix negative signs on table labels for categorical array and multiple response variables
6+
17
## crunchtabs 2.0.0
28

39
- 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.

R/adjustIdentifiers.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#'
2+
#' @export
3+
adjustCrunchName <- function(nm) {
4+
nm <- gsub("Level of agreement (5-point scale): ","", nm, fixed = T)
5+
nm <- gsub('"', '', nm)
6+
if(nchar(nm) > 40) {
7+
nm <- substr(nm, 1, 37)
8+
nm <- paste0(nm, "...")
9+
}
10+
nm
11+
}
12+
13+
#' @export
14+
adjustCrunchAlias <- function(alias) {
15+
if(nchar(alias) > 35) {
16+
alias <- substr(alias, 1, 32)
17+
alias <- paste0(alias, "...")
18+
}
19+
alias
20+
}
21+
22+
#' @export
23+
adjustCrunchDescription <- function(nm, alias, description) {
24+
nm <- gsub("Level of agreement (5-point scale): ","", nm, fixed = T)
25+
nm <- gsub('"', '', nm)
26+
27+
if(nchar(nm) > 40 & !grepl(nm, description)) {
28+
description <- paste0(nm, description)
29+
}
30+
31+
if(nchar(alias) > 35 & description != "") {
32+
description <- paste0(description, " \n(", alias,")")
33+
} else if(description == "") {
34+
description <- paste0("(", alias, ")")
35+
}
36+
37+
description
38+
}

R/codeBookItemBody.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,15 @@ codeBookItemBody.CategoricalVariable <- function(x, meta = NULL, ...) { # nolint
125125
}
126126

127127
#' @describeIn codeBookItemBody Creates item body for CategoricalArrayVariable
128+
#' @importFrom stringr str_extract
128129
#' @export
129130
codeBookItemBody.CategoricalArrayVariable <- function(x, meta = NULL, ...) { # nolint
130131
k <- codeBookSummary(x)
131132
k[, 1] <- texEscape(k[, 1])
132133
k[, 2] <- texEscape(k[, 2])
133134

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

137138
rownames(k) <- NULL
138139

R/codeBookLatex.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#' @export
99
codeBookItemTxtHeader <- function(x, ...) {
1010
txt <- list()
11-
txt$name <- crunch::name(x)
12-
txt$alias <- crunch::alias(x)
11+
txt$name <- adjustCrunchName(crunch::name(x))
12+
txt$alias <- adjustCrunchAlias(crunch::alias(x))
13+
14+
1315

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

@@ -65,9 +67,9 @@ codeBookItemTxtHeader <- function(x, ...) {
6567
#' @export
6668
codeBookItemTxtDescription <- function(x, ...) {
6769
txt <- list()
68-
txt$description <- crunch::description(x)
70+
txt$description <- adjustCrunchDescription(crunch::name(x), crunch::alias(x), crunch::description(x))
6971
txt$notes <- crunch::notes(x)
70-
txt$alias <- crunch::alias(x)
72+
txt$alias <- adjustCrunchAlias(crunch::alias(x))
7173
txt$alias_toc <- ifelse(
7274
nchar(txt$alias) > 20,
7375
paste0(substr(txt$alias, 1, 22), "..."),
@@ -210,7 +212,7 @@ scolumnAlign <- function(k, alignment) {
210212
if (maxnchar > 6) {
211213
alignment[i] <- sprintf("S[table-format=%s]", maxnchar)
212214
} else {
213-
alignment[i] <- c("J", "K", "d", "M", "N", "O", "L", "M", "N")[maxnchar]
215+
alignment[i] <- c("J", "K", "d", "M", "N", "O", "L", "P", "Q")[maxnchar]
214216
}
215217
}
216218
}

inst/codebook_latex_wrap.tex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
\usepackage{longtable}
1515
\usepackage{siunitx}
1616
\sisetup{
17-
round-mode = places, % Rounds numbers
17+
% round-mode = places, % Rounds numbers
1818
round-precision = 2, % to 2 places
1919
table-format = 3
2020
}
@@ -36,8 +36,8 @@
3636
\newcolumntype{N}{S[table-format=5]}
3737
\newcolumntype{O}{S[table-format=6]}
3838
\newcolumntype{L}{S[table-format=7]}
39-
\newcolumntype{M}{S[table-format=8]}
40-
\newcolumntype{N}{S[table-format=9]}
39+
\newcolumntype{P}{S[table-format=8]}
40+
\newcolumntype{Q}{S[table-format=9]}
4141
% default is 3 so it's not included above
4242
\usepackage{float}
4343
\usepackage{marginnote}

tests/testthat/test-codeBookItemTxtDescription.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test_that("codeBookItemTxtDescription NumericVariable", {
3434

3535
expect_equal(
3636
res,
37-
"\\vskip 0.10in\n\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{ndogs}} Number of dogs}\n\\vskip 0.10in" # nolint
37+
"\\vskip 0.10in\n(ndogs)\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{ndogs}} Number of dogs}\n\\vskip 0.10in" # nolint
3838
)
3939
})
4040

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

4444
expect_equal(
4545
res,
46-
"\\vskip 0.10in\n\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{wave}} Wave}\n\\vskip 0.10in" # nolint
46+
"\\vskip 0.10in\n(wave)\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{wave}} Wave}\n\\vskip 0.10in" # nolint
4747
)
4848
})
4949

0 commit comments

Comments
 (0)