Author: Brandon Bertelsen
Last updated: 2023-01-18
The pdftools
library provides us with the ability to splice, combine and append pdf files together. We can use this to combine one or more arbitrary documents together. One major caveat is the lack of a combined table of contents.
There are three functions of interest:
pdf_subset
which extracts specific pages from a pdf documentpdf_combine
which puts two documents together, each one appended to the lastpdf_split
which breaks up a document into it’s component pages. The default is “path_XXX” where XXX is the page number.
library(pdftools)
# Combining two reports
pdf_combine(c("path/to/doc1.pdf", "path/to/doc2.pdf"), output="combined.pdf")
# Resulting PDF would be doc1 followed by doc2 in the same document.
As an alternative, one could weave together the latex that builds PDF reporting. However, this is not recommended, challenging and outside of the scope of this FAQ.
Unfortunately there is not yet a built in way to relabel the table of contents. However, there is a straight forward solution using the openxls
package.
The stub width (the first column of a crosstabulation) is fixed at 1.5inches by default. In v1.2.3 we added a feature allowing an override.
The code example above would set the stub width to 2 inches. For a PDF document, col_width is in inches. You can also create an exceptional stub width for one or more questions, as follows:
myTheme = themeNew(
... # your other theme optinos
format_label_column_exceptions = c("alias"=2.5, "otheralias"=1.25)
)
In this case, you create a named numeric vector where the name is the question alias whose stub width you wish to change and the numeric value is the stub width in inches.
A number of theme options exist for this purpose, format_var_alias
, format_var_description
, format_var_filtertext
, and format_var_name
. In your format list, set an item include_q_number = FALSE
as in the example below.
myAwesomeTheme = themeNew(
...,
one_per_sheet = FALSE
)
format(1000, nsmall=0, big.mark=",")
A common usage of the subtitle is to include the survey’s field window. Especially for studies that are run continuously.
writeLatex(
...,
subtitle = "My Amazing Subtitle"
)
See the section about appending text in the overview
Below is an example of masking responses where the base size is less than the central limit theorem with a “-” character.
We can chain crosstabs objects easily to create the desired effect. Below, we create two crosstabs objects with the variables and the weighting we want applied and then we concatenate them together.
# ...
topline_summary = crosstabs(ds, vars = c("a", "b"), weight = "weightvar")
chain_me = crosstabs(ds, vars = c("c"), weight = "other_weightvar")
topline_summary$results = c(
topline_summary$results,
chain_me$results
)
topline_summary$results = crunchtabs:::reflowQuestionNumbers(
topline_summary$results
)
writeLatex(topline_summary)
Re-weighting the same variable with another weight in the same report is possible but requires you to also adjust the names so that you can edit the question text appropriately to demonstrate the different weighting:
# ...
topline_summary = crosstabs(ds, vars = c("a", "b"), weight = "weightvar")
chain_me = crosstabs(ds, vars = c("a"), weight = "other_weightvar")
names(chain_me$results) = "a_other_weight_var" # rename the alias
chain_me$results$a_other_weight_var$description = "This is question text (weighted by other_weight_var)"
topline_summary$results = c(
topline_summary$results,
chain_me$results
)
topline_summary$results = crunchtabs:::reflowQuestionNumbers(
topline_summary$results
)
writeLatex(topline_summary)
In the code example below we show you how to install Release 1.2.1:
remotes::install_github("Crunch-io/crunchtabs@v1.2.1")
We can manipulate the crosstab object so that instead of displaying a number, we display the question alias.
ds = loadDataset("Example dataset")
# Use ds = newExampleDataset() if it doesn't work
ct = crosstabs(ds)
nms = names(ct$results)
for(i in 1:results) {
ct$results[[i]]$number = i
}
writeLatex(ct, ...)
Instead of:
- This is my question text
You should see:
question_alias. This is my question text
In the code example below we show you how to install Release 1.2.1:
remotes::install_github("Crunch-io/crunchtabs@v1.2.1")
See releases for more information about the changes between releases.
You can control the height, width, dpi and start column (specified by a number).
library(crunchtabs)
login()
ds = loadDataset("Example dataset")
# Use ds = newExampleDataset() if not found!
ct_banner <- banner(ds, vars = list(`banner 1` = c('allpets')))
ct_summary <- crosstabs(dataset = ds, banner = ct_banner)
myTheme <- themeNew(
logo = list(
file = "yougov-logo.png",
dpi = 100,
height = 0.75,
width = 3,
startCol = 6)
)
writeExcel(ct_summary, filename = "output", theme = myTheme)
You can sort response categories alphabetically or by proportion. You can apply a fixed presentation order and you can pin response categories to the top or bottom.
ds = loadDataset("Example dataset")
# Use ds = newExampleDataset() if it doesn't work
ct = crosstabs(ds)
ct = sortAliases(ct)
writeLatex(ct, ...)
The default behaviour sorts all questions that can be sorted, descending by order of proportion. But we can apply a variety of different sorting options by using the sortAliases
function repeatedly.
# load ds, define theme ...
ct = sortAliases(ct, var = c("a", "b"), alpha = TRUE) # sort a/b by alpha
ct = sortAliases(ct, var = "c") # sort c by numeric
ct = sortAliases(ct, var = "d", pin_to_bottom = "Don't know")
writeLatex(ct, ...)
In 1.2.8 a new feature was added that allows you to avoid page breaks within a banner. Some users were adding manual latex page breaks to avoid this behavior. Now, you can set the pagebreak_in_banner
parameter to FALSE and crunchtabs will automatically push the banner to the next page to avoid a page break.
crosstabs_theme <- themeNew(..., pagebreak_in_banner = FALSE)
We welcome features requests as new issues to the crunchtabs github repository