Skip to content

Commit 47ccd92

Browse files
committed
- improve docs
- add examples for codebook functions
1 parent 98c1bfd commit 47ccd92

34 files changed

Lines changed: 2361 additions & 116 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ plans/
66
tests/test_codebook_*
77
tests/test_codebook.html
88
inst/doc
9+
figure/

DESCRIPTION

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
Package: codebook
2-
Title: Cook Codebooks from Survey Metadata Encoded in Attributes in R
2+
Title: Automatic Codebooks from Survey Metadata Encoded in Attributes
33
Description: Easily automate the following tasks to describe data frames:
44
computing reliabilities (internal consistencies, retest, multilevel) for psychological scales,
55
summarise the distributions of scales and items graphically and using descriptive statistics,
6-
combine this information with metadata such as item labels and labelled values that is derived from R attributes.
7-
To do so, the package relies on rmarkdown partials. To mark up the metadata you need to use haven semantics.
8-
Version: 0.5.6
6+
combine this information with metadata (such as item labels and labelled values) that is derived from R attributes.
7+
To do so, the package relies on 'rmarkdown' partials, so you can generate HTML, PDF, and Word documents. Codebooks
8+
are also available as tables (CSV, Excel, etc.).
9+
Version: 0.5.7
910
Authors@R: person("Ruben", "Arslan", email = "ruben.arslan@gmail.com", role = c("aut", "cre"))
1011
Depends: R (>= 3.0.1)
1112
URL: https://github.com/rubenarslan/codebook

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# codebook 0.5.7
2+
- changed description and documentation
3+
14
# codebook 0.5.6
25
- changed license to MIT
36

R/codebook.R

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22

33
#' Generate rmarkdown codebook
44
#'
5-
#' If you pass the object resulting from a call to formr_results to this function, it will generate a markdown codebook for this object.
5+
#' Pass a data frame to this function to make a codebook for that dataset.
6+
#' If the dataset has metadata (attributes) set on its variables, these will be
7+
#' used to make the codebook more informative. Examples are item, value, and
8+
#' missing labels.
9+
#' Data frames imported via [haven::read_dta], [haven::read_sav], or from
10+
#' [formr.org](https://formr.org) will have these attributes in the right format.
11+
#' By calling this function inside a knitr code chunk, the
12+
#' codebook will become part of the document you are generating.
613
#'
7-
#' @param results a formr results table with attributes set on items and scales
14+
#' @param results a data frame, ideally with attributes set on variables
815
#' @param reliabilities a named list with one entry per scale and one or several printable reliability computations for this scale. if NULL, computed on-the-fly using compute_reliabilities
916
#' @param survey_repetition defaults to "auto" which is to try to determine the level of repetition from the "session" and "created" variables. Other values are: single, repeated_once, repeated_many
1017
#' @param missingness_report whether to print a missingness report. Turn off if this gets too complicated and you need a custom solution (e.g. in case of random missings).
@@ -14,7 +21,10 @@
1421
#'
1522
#' @export
1623
#' @examples
17-
#' # see vignette
24+
#' # will generate figures in a figure/ subdirectory
25+
#' data("bfi")
26+
#' bfi <- bfi[, c("BFIK_open_1", "BFIK_open_1")]
27+
#' md <- codebook(bfi, survey_repetition = "single", metadata_table = FALSE)
1828
codebook <- function(results, reliabilities = NULL,
1929
survey_repetition = c('auto', 'single', 'repeated_once', 'repeated_many'),
2030
missingness_report = TRUE, metadata_table = TRUE,
@@ -49,10 +59,6 @@ codebook <- function(results, reliabilities = NULL,
4959
"repeated_many"))
5060
}
5161
}
52-
suppressMessages(
53-
skimr::skim_with(labelled = skimr::get_skimmers()$factor)
54-
)
55-
on.exit(skimr::skim_with_defaults())
5662

5763
if (is.null(reliabilities)) {
5864
reliabilities <- compute_reliabilities(results, survey_repetition)
@@ -137,11 +143,15 @@ codebook <- function(results, reliabilities = NULL,
137143
#' Codebook survey overview
138144
#'
139145
#'
140-
#' @param results a formr results table which has the following columns: session, created, modified, expired, ended
146+
#' @param results a data frame which has the following columns: session, created, modified, expired, ended
141147
#' @param survey_repetition defaults to single (other values: repeated_once, repeated_many). controls whether internal consistency, retest reliability or multilevel reliability is computed
142148
#' @param indent add # to this to make the headings in the components lower-level. defaults to beginning at h2
143149
#'
144150
#' @export
151+
#' @examples
152+
#' # will generate figures in a figure/ subdirectory
153+
#' data("bfi")
154+
#' codebook_survey_overview(bfi)
145155
codebook_survey_overview <- function(results, survey_repetition = "single",
146156
indent = "##") {
147157
stopifnot(exists("session", results))
@@ -185,10 +195,13 @@ codebook_survey_overview <- function(results, survey_repetition = "single",
185195
#' Codebook missingness
186196
#'
187197
#'
188-
#' @param results a formr results table which has the following columns: session, created, modified, expired, ended
198+
#' @param results a data frame
189199
#' @param indent add # to this to make the headings in the components lower-level. defaults to beginning at h2
190200
#'
191201
#' @export
202+
#' @examples
203+
#' data("bfi")
204+
#' codebook_missingness(bfi)
192205
codebook_missingness <- function(results, indent = "##") {
193206
options <- list(
194207
fig.path = paste0(knitr::opts_chunk$get("fig.path"), "overview_"),
@@ -202,10 +215,13 @@ codebook_missingness <- function(results, indent = "##") {
202215

203216
#' Metadata as JSON-LD
204217
#'
218+
#' Echo a list of a metadata as JSON-LD in a script tag.
205219
#'
206-
#' @param results a formr results table which has the following columns: session, created, modified, expired, ended
207-
#'
220+
#' @param results a data frame, ideally with attributes set on variables
208221
#' @export
222+
#' @examples
223+
#' data("bfi")
224+
#' metadata_jsonld(bfi)
209225
metadata_jsonld <- function(results) {
210226
options <- list(
211227
fig.path = paste0(knitr::opts_chunk$get("fig.path"), "metadata_"),
@@ -221,10 +237,16 @@ metadata_jsonld <- function(results) {
221237
#' Renders a tabular codebook including attributes and data summaries.
222238
#'
223239
#'
224-
#' @param results a formr results table which has the following columns: session, created, modified, expired, ended
240+
#' @param results a data frame, ideally with attributes set on variables
225241
#' @param indent add # to this to make the headings in the components lower-level. defaults to beginning at h2
226242
#'
227243
#' @export
244+
#' @examples
245+
#' data("bfi")
246+
#' \dontrun{
247+
#' # doesn't show interactively, because a html widget needs to be registered
248+
#' codebook_items(bfi)
249+
#' }
228250
codebook_items <- function(results, indent = "##") {
229251
options <- list(
230252
fig.path = paste0(knitr::opts_chunk$get("fig.path"), "overview_"),
@@ -245,6 +267,12 @@ codebook_items <- function(results, indent = "##") {
245267
#' @param indent add # to this to make the headings in the components lower-level. defaults to beginning at h2
246268
#'
247269
#' @export
270+
#' @examples
271+
#' # will generate figure in a figure/ subdirectory
272+
#' data("bfi")
273+
#' bfi <- bfi[,c("BFIK_open", paste0("BFIK_open_", 1:4))]
274+
#' codebook_component_scale(bfi[,1], "BFIK_open", bfi[,-1],
275+
#' reliabilities = list(BFIK_open = psych::alpha(bfi[,-1])))
248276
codebook_component_scale <- function(scale, scale_name, items, reliabilities,
249277
indent = '##') {
250278
stopifnot( exists("scale_item_names", attributes(scale)))
@@ -254,6 +282,10 @@ codebook_component_scale <- function(scale, scale_name, items, reliabilities,
254282
fig.path = paste0(knitr::opts_chunk$get("fig.path"), scale_name, "_"),
255283
cache.path = paste0(knitr::opts_chunk$get("cache.path"), scale_name, "_")
256284
)
285+
old_opt <- options('knitr.duplicate.label')$knitr.duplicate.label
286+
options(knitr.duplicate.label = 'allow')
287+
on.exit(options(knitr.duplicate.label = old_opt))
288+
257289
asis_knit_child(require_file("_codebook_scale.Rmd"), options = options)
258290
}
259291

@@ -265,6 +297,10 @@ codebook_component_scale <- function(scale, scale_name, items, reliabilities,
265297
#' @param indent add # to this to make the headings in the components lower-level. defaults to beginning at h2
266298
#'
267299
#' @export
300+
#' @examples
301+
#' # will generate figure in a figure/ subdirectory
302+
#' #' data("bfi")
303+
#' codebook_component_single_item(bfi$BFIK_open_1, "BFIK_open_1")
268304
codebook_component_single_item <- function(item, item_name, indent = '##') {
269305
options <- list(
270306
fig.path = paste0(knitr::opts_chunk$get("fig.path"), item_name, "_"),
@@ -278,11 +314,14 @@ codebook_component_single_item <- function(item, item_name, indent = '##') {
278314
#' will generate a table combining metadata from variable attributes
279315
#' with data summaries generated using skimr
280316
#'
281-
#' @param results a data frame
317+
#' @param results a data frame, ideally with attributes set on variables
282318
#'
283319
#' @export
320+
#' @examples
321+
#' data("bfi")
322+
#' codebook_table(bfi)
284323
codebook_table <- function(results) {
285-
skimmed <- skimr::skim_to_wide(results)
324+
skimmed <- skim_to_wide_labelled(results)
286325

287326
metadata <- dplyr::bind_rows(
288327
# var = results$session
@@ -356,9 +395,13 @@ codebook_table <- function(results) {
356395
#'
357396
#' Returns a list containing variable metadata (attributes) and data summaries.
358397
#'
359-
#' @param results a data frame
398+
#' @param results a data frame, ideally with attributes set on variables
360399
#'
361400
#' @export
401+
#' @examples
402+
#' data("bfi")
403+
#' md_list <- metadata_list(bfi)
404+
#' md_list$variableMeasured[[20]]
362405
metadata_list <- function(results) {
363406
name <- deparse(substitute(results))
364407
metadata <- attributes(results)
@@ -411,7 +454,7 @@ metadata_list <- function(results) {
411454
}
412455

413456
}
414-
x$data_summary <- skimr::skim_to_wide(results[[var]])
457+
x$data_summary <- skim_to_wide_labelled(results[[var]])
415458
x$data_summary$variable <- NULL
416459
if (exists("type", x$data_summary)) {
417460
if (!exists("value", x)) {
@@ -436,3 +479,12 @@ metadata_list <- function(results) {
436479

437480
metadata
438481
}
482+
483+
484+
skim_to_wide_labelled <- function(...) {
485+
suppressMessages(
486+
skimr::skim_with(labelled = skimr::get_skimmers()$factor)
487+
)
488+
on.exit(skimr::skim_with_defaults())
489+
skimr::skim_to_wide(...)
490+
}

README.Rmd

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@ knitr::opts_chunk$set(
1515
# Codebook Cookbook
1616
[![Travis-CI Build Status](https://travis-ci.org/rubenarslan/codebook.svg?branch=master)](https://travis-ci.org/rubenarslan/codebook) [![codecov](https://codecov.io/gh/rubenarslan/codebook/branch/master/graph/badge.svg)](https://codecov.io/gh/rubenarslan/codebook)
1717

18+
## Description
19+
20+
Easily automate the following tasks to describe data frames:
21+
computing reliabilities (internal consistencies, retest, multilevel) for psychological scales,
22+
summarise the distributions of scales and items graphically and using descriptive statistics,
23+
combine this information with metadata (such as item labels and labelled values) that is derived from R attributes.
24+
To do so, the package relies on 'rmarkdown' partials, so you can generate HTML, PDF, and Word documents. Codebooks
25+
are also available as tables (CSV, Excel, etc.).
26+
1827
## Generate markdown codebooks from the attributes of the variables in your data frame
1928

2029
RStudio and a few of the tidyverse package already usefully display the information contained in the attributes of the variables in your data frame. The [haven](https://github.com/hadley/haven) package also manages to grab variable documentation from SPSS or Stata files.
2130

2231
The codebook package takes those attributes and the data and tries to produce a good-looking codebook, i.e. a place to get an overview of the variables in a dataset. The codebook processes single items, but also "scales", i.e. psychological questionnaires that are aggregated to extract a construct. For scales, the appropriate reliability coefficients (internal consistencies for single measurements, retest reliabilities for repeated measurements, multilevel reliability for multilevel data) are computed.
2332
For items and scales, the distributions are summarised graphically and numerically.
2433

25-
This package integrates tightly with formr ([formr.org](https://formr.org)), an online survey framework and especially the data frames produced and marked up by the [formr R package](https://github.com/rubenarslan/formr), but is completely independent of it.
26-
27-
To do this, this package uses rmarkdown _partials_, that can be integrated in a larger rmarkdown document and then translated to HTML or PDF format.
34+
This package integrates tightly with formr ([formr.org](https://formr.org)), an online survey framework and especially the data frames produced and marked up by the [formr R package](https://github.com/rubenarslan/formr). However, codebook is completely independent of it.
2835

2936
## Documentation
30-
Confer the help or: https://rubenarslan.github.io/codebook
37+
Confer the help or: https://rubenarslan.github.io/codebook.
38+
See the [vignette](https://rubenarslan.github.io/codebook/articles/codebook.html) for a quick example of an HTML document generated using `codebook`.
3139

3240
## Use as a webapp
3341

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ Codebook Cookbook
55

66
[![Travis-CI Build Status](https://travis-ci.org/rubenarslan/codebook.svg?branch=master)](https://travis-ci.org/rubenarslan/codebook) [![codecov](https://codecov.io/gh/rubenarslan/codebook/branch/master/graph/badge.svg)](https://codecov.io/gh/rubenarslan/codebook)
77

8+
Description
9+
-----------
10+
11+
Easily automate the following tasks to describe data frames: computing reliabilities (internal consistencies, retest, multilevel) for psychological scales, summarise the distributions of scales and items graphically and using descriptive statistics, combine this information with metadata (such as item labels and labelled values) that is derived from R attributes. To do so, the package relies on 'rmarkdown' partials, so you can generate HTML, PDF, and Word documents. Codebooks are also available as tables (CSV, Excel, etc.).
12+
813
Generate markdown codebooks from the attributes of the variables in your data frame
914
-----------------------------------------------------------------------------------
1015

1116
RStudio and a few of the tidyverse package already usefully display the information contained in the attributes of the variables in your data frame. The [haven](https://github.com/hadley/haven) package also manages to grab variable documentation from SPSS or Stata files.
1217

1318
The codebook package takes those attributes and the data and tries to produce a good-looking codebook, i.e. a place to get an overview of the variables in a dataset. The codebook processes single items, but also "scales", i.e. psychological questionnaires that are aggregated to extract a construct. For scales, the appropriate reliability coefficients (internal consistencies for single measurements, retest reliabilities for repeated measurements, multilevel reliability for multilevel data) are computed. For items and scales, the distributions are summarised graphically and numerically.
1419

15-
This package integrates tightly with formr ([formr.org](https://formr.org)), an online survey framework and especially the data frames produced and marked up by the [formr R package](https://github.com/rubenarslan/formr), but is completely independent of it.
16-
17-
To do this, this package uses rmarkdown *partials*, that can be integrated in a larger rmarkdown document and then translated to HTML or PDF format.
20+
This package integrates tightly with formr ([formr.org](https://formr.org)), an online survey framework and especially the data frames produced and marked up by the [formr R package](https://github.com/rubenarslan/formr). However, codebook is completely independent of it.
1821

1922
Documentation
2023
-------------
2124

22-
Confer the help or: <https://rubenarslan.github.io/codebook>
25+
Confer the help or: <https://rubenarslan.github.io/codebook>. See the [vignette](https://rubenarslan.github.io/codebook/articles/codebook.html) for a quick example of an HTML document generated using `codebook`.
2326

2427
Use as a webapp
2528
---------------

cran-comments.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
## Resubmission
2-
This is a resubmission. In this version I have:
2+
This is a second resubmission. In this version I have:
3+
4+
* changed the title to omit "in R"
5+
* changed the description to put 'rmarkdown' in single quotes
6+
* added small executable examples to the codebook functions in the codebook.R.
7+
Some of these examples will, by necessity, create files.
8+
I know other examples also do this (e.g. knitr), but I'm unsure whether it's
9+
desirable. I've noted/warned about file creation in every example.
10+
* the function `codebook_items` cannot be run in examples.
11+
It causes an error during checking, probably because it indirectly includes
12+
an htmlwidget. It works fine in non-example use (tests, vignettes,
13+
interactively). Therefore, I wrapped the example in dontrun.
14+
* slightly changed the docs to be clearer
15+
16+
In the first resubmission I
317

418
* changed the LICENSE to MIT and used the appropriate file template (before I had BSD_2_clause and didn't know I had to use a specific file template)
519

docs/LICENSE-text.html

Lines changed: 2 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/articles/codebook.html

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
12 Bytes
Loading

0 commit comments

Comments
 (0)