Skip to content

Commit

Permalink
Closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
1beb committed Sep 4, 2020
1 parent 2f1d9df commit b422ac0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 17 additions & 4 deletions R/writeCodeBookLatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
#' @param appendix Should categorical questions with greater than 20 categories be put in an apppendix? Defaults to TRUE.
#' @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 ... 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,
position = NULL, path = NULL,
...) {

options("crunchtabs.codebook.suppress.zeros" = suppress_zero_counts)
Expand Down Expand Up @@ -194,10 +195,22 @@ writeCodeBookLatex <- function(
# codebook <- gsub("\\begin{longtabu}", paste0("\\begin{longtabu}", replacement), codebook, fixed = TRUE)
}

write(codebook, gsub(" ","-", paste0(name(ds), ".tex")))

# Issue 185 - Specify a path
if (!is.null(path)) {
basename <- gsub(" ","-", name(ds))
texname <- paste0(path,"/", basename, ".tex")
pdfname <- paste0(path, "/", basename, ".pdf")
} else {
basename <- gsub(" ","-", name(ds))
texname <- paste0(basename, ".tex")
pdfname <- paste0(basename, ".pdf")
}

write(codebook, texname)

if (pdf) {
tinytex::pdflatex(gsub(" ","-", paste0(name(ds), ".tex")))
file.open(paste0(gsub(" ","-", paste0(name(ds))), ".pdf"))
tinytex::pdflatex(texname, pdf_file = pdfname)
file.open(pdfname)
}
}
15 changes: 15 additions & 0 deletions tests/testthat/test-writeCodeBookLatex.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ with_api_fixture("fixtures-1-2-5", {
expect_true(length(tex) == length(original))
expect_true(sum(tex %in% original)/length(tex) > 0.98)
})

test_that("Default tex as expected", {
dir.create("tmp")
suppressWarnings(writeCodeBookLatex(
ds[1],
url = "https://app.crunch.io/dataset/10c3c3/",
appendix = TRUE, suppress_zero_counts = FALSE, pdf = FALSE, path = "tmp")
)
tex <- readLines("tmp/Data-for-Progress-National-Issues-Survey----Foreign-Policy.tex")
original <- readRDS("fixtures/writeCodeBookLatexLongCat.rds")
expect_true(length(tex) == length(original))
expect_true(sum(tex %in% original)/length(tex) > 0.98)
file.remove("tmp/Data-for-Progress-National-Issues-Survey----Foreign-Policy.tex")
file.remove("tmp")
})
})

# with_api_fixture("fixtures-1-2-5", {
Expand Down

0 comments on commit b422ac0

Please sign in to comment.