R/codebook.R
Codebook component for single items
codebook_component_single_item(item, item_name, indent = "##")
| 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 |
# 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_missings <- FALSE #> if (haven::is.labelled(item)) { #> missings <- item[is.na(haven::zap_missing(item))] #> if (is.numeric(item)) { #> show_missings <- length(unique(haven::na_tag(missings))) > 1 #> item <- haven::zap_missing(item) #> } #> if (length(attributes(item)$labels) == 0 && is.numeric(item)) { #> item <- haven::zap_labels(item) #> } #> } #> old_height <- knitr::opts_chunk$get("fig.height") #> non_missing_choices <- attributes(item)[["labels"]] #> many_labels <- length(non_missing_choices) > 7 #> go_vertical <- !is.numeric(item) || 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(na.omit(item)) > length(non_missing_choices)) { #> choices <- unique(na.omit(item)) #> names(choices) <- choices #> non_missing_choices <- 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 (is.numeric(item) || dplyr::n_distinct(item) < 20) { #> attribs <- attributes(item) #> item <- item[!is.na(item)] #> attributes(item) <- attribs #> plot_labelled(item, item_name, wrap_at, go_vertical) #> } else { #> cat(dplyr::n_distinct(item), " unique, categorical values, so not shown.") #> } #> ``` #> #>  #> #> ```r #> knitr::opts_chunk$set(fig.height = old_height) #> ``` #> #> 0 missings. #> #> ##### Summary statistics {#BFIK_open_1_summary} #> #> ```r #> df = data.frame(item) #> names(df) = item_name #> pander::pander(codebook_table(df)) #> ``` #> #> #> ------------------------------------------------------------------------------- #> name label type type_options data_type #> ------------- ---------------------- --------------- -------------- ----------- #> BFIK_open_1 __Ich bin vielseitig rating_button 5 labelled #> interessiert.__ #> ------------------------------------------------------------------------------- #> #> Table: Table continues below #> #> #> ------------------------------------------------------------------------ #> ordered value_labels optional item_order missing #> --------- ---------------------------- ---------- ------------ --------- #> FALSE 1: Trifft überhaupt nicht 0 8 0 #> zu=1,2=2,3=3,4=4,5: Trifft #> voll und ganz zu=5 #> ------------------------------------------------------------------------ #> #> Table: Table continues below #> #> #> ---------------------------------------------------- #> complete n n_unique top_counts #> ---------- ---- ---------- ------------------------- #> 28 28 4 5: 16, 4: 8, 3: 3, 2: 1 #> ---------------------------------------------------- #> #> #> #> ```r #> if (show_missings) { #> plot_labelled(missings, item_name, wrap_at) #> } #> ``` #> #> ##### Item {#BFIK_open_1_item} #> #> #> ```r #> if (!is.null(item_info)) { #> item_info$label_parsed <- item_info$choices <- #> item_info$choice_list <- item_info$study_id <- item_info$id <- NULL #> pander::pander(as.data.frame(t(item_info))) #> } #> ``` #> #> #> ------------------------------------------------------------------------------ #> type type_options name label optional #> --------------- -------------- ------------- ---------------------- ---------- #> rating_button 5 BFIK_open_1 __Ich bin vielseitig 0 #> interessiert.__ #> ------------------------------------------------------------------------------ #> #> Table: Table continues below #> #> #> --------------------------------------------------- #> class showif value block_order item_order #> ------- -------- ------- ------------- ------------ #> NULL NULL NULL NULL 8 #> --------------------------------------------------- #> #> #> ##### Value labels {#BFIK_open_1_labels} #> #> ```r #> if (!is.null(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 --> #>