diff --git a/DESCRIPTION b/DESCRIPTION index c3f9643b..8b37f503 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,7 +6,7 @@ Description: In order to generate custom survey reports, this package provides 'banners' (cross-tabulations) of datasets in the Crunch () web service. Reports can be written in 'PDF' format using 'LaTeX' or in Microsoft Excel '.xlsx' files. -Version: 1.2.8 +Version: 1.2.9 Authors@R: c( person("Persephone", "Tsebelis", role="aut"), person("Kamil", "Sedrowicz", role="aut"), @@ -35,6 +35,6 @@ Suggests: rmarkdown, testthat (>= 2.1.0), kableExtra (>= 1.1.0.9000) -RoxygenNote: 7.1.0 +RoxygenNote: 7.1.1 VignetteBuilder: knitr Encoding: UTF-8 diff --git a/NEWS.md b/NEWS.md index 2cf3e61e..21134781 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +## crunchtabs 1.2.9 + +- Codebook question descriptions now appropriately escape special characters +- Added option enforce_onehundred which allows one to avoid rounding errors in totals rows +- Codebook table of contents overruns, cutting text and adding "..." +- Codebook generation now supports a filepath +- Added vertical space before append_text +- Bugfix: append_text that is multiple lines du0lpicated vertical space. Collapsing. + ## crunchtabs 1.2.8 - Documentation for generating codebooks (#180) diff --git a/R/codebookLatex.R b/R/codebookLatex.R index 68d4110e..188ce10c 100644 --- a/R/codebookLatex.R +++ b/R/codebookLatex.R @@ -72,22 +72,29 @@ codeBookItemTxtDescription <- function(x, ...) { txt$name <- crunch::name(x) + txt$name_toc <- ifelse( + nchar(txt$name) > 65, + paste0(substr(txt$name, 1, 65), "..."), + txt$name + ) + + if (txt$notes != "") { - 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" + 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" tex = sprintf( tex, texEscape(txt$description), texEscape(txt$alias_toc), - texEscape(txt$name), + texEscape(txt$name_toc), txt$notes ) } else { - tex = "\\vskip 0.10in\n%s\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{%s}} %s}\n\\vskip 0.10in" + tex = "\\vskip 0.10in\n%s\n\\addcontentsline{lot}{table}{\\parbox{1.8in}{\\ttfamily{%s }} %s}\n\\vskip 0.10in" tex = sprintf( tex, texEscape(txt$description), texEscape(txt$alias_toc), - texEscape(txt$name) + texEscape(txt$name_toc) ) } diff --git a/R/tex.R b/R/tex.R index d8a2dc0a..1b146a6e 100644 --- a/R/tex.R +++ b/R/tex.R @@ -34,16 +34,12 @@ texEscape <- function(string) { if (is.null(string)) { return("") } - # TODO: Change to one line rather than nested gsubs(), yuck. - gsub("^ *(\\[)", "\\\\hspace\\*\\{0in\\}\\1", # Trim leading whitespace - gsub("([#$%&_])", "\\\\\\1", # Escape special characters - gsub("[\u00A3\uFFE1]", "\\\\pounds", # Handle GBP currency - gsub("\n", " \\\\newline ", # Turn newlines into \newlines - string - ) - ) - ) - ) + s + string <- gsub("^ *(\\[)", "\\\\hspace\\*\\{0in\\}\\1", string) + string <- gsub("([#$%&_])", "\\\\\\1", string) + string <- gsub("[\u00A3\uFFE1]", "\\\\pounds", string) + string <- gsub("\n", " \\\\newline ", string) + string } #' Font Size diff --git a/R/theme.R b/R/theme.R index eab98fce..cf384bf0 100644 --- a/R/theme.R +++ b/R/theme.R @@ -261,6 +261,7 @@ themeDefaultLatex <- function(font = getOption("font", default = "helvet"), excel_freeze_column = 0, excel_orientation = "portrait", latex_round_percentages = FALSE, + enforce_onehundred = FALSE, latex_headtext = "", latex_foottext = "", latex_table_align = "r", diff --git a/R/writeCodeBookLatex.R b/R/writeCodeBookLatex.R index 577f84e2..407a3cf1 100644 --- a/R/writeCodeBookLatex.R +++ b/R/writeCodeBookLatex.R @@ -18,13 +18,15 @@ #' @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. #' @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. #' @param path The path to place .tex and .pdf files. +#' @param logging Leave logs in the working directory, defaults to FALSE #' @param ... Additional arguments passed to \link[kableExtra]{kable_styling} Unused. +#' #' @export writeCodeBookLatex <- function( ds, url = NULL, rmd = TRUE, pdf = TRUE, title = NULL, subtitle = NULL, table_of_contents = FALSE, sample_desc = NULL, field_period = NULL, preamble = NULL, suppress_zero_counts = FALSE, appendix = TRUE, logo = NULL, - position = NULL, path = NULL, + position = NULL, path = NULL, logging = FALSE, ...) { options("crunchtabs.codebook.suppress.zeros" = suppress_zero_counts) @@ -211,6 +213,15 @@ writeCodeBookLatex <- function( if (pdf) { tinytex::pdflatex(texname, pdf_file = pdfname) + + if (!logging) { + files <- list.files(path = getwd()) + files <- grep("out$|log$|aux$", files, value = TRUE) + if (length(files)) { + file.remove(file.path(getwd(), files)) + } + } + file.open(pdfname) } } diff --git a/R/writeLatex.R b/R/writeLatex.R index ff4373a7..e15a2ecc 100644 --- a/R/writeLatex.R +++ b/R/writeLatex.R @@ -20,7 +20,7 @@ #' @param moe An optional numeric margin of error. #' @param append_text An optional character string that, if supplied, will be appended after #' the final table. Useful for adding in disclosure information. Defaults to an empty string. -#' @param logging add log messages +#' @param logging Leave logs in the working directory, defaults to FALSE #' #' @return If \code{returndata} is set to \code{TRUE}, a processed data that was used to produce #' the report is returned. Otherwise \code{NULL} is returned. @@ -65,7 +65,7 @@ writeLatex <- function(data_summary, theme = themeDefaultLatex(), } if (!is.null(append_text)) { - append_text <- paste0("\\vspace{0.5in}\n\n", append_text) + append_text <- paste0("\\vspace{0.5in}\n\n", paste0(append_text, collapse = "\n")) } # Now assemble the .tex document diff --git a/man/writeCodeBookLatex.Rd b/man/writeCodeBookLatex.Rd index 46e15910..e954275b 100644 --- a/man/writeCodeBookLatex.Rd +++ b/man/writeCodeBookLatex.Rd @@ -19,6 +19,8 @@ writeCodeBookLatex( appendix = TRUE, logo = NULL, position = NULL, + path = NULL, + logging = FALSE, ... ) } @@ -54,6 +56,10 @@ LaTeX should be escaped.} \item{position}{Defaults to NULL. Identifies the position of the table on the page. Accepts "c", "l", or "r". Default position is left aligned tables.} +\item{path}{The path to place .tex and .pdf files.} + +\item{logging}{Leave logs in the working directory, defaults to FALSE} + \item{...}{Additional arguments passed to \link[kableExtra]{kable_styling} Unused.} } \description{ diff --git a/man/writeLatex.Rd b/man/writeLatex.Rd index 05eeb00f..ec5c2ff5 100644 --- a/man/writeLatex.Rd +++ b/man/writeLatex.Rd @@ -52,7 +52,7 @@ if \code{FALSE} the report shows counts.} \item{open}{logical. If PDF document was produced, open it with the default application? Only implemented for MacOS.} -\item{logging}{add log messages} +\item{logging}{Leave logs in the working directory, defaults to FALSE} } \value{ If \code{returndata} is set to \code{TRUE}, a processed data that was used to produce diff --git a/vignettes/FAQ.Rmd b/vignettes/FAQ.Rmd index c8415cb0..a4855eb8 100644 --- a/vignettes/FAQ.Rmd +++ b/vignettes/FAQ.Rmd @@ -1,12 +1,12 @@ --- title: "Frequently Asked Questions" author: "Brandon Bertelsen" -date: "`r Sys.Date()`" +date: "Last Update: 2020-09-08" output: rmarkdown::html_vignette: toc: true vignette: > - %\VignetteIndexEntry{Vignette Title} + %\VignetteIndexEntry{Frequently Asked Questions} %\VignetteEngine{knitr::knitr} %\VignetteEncoding{UTF-8} --- diff --git a/vignettes/Overview.Rmd b/vignettes/Overview.Rmd index a533d855..037f3e8f 100644 --- a/vignettes/Overview.Rmd +++ b/vignettes/Overview.Rmd @@ -6,7 +6,7 @@ output: rmarkdown::html_vignette: toc: true vignette: > - %\VignetteIndexEntry{Crunchtabs Quick Start} + %\VignetteIndexEntry{Introduction and Overview} %\VignetteEngine{knitr::knitr} %\VignetteEncoding{UTF-8} ---