Author: Brandon Bertelsen
Last updated: 2021-09-10

## FAQ

### How do I combine multiple crunchtabs reports

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 document
• pdf_combine which puts two documents together, each one appended to the last
• pdf_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.

library(openxlsx)
lapply(
1:length(names(wb)), function(row) {
writeFormula(
wb = wb,
sheet = 1,
startCol = 1,
startRow = 4 + row,
x = paste0(
names(wb)[row], '!A5", "', names(wb)[row], '")'
}
)

### How do I change the crosstab stub width?

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.

myTheme = themeNew(
... # your other theme optinos
format_label_column = list(col_width = 2)
)

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.

### How do I hide the question numbers?

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(
..., # your other theme options
format_var_description = list(include_q_number = FALSE),
format_var_filtertext = list(include_q_number = FALSE)
)

### How do I make it so that there is more than one question per page?

myAwesomeTheme = themeNew(
...,
one_per_sheet = FALSE
)

### How do I output numbers like 1,000 instead of 1000?

format(1000, nsmall=0, big.mark=",")

### How do I add a subtitle to my document?

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

### How do I mask questions with low base sizes?

Below is an example of masking responses where the base size is less than the central limit theorem with a “-” character.

myAwesomeTheme = themeNew(
...,
format_min_base = list(mask = "-", min_base = 30)
)

### How do I display questions with different weights in the same report?

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)

### How do I install a different release of crunchtabs?

In the code example below we show you how to install Release 1.2.1:

remotes::install_github("Crunch-io/crunchtabs@v1.2.1")

### How do I replace question numbers with question aliases?

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, ...)

1. This is my question text

You should see:

question_alias. This is my question text

### How do I install a different release of crunchtabs?

In the code example below we show you how to install Release 1.2.1:

remotes::install_github("Crunch-io/crunchtabs@v1.2.1")

### How do I use a logo in Excel?

You can control the height, width, dpi and start column (specified by a number).

library(crunchtabs)

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) 

### How do I sort a categorical or multiple response question’s presenation?

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, ...)

### How do I stop crosstabs from breaking in the middle of a tab?

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)

### How do I request a new feature?

We welcome features requests as new issues to the crunchtabs github repository