Skip to content

Commit

Permalink
Ready for release, may need a test or two
Browse files Browse the repository at this point in the history
  • Loading branch information
1beb committed Sep 9, 2020
1 parent 6677255 commit d992cf3
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 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.8
Version: 1.2.9
Authors@R: c(
person("Persephone", "Tsebelis", role="aut"),
person("Kamil", "Sedrowicz", role="aut"),
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
15 changes: 11 additions & 4 deletions R/codebookLatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

Expand Down
16 changes: 6 additions & 10 deletions R/tex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 12 additions & 1 deletion R/writeCodeBookLatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions R/writeLatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions man/writeCodeBookLatex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/writeLatex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vignettes/FAQ.Rmd
Original file line number Diff line number Diff line change
@@ -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}
---
Expand Down
2 changes: 1 addition & 1 deletion vignettes/Overview.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
---
Expand Down

0 comments on commit d992cf3

Please sign in to comment.