Skip to content

Commit bc0b91a

Browse files
authored
Merge pull request #188 from Crunch-io/issue_185
Release 1.2.9
2 parents 2f1d9df + dcb6bd6 commit bc0b91a

18 files changed

+222
-33
lines changed

DESCRIPTION

Lines changed: 2 additions & 3 deletions
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) 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: 1.2.8
9+
Version: 1.2.9
1010
Authors@R: c(
1111
person("Persephone", "Tsebelis", role="aut"),
1212
person("Kamil", "Sedrowicz", role="aut"),
@@ -15,7 +15,6 @@ Authors@R: c(
1515
URL: https://github.com/Crunch-io/crunchtabs
1616
BugReports: https://github.com/Crunch-io/crunchtabs/issues
1717
License: LGPL (>= 3)
18-
Remotes: haozhu233/kableExtra
1918
Depends:
2019
R (>= 3.5.0),
2120
crunch
@@ -35,6 +34,6 @@ Suggests:
3534
rmarkdown,
3635
testthat (>= 2.1.0),
3736
kableExtra (>= 1.1.0.9000)
38-
RoxygenNote: 7.1.0
37+
RoxygenNote: 7.1.1
3938
VignetteBuilder: knitr
4039
Encoding: UTF-8

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## crunchtabs 1.2.9
2+
3+
- Codebook question descriptions now appropriately escape special characters (#187)
4+
- Added option enforce_onehundred which allows one to avoid rounding errors in totals rows (#189)
5+
- Codebook table of contents overruns, cutting text and adding "..." (#186)
6+
- Codebook generation now supports a filepath (#185)
7+
- Added vertical space before append_text (#182)
8+
- Bugfix: append_text that is multiple lines du0lpicated vertical space. Collapsing. (#191)
9+
- Remove requirement for dev version of kableExtra (#184)
10+
111
## crunchtabs 1.2.8
212

313
- Documentation for generating codebooks (#180)

R/codebookLatex.R

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,37 @@ codeBookItemTxtDescription <- function(x, ...) {
6464
txt$description <- crunch::description(x)
6565
txt$notes <- crunch::notes(x)
6666
txt$alias <- crunch::alias(x)
67+
txt$alias_toc <- ifelse(
68+
nchar(txt$alias) > 20,
69+
paste0(substr(txt$alias, 1, 22), "..."),
70+
txt$alias
71+
)
72+
6773
txt$name <- crunch::name(x)
6874

75+
txt$name_toc <- ifelse(
76+
nchar(txt$name) > 65,
77+
paste0(substr(txt$name, 1, 65), "..."),
78+
txt$name
79+
)
80+
81+
6982
if (txt$notes != "") {
7083
tex = "\\vskip 0.10in\n%s\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{%s}} %s}\n\\vskip 0.10in\n\\emph{%s}\n\\vskip 0.10in"
7184
tex = sprintf(
7285
tex,
73-
txt$description,
74-
texEscape(txt$alias),
75-
texEscape(txt$name),
86+
texEscape(txt$description),
87+
texEscape(txt$alias_toc),
88+
texEscape(txt$name_toc),
7689
txt$notes
7790
)
7891
} else {
7992
tex = "\\vskip 0.10in\n%s\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{%s}} %s}\n\\vskip 0.10in"
8093
tex = sprintf(
8194
tex,
82-
txt$description,
83-
texEscape(txt$alias),
84-
texEscape(txt$name)
95+
texEscape(txt$description),
96+
texEscape(txt$alias_toc),
97+
texEscape(txt$name_toc)
8598
)
8699
}
87100

R/reformatResults.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ reformatVar <- function(var, banner_name, theme, proportions, banner_info, latex
203203
if (dt %in% "totals_row") {
204204
if (proportions) {
205205
data_tmp <- colSums(data)
206+
if (theme$enforce_onehundred) {
207+
data_tmp[data_tmp < 100 | data_tmp > 100] = 100
208+
}
206209
} else {
207210
data_tmp <- getItemData(
208211
data = var$crosstabs[[banner_name]],

R/tex.R

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@ texEscape <- function(string) {
3434
if (is.null(string)) {
3535
return("")
3636
}
37-
# TODO: Change to one line rather than nested gsubs(), yuck.
38-
gsub("^ *(\\[)", "\\\\hspace\\*\\{0in\\}\\1", # Trim leading whitespace
39-
gsub("([#$%&_])", "\\\\\\1", # Escape special characters
40-
gsub("[\u00A3\uFFE1]", "\\\\pounds", # Handle GBP currency
41-
gsub("\n", " \\\\newline ", # Turn newlines into \newlines
42-
string
43-
)
44-
)
45-
)
46-
)
37+
38+
string <- gsub("^ *(\\[)", "\\\\hspace\\*\\{0in\\}\\1", string)
39+
string <- gsub("([#$%&_])", "\\\\\\1", string)
40+
string <- gsub("[\u00A3\uFFE1]", "\\\\pounds", string)
41+
string <- gsub("\n", " \\\\newline ", string)
42+
string
4743
}
4844

4945
#' Font Size

R/theme.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ themeDefaultExcel <- function(
209209
digits_numeric = 2,
210210
one_per_sheet = FALSE,
211211
latex_round_percentages = TRUE,
212+
enforce_onehundred = FALSE,
212213
latex_headtext = "",
213214
latex_foottext = "",
214215
latex_table_align = "r",
@@ -261,6 +262,7 @@ themeDefaultLatex <- function(font = getOption("font", default = "helvet"),
261262
excel_freeze_column = 0,
262263
excel_orientation = "portrait",
263264
latex_round_percentages = FALSE,
265+
enforce_onehundred = FALSE,
264266
latex_headtext = "",
265267
latex_foottext = "",
266268
latex_table_align = "r",
@@ -417,6 +419,7 @@ validators_to_use <- list(
417419
latex_multirowheaderlines = c(class = "logical", len = 1, missing = FALSE, default = FALSE),
418420
latex_round_percentages = c(class = "logical", len = 1, missing = FALSE, default = FALSE),
419421
latex_round_percentages_exception = c(class = "character", len = NA, missing = TRUE),
422+
enforce_onehundred = c(class = "logical", len = 1, missing = FALSE, default = FALSE),
420423
latex_table_align = c(class = "character", len = 1, missing = FALSE, default = ""),
421424
logo = list(missing = TRUE, include = list("file", "startRow", "startCol",
422425
"width", "height", "units", "dpi")),
@@ -461,7 +464,7 @@ theme_validator <- function(theme) {
461464
"format_var_alias", "format_var_description", "format_var_filtertext",
462465
"format_var_name", "format_var_subname", "format_weighted_n", "halign",
463466
"latex_foottext", "latex_headtext", "latex_max_lines_for_tabular",
464-
"latex_multirowheaderlines", "latex_round_percentages",
467+
"latex_multirowheaderlines", "latex_round_percentages", "enforce_onehundred",
465468
"latex_round_percentages_exception", "latex_table_align", "logo",
466469
"one_per_sheet","valign", "pagebreak_in_banner")
467470

R/writeCodeBookLatex.R

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
#' @param appendix Should categorical questions with greater than 20 categories be put in an apppendix? Defaults to TRUE.
1818
#' @param logo Default to NULL. A character string one of: yougov or ygblue. Includes the logo automatically. Also accepts a path to a logo file.
1919
#' @param position Defaults to NULL. Identifies the position of the table on the page. Accepts "c", "l", or "r". Default position is left aligned tables.
20+
#' @param path The path to place .tex and .pdf files.
21+
#' @param logging Leave logs in the working directory, defaults to FALSE
2022
#' @param ... Additional arguments passed to \link[kableExtra]{kable_styling} Unused.
23+
#'
2124
#' @export
2225
writeCodeBookLatex <- function(
2326
ds, url = NULL, rmd = TRUE, pdf = TRUE, title = NULL, subtitle = NULL,
2427
table_of_contents = FALSE, sample_desc = NULL, field_period = NULL,
2528
preamble = NULL, suppress_zero_counts = FALSE, appendix = TRUE, logo = NULL,
26-
position = NULL,
29+
position = NULL, path = NULL, logging = FALSE,
2730
...) {
2831

2932
options("crunchtabs.codebook.suppress.zeros" = suppress_zero_counts)
@@ -194,10 +197,31 @@ writeCodeBookLatex <- function(
194197
# codebook <- gsub("\\begin{longtabu}", paste0("\\begin{longtabu}", replacement), codebook, fixed = TRUE)
195198
}
196199

197-
write(codebook, gsub(" ","-", paste0(name(ds), ".tex")))
200+
201+
# Issue 185 - Specify a path
202+
if (!is.null(path)) {
203+
basename <- gsub(" ","-", name(ds))
204+
texname <- paste0(path,"/", basename, ".tex")
205+
pdfname <- paste0(path, "/", basename, ".pdf")
206+
} else {
207+
basename <- gsub(" ","-", name(ds))
208+
texname <- paste0(basename, ".tex")
209+
pdfname <- paste0(basename, ".pdf")
210+
}
211+
212+
write(codebook, texname)
198213

199214
if (pdf) {
200-
tinytex::pdflatex(gsub(" ","-", paste0(name(ds), ".tex")))
201-
file.open(paste0(gsub(" ","-", paste0(name(ds))), ".pdf"))
215+
tinytex::pdflatex(texname, pdf_file = pdfname)
216+
217+
if (!logging) {
218+
files <- list.files(path = getwd())
219+
files <- grep("out$|log$|aux$", files, value = TRUE)
220+
if (length(files)) {
221+
file.remove(file.path(getwd(), files))
222+
}
223+
}
224+
225+
file.open(pdfname)
202226
}
203227
}

R/writeLatex.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#' @param moe An optional numeric margin of error.
2121
#' @param append_text An optional character string that, if supplied, will be appended after
2222
#' the final table. Useful for adding in disclosure information. Defaults to an empty string.
23-
#' @param logging add log messages
23+
#' @param logging Leave logs in the working directory, defaults to FALSE
2424
#'
2525
#' @return If \code{returndata} is set to \code{TRUE}, a processed data that was used to produce
2626
#' the report is returned. Otherwise \code{NULL} is returned.
@@ -65,7 +65,7 @@ writeLatex <- function(data_summary, theme = themeDefaultLatex(),
6565
}
6666

6767
if (!is.null(append_text)) {
68-
append_text <- paste0("\\vspace{0.5in}\n\n", append_text)
68+
append_text <- paste0("\\vspace{0.5in}\n\n", paste0(append_text, collapse = "\n"))
6969
}
7070

7171
# Now assemble the .tex document

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ writeExcel(ct_summary, filename = "output") # output.xlsx will be written
7878

7979
## Generating Codebooks
8080

81-
To generate a codebook, you must install the development version of kableExtra
81+
Generating a codebook is easy!
8282

8383
```
84-
devtools::install_github("haozhu233/kableExtra")
85-
8684
# library(crunchtabs)
8785
# login()
8886

dev-misc/codebook_visual_integration.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,21 @@ writeCodeBookLatex(ds)
8484

8585
ds <- loadDataset("Forked Huffpost")
8686
writeCodeBookLatex(ds, logo = "yougov", table_of_contents = T)
87+
88+
ds = loadDataset("https://app.crunch.io/dataset/2375608c53694a899213fe7daf7e2d1e/")
89+
90+
writeCodeBookLatex(
91+
ds,
92+
url = "https://app.crunch.io/dataset/2375608c53694a899213fe7daf7e2d1e/",
93+
table_of_contents = TRUE, logo = "yougov",
94+
pdf = TRUE
95+
)
96+
97+
98+
ds <- loadDataset("BEB Fork STAN0138")
99+
weight(ds) <- NULL
100+
writeCodeBookLatex(ds, table_of_contents = TRUE, suppress_zero_counts = TRUE,
101+
title = 'Presidential Election Study - August 2020',
102+
field_period = 'August 24-31, 2020',
103+
sample_desc = paste0(nrow(ds), ' Adults'),
104+
rmd=FALSE)

man/writeCodeBookLatex.Rd

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/writeLatex.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-codeBookItemTxtDescription.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ with_api_fixture("fixtures-1-2-5", {
5151
)
5252
})
5353

54+
test_that("codebookItemTxtDescription notes", {
55+
res = with_mock(codeBookItemTxtDescription(ds$q1), "crunch::notes" = function(x) "This is a note!")
56+
expect_equal(
57+
res,
58+
"\\vskip 0.10in\nWhat is your favorite pet?\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{q1}} Pet}\n\\vskip 0.10in\n\\emph{This is a note!}\n\\vskip 0.10in")
59+
60+
})
61+
5462
})

tests/testthat/test-codeBookItemTxtHeader.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,9 @@ with_api_fixture("fixtures-1-2-5", {
5151
)
5252

5353
})
54+
55+
56+
# Test for texEscape()
57+
58+
5459
})

tests/testthat/test-tex.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
context("underline")
2+
3+
test_that("underline works as expected", {
4+
r <- underline("Hello!")
5+
expect_equal(r, "\\underline{Hello!}")
6+
})
7+
8+
context("applyLatexStyle")
9+
10+
test_that("underline or underline2", {
11+
item <- "Hello!"
12+
item_theme <- list()
13+
item_theme$decoration <- "underline"
14+
r <- applyLatexStyle(item, item_theme)
15+
expect_equal(r, "\\underline{Hello!}")
16+
})
17+
18+
test_that("Warning on hex color", {
19+
item <- "Hello!"
20+
item_theme <- list()
21+
item_theme$font_color <- "#000000"
22+
expect_warning(applyLatexStyle(item, item_theme), "In Latex, colors must be color names not hex codes")
23+
})
24+
25+
test_that("Color application", {
26+
item <- "Hello!"
27+
item_theme <- list()
28+
item_theme$font_color <- "blue"
29+
r <- applyLatexStyle(item, item_theme)
30+
expect_equal(r, "\\color{blue}Hello!")
31+
})
32+
33+
context("validLatexFont")
34+
35+
test_that("Warning on missing font set to helvet", {
36+
expect_warning(validLatexFont("notafont"), "It has been set to")
37+
})

0 commit comments

Comments
 (0)