Skip to content

Commit c4b1792

Browse files
authored
Merge pull request #256 from Crunch-io/vignette_updates
Vignette updates
2 parents 37d0c8e + 18b4b05 commit c4b1792

10 files changed

+374
-51
lines changed

R/codeBookSummary.R

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,23 @@ codeBookSummary.default <- function(x, meta, ...) {
3636
#' @param meta A list containning question meta data
3737
#' @export
3838
codeBookSummary.factor <- function(x, meta, ...) {
39-
# Should return
40-
# id, name, n
41-
42-
labs = na.omit(
43-
eval(
44-
parse(
45-
text = as.character(
46-
meta$recode
39+
40+
if(!is.na(meta$recode)) {
41+
labs = na.omit(
42+
eval(
43+
parse(
44+
text = as.character(
45+
meta$recode
46+
)
4747
)
4848
)
4949
)
50-
)
5150

52-
labs <- labs[!is.na(labs)]
53-
labs[labs == " " & !is.na(labs)] <- NA
54-
# x <- as.character(x)
55-
# x[x == " " & !is.na(x)] <- NA
56-
#
57-
# fac <- factor(x, levels = labs, labels = labs)
51+
labs <- labs[!is.na(labs)]
52+
labs[labs == " " & !is.na(labs)] <- NA
53+
} else {
54+
labs <- levels(x)
55+
}
5856

5957
smry <- table(x, useNA = "ifany")
6058

vignettes/Codebooks.Rmd

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ A codebook is used as a reference to document the contents of a survey by displa
1818

1919
### Generating Codebooks
2020

21-
Creating a codebook from a crunch dataset is easy! In the examples below we will work with the example dataset.
21+
Creating a codebook from a crunch dataset is easy. In the examples below we will work with the "Example dataset" built into crunch that you can .
2222

2323
```{r, eval = FALSE}
2424
library(crunchtabs)
@@ -44,7 +44,7 @@ writeCodeBookLatex(ds, url = "https://api.crunch.io/datasets/<dataset-id>")
4444
4545
## Options
4646

47-
There are a number of optional settings that you can control such as: displaying a table of contents, a sample descriptions, changing the title or subtitle, or adding a logo. In this section, we will present each of these.
47+
There are a number of optional settings that you can control such as: displaying a table of contents, a sample description, changing the title or subtitle, or adding a logo. In this section, we will present each of these.
4848

4949
### Table of Contents
5050

@@ -54,10 +54,6 @@ A table of contents can be added by using the `table_of_contents` parameter. Pro
5454
writeCodeBookLatex(ds, table_of_contents = TRUE)
5555
```
5656

57-
```{r, echo = FALSE}
58-
knitr::include_graphics("codebook-toc-example-002.png")
59-
```
60-
6157
![Table of contents](https://raw.githubusercontent.com/Crunch-io/crunchtabs/main/vignettes/codebook-toc-example-002.png)
6258

6359
### Appendices
@@ -74,12 +70,12 @@ Currently you can change the title and subtitle as well as provide descriptives
7470

7571
```{r, eval = FALSE}
7672
writeCodeBookLatex(
77-
ds,
78-
title = "An Amazing Title",
79-
subtitle = "A Breathtaking Subtitle",
80-
field_period = "To infinity... and beyond!",
73+
ds[1],
74+
title = "Your Title Here",
75+
subtitle = "Your Subtitle Here",
76+
field_period = "Feb 2021 - Mar 2021",
8177
sample_desc = "US Voting Adults",
82-
preamble = "e^{ \\pm i\\theta } = \\cos \\theta \\pm i\\sin \\theta"
78+
preamble = "You can enter arbitrary \\textbf{\\emph{tex}} in the preamble, but it must be double escaped. "
8379
)
8480
```
8581

@@ -96,9 +92,9 @@ writeCodeBookLatex(ds, logo = "yougov")
9692

9793
![Table of contents](https://raw.githubusercontent.com/Crunch-io/crunchtabs/main/vignettes/codebook-logos-example-004.png)
9894

99-
### Suppress Zeroes
95+
### Suppressing Zeroes
10096

101-
Although not recommended, there are some questions that have a significant amount of zero-filled categories (country of residence for example). This can lead to the end-user of the codebook scrolling through information that is useless to them.
97+
Although not recommended, there are some questions that have a significant amount of zero-filled categories (country of residence for example). This can lead to the end-user of the codebook scrolling through pages and pages of zero filled categories. You can supress zeroes using the following:
10298

10399
```{r, eval = FALSE}
104100
writeCodeBookLatex(ds, suppress_zero_counts = TRUE)
@@ -112,4 +108,68 @@ The table alignment on the page can be adjusted to one of: "l" for left (the de
112108
writeCodeBookLatex(ds, position = "c")
113109
```
114110

115-
![Table of contents](https://raw.githubusercontent.com/Crunch-io/crunchtabs/main/vignettes/codebook-centered-example-005.png)
111+
![Centered codebook](https://raw.githubusercontent.com/Crunch-io/crunchtabs/main/vignettes/codebook-centered-example-005.png)
112+
113+
## Generating Codebooks from Generic Data
114+
115+
You can also create a codebook from a generic dataset but you are required to create the meta data yourself. Below, we will discuss the format of the meta data and what you need to do to put it together.
116+
117+
Your meta data should include the following:
118+
119+
- alias: The short code for the name of the column
120+
- descrption: A long description of the contents of the variables. If this is survey data, your question text would go here.
121+
- name: This is a short name for the variable that is designed to provide flavor for the alias but no detail as in the description.
122+
- notes: This is descriptive text that identifies a subset.
123+
- recode: Required column but optional fill. Used in other processes where raw data requires additional manipulation or recoding.
124+
125+
> Important: We parse the meta data file to create the codebook, we don't parse the data other than for summarization, columns/variables not specified in the meta data are ignored.
126+
127+
```{r, eval = FALSE}
128+
library(crunchtabs)
129+
ds <- datasets::iris
130+
meta <- data.frame(
131+
alias = c(
132+
"Sepal.Length",
133+
"Sepal.Width",
134+
"Petal.Length",
135+
"Petal.Width",
136+
"Species"
137+
),
138+
name = c(
139+
"Sepal Length",
140+
"Sepal Width",
141+
"Petal Length",
142+
"Petal Width",
143+
"Iris Species"
144+
),
145+
description = c(
146+
"The length of the flower's sepal",
147+
"The width of the flower's sepal",
148+
"The length of the flower's petals",
149+
"The width of the flower's petals",
150+
"The iris species"
151+
),
152+
notes = rep("", 5),
153+
recode = rep(NA, 5),
154+
stringsAsFactors = FALSE
155+
)
156+
157+
writeCodeBookLatexGeneric(
158+
ds[c(1,5)],
159+
meta = meta[c(1,5),],
160+
title = "Edgar Anderson's Iris Data",
161+
subtitle = "The data were collected by Anderson, Edgar",
162+
field_period = "1935",
163+
sample_desc = "The irises of the Gaspe Peninsula",
164+
preamble = "This famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.",
165+
table_of_contents = TRUE,
166+
)
167+
```
168+
169+
![Codebook generic example 001](https://raw.githubusercontent.com/Crunch-io/crunchtabs/main/vignettes/codebook-generic-example-001.png)
170+
171+
![Codebook generic example 001](https://raw.githubusercontent.com/Crunch-io/crunchtabs/main/vignettes/codebook-generic-example-002.png)
172+
173+
## Known Limitations
174+
175+
- This software only supports latin characters. English, French, Spanish, and Portuguese will work out of the box. Other languages with significant special character or RTL are not supported.
52 KB
Binary file not shown.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
\documentclass{article}
2+
\usepackage[pdftex]{graphicx}
3+
\usepackage[utf8]{inputenc}
4+
\usepackage{fancyhdr}
5+
\usepackage{sfmath}
6+
\usepackage{comment}
7+
\usepackage[T1]{fontenc}
8+
\usepackage[pdftex=true, pdftoolbar=true, pdfmenubar=true, pdfauthor = {}, pdfcreator = {PDFLaTeX}, pdftitle = {}, colorlinks=true, urlcolor=blue, linkcolor=blue, citecolor=blue, implicit=true, hypertexnames=false]{hyperref}
9+
\usepackage[scaled]{helvet}
10+
\renewcommand*\familydefault{\sfdefault}
11+
\usepackage{booktabs}
12+
\usepackage{tabu}
13+
\usepackage{longtable}
14+
\usepackage{siunitx}
15+
\sisetup{
16+
round-mode = places, % Rounds numbers
17+
round-precision = 2, % to 2 places
18+
table-format = 3
19+
}
20+
\usepackage[top=0.6in, bottom=0.6in, left=1in, right=1in, includeheadfoot]{geometry}
21+
\usepackage{array}
22+
\usepackage[english]{babel}
23+
\newcolumntype{B}[2]{>{#1\hspace{0pt}\arraybackslash}b{#2}}
24+
\setlength{\parindent}{0pt}
25+
\usepackage[dvipsnames]{color}
26+
\definecolor{gray}{gray}{0.85}
27+
\pagestyle{fancy}
28+
\renewcommand{\headrulewidth}{0pt}
29+
\renewcommand{\footrulewidth}{0pt}
30+
31+
\newcolumntype{d}{S}
32+
\newcolumntype{J}{S[table-format=1]}
33+
\newcolumntype{K}{S[table-format=2]}
34+
\newcolumntype{M}{S[table-format=4]}
35+
\newcolumntype{N}{S[table-format=5]}
36+
\newcolumntype{O}{S[table-format=6]}
37+
% default is 3 so it's not included above
38+
\usepackage{float}
39+
\usepackage{marginnote}
40+
\setlength\extrarowheight{2pt}
41+
\newlength\mywidth
42+
\setlength\mywidth{3.5in}
43+
\usepackage{caption}
44+
\captionsetup[table]{labelformat=empty}
45+
\renewcommand*{\marginfont}{\scriptsize\itshape}
46+
\fancyfoot{}
47+
\fancyfoot[R]{\thepage}
48+
\newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
49+
\let\PBS=\PreserveBackslash
50+
\newcommand{\longtablesep}{\endfirsthead \multicolumn{2}{c}{\textit{}} \\ \endhead \multicolumn{2}{c}{\textit{}} \\ \endfoot \endlastfoot}
51+
\usepackage[titles]{tocloft}
52+
\newcommand{\cftchapfont}{12}
53+
\newcommand{\formatvardescription}[1]{#1}
54+
\newcommand{\formatvarname}[1]{#1}
55+
\newcommand{\formatvaralias}[1]{#1}
56+
\newcommand{\formatvarfiltertext}[1]{\fontsize{8}{12}\textit{#1}}
57+
\newcommand{\formatvarsubname}[1]{#1}
58+
\usepackage{amsmath}
59+
\usepackage{listings}
60+
\usepackage{inconsolata}
61+
62+
\newenvironment{absolutelynopagebreak}
63+
{\par\nobreak\vfil\penalty0\vfilneg
64+
\vtop\bgroup}
65+
{\par\xdef\tpd{\the\prevdepth}\egroup
66+
\prevdepth=\tpd}
67+
68+
\fancyhead{}
69+
\fancyhead[L]{{\fontsize{16}{24}\textbf{Edgar Anderson's Iris Data}}\fontsize{12}{18}\textbf{ \\ The data were collected by Anderson, Edgar}}
70+
71+
72+
\begin{document}
73+
\setlength{\tabcolsep}{1em}
74+
\setlength{\LTleft}{0pt}
75+
\setlength{\LTright}{\fill}
76+
\setlength{\LTcapwidth}{\textwidth}
77+
\vspace{.25in}
78+
79+
\begin{longtable}[l]{ll}
80+
Sample & The irises of the Gaspe Peninsula \\
81+
Conducted & 1935 \\
82+
\end{longtable}
83+
This famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.
84+
85+
86+
\renewcommand{\listtablename}{Table of Contents}
87+
\listoftables
88+
\clearpage
89+
90+
\begin{absolutelynopagebreak}
91+
\begin{absolutelynopagebreak}
92+
\textbf{Sepal Length}\hfill\textbf{\ttfamily{Sepal.Length}}
93+
94+
{\small numeric}
95+
96+
\vskip 0.10in
97+
The length of the flower's sepal
98+
\addcontentsline{lot}{table}{\parbox{1.8in}{\ttfamily{Sepal.Length}} Sepal Length}
99+
\vskip 0.10in\end{absolutelynopagebreak}
100+
\begin{longtable}[l]{cccccc}
101+
\toprule
102+
{Mean} & {SD} & {Min} & {Max} & {n} & {Missing}\\
103+
\midrule
104+
5.84 & 1 & 4.3 & 7.9 & 150 & 0\\
105+
\bottomrule
106+
\end{longtable}\end{absolutelynopagebreak}
107+
\begin{absolutelynopagebreak}
108+
\begin{absolutelynopagebreak}
109+
110+
\vskip 0.25in
111+
\textbf{Iris Species}\hfill\textbf{\ttfamily{Species}}
112+
113+
{\small factor}
114+
115+
\vskip 0.10in
116+
The iris species
117+
\addcontentsline{lot}{table}{\parbox{1.8in}{\ttfamily{Species}} Iris Species}
118+
\vskip 0.10in\end{absolutelynopagebreak}
119+
\begin{longtable}[l]{JlK}
120+
\toprule
121+
{Code} & {Label} & {Count}\\
122+
\midrule
123+
1 & setosa & 50\\
124+
2 & versicolor & 50\\
125+
3 & virginica & 50\\
126+
\bottomrule
127+
\end{longtable}\end{absolutelynopagebreak}
128+
129+
\clearpage
130+
131+
132+
133+
134+
\end{document}

vignettes/Example-dataset.pdf

57.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)