Codebook component for single items

codebook_component_single_item(item, item_name, indent = "##")

Arguments

item

an item with attributes set

item_name

the item name

indent

add # to this to make the headings in the components lower-level. defaults to beginning at h2

Examples

# will generate figure in a temporary directory old_base_dir <- knitr::opts_knit$get("base.dir") knitr::opts_knit$set(base.dir = tempdir()) on.exit(knitr::opts_knit$set(base.dir = old_base_dir)) data("bfi") codebook_component_single_item(bfi$BFIK_open_1, "BFIK_open_1")
#> #> #> #> #### BFIK_open_1 {#BFIK_open_1 .tabset} #> #> __Ich bin vielseitig interessiert.__ #> #> ##### Distribution {#BFIK_open_1_distribution} #> #> ```r #> show_missing_values <- FALSE #> if (has_labels(item)) { #> missing_values <- item[is.na(haven::zap_missing(item))] #> attributes(missing_values) <- attributes(item) #> if (!is.null(attributes(item)$labels)) { #> attributes(missing_values)$labels <- attributes(missing_values)$labels[is.na(attributes(missing_values)$labels)] #> attributes(item)$labels <- attributes(item)$labels[!is.na(attributes(item)$labels)] #> } #> if (is.double(item)) { #> show_missing_values <- length(unique(haven::na_tag(missing_values))) > 1 #> item <- haven::zap_missing(item) #> } #> if (length(item_attributes$labels) == 0 && is.numeric(item)) { #> item <- haven::zap_labels(item) #> } #> } #> item_nomiss <- item[!is.na(item)] #> #> # unnest mc_multiple and so on #> if ( #> is.character(item_nomiss) && #> stringr::str_detect(item_nomiss, stringr::fixed(", ")) && #> (exists("type", item_info) && #> stringr::str_detect(item_info$type, pattern = stringr::fixed("multiple"))) #> ) { #> item_nomiss <- unlist(stringr::str_split(item_nomiss, pattern = stringr::fixed(", "))) #> } #> attributes(item_nomiss) <- attributes(item) #> #> old_height <- knitr::opts_chunk$get("fig.height") #> non_missing_choices <- item_attributes[["labels"]] #> many_labels <- length(non_missing_choices) > 7 #> go_vertical <- !is.numeric(item_nomiss) || many_labels #> if ( go_vertical ) { #> # numeric items are plotted horizontally (because that's what usually expected) #> # categorical items are plotted vertically because we can use the screen real estate better this way #> #> if (is.null(choices) || #> dplyr::n_distinct(item_nomiss) > length(non_missing_choices)) { #> non_missing_choices <- unique(item_nomiss) #> names(non_missing_choices) <- non_missing_choices #> } #> choice_multiplier <- old_height/6.5 #> new_height <- 2 + choice_multiplier * length(non_missing_choices) #> new_height <- ifelse(new_height > 20, 20, new_height) #> new_height <- ifelse(new_height < 1, 1, new_height) #> knitr::opts_chunk$set(fig.height = new_height) #> } #> #> wrap_at <- knitr::opts_chunk$get("fig.width") * 10 #> ``` #> #> ```r #> # todo: if there are free-text choices mingled in with the pre-defined ones, don't show #> # todo: show rare items if they are pre-defined #> # todo: bin rare responses into "other category" #> if (!length(item_nomiss)) { #> cat("No non-missing values to show.") #> } else if (is.numeric(item_nomiss) || dplyr::n_distinct(item_nomiss) < 20) { #> plot_labelled(item_nomiss, item_name, wrap_at, go_vertical) #> } else { #> cat(dplyr::n_distinct(item_nomiss), " unique, categorical values, so not shown.") #> } #> ``` #> #> ![plot of chunk distribution](figure/BFIK_open_1_distribution-1.png) #> #> ```r #> knitr::opts_chunk$set(fig.height = old_height) #> ``` #> #> 0 missing values. #> #> ##### Summary statistics {#BFIK_open_1_summary} #> #> ```r #> attributes(item) <- item_attributes #> df = data.frame(item, stringsAsFactors = FALSE) #> names(df) = html_item_name #> escaped_table(codebook_table(df)) #> ``` #> #> #> #> |name |label |type |type_options |data_type |value_labels |optional |item_order |missing |complete |n |mean |sd |p0 |p25 |p50 |p75 |p100 |hist | #> |:-----------|:------------------------------------|:-------------|:------------|:---------|:-------------------------------------------------------------------------------------------------------------------------------------------|:--------|:----------|:-------|:--------|:--|:----|:----|:--|:---|:---|:---|:----|:--------| #> |BFIK_open_1 |__Ich bin vielseitig interessiert.__ |rating_button |5 |numeric |1. 1: Trifft überhaupt nicht zu,<br>2. 2,<br>3. 3,<br>4. 4,<br>5. 5: Trifft voll und ganz zu,<br>NA. Item was never rendered for this user. |0 |8 |0 |28 |28 |4.39 |0.83 |2 |4 |5 |5 |5 |▁▁▂▁▁▃▁▇ | #> #> #> #> ```r #> if (show_missing_values) { #> plot_labelled(missing_values, item_name, wrap_at) #> } #> ``` #> #> ##### Item {#BFIK_open_1_item} #> #> #> ```r #> if (!is.null(item_info)) { #> # don't show choices again, if they're basically same thing as value labels #> if (!is.null(choices) && !is.null(item_info$choices) && #> all(names(na.omit(choices)) == item_info$choices) && #> all(na.omit(choices) == names(item_info$choices))) { #> item_info$choices <- NULL #> } #> item_info$label_parsed <- #> item_info$choice_list <- item_info$study_id <- item_info$id <- NULL #> pander::pander(item_info) #> } #> ``` #> #> #> #> * **type**: rating_button #> * **type_options**: 5 #> * **name**: BFIK_open_1 #> * **label**: __Ich bin vielseitig interessiert.__ #> * **optional**: _0_ #> * **class**: #> * **showif**: #> * **value**: #> * **block_order**: #> * **item_order**: _8_ #> #> <!-- end of list --> #> #> #> ##### Value labels {#BFIK_open_1_labels} #> #> ```r #> if (!is.null(choices) && length(choices) && length(choices) < 30) { #> pander::pander(as.list(choices)) #> } #> ``` #> #> #> #> * **1: Trifft überhaupt nicht zu**: _1_ #> * **2**: _2_ #> * **3**: _3_ #> * **4**: _4_ #> * **5: Trifft voll und ganz zu**: _5_ #> * **Item was never rendered for this user.**: _NA_ #> #> <!-- end of list --> #>