|
3 | 3 | #' The codebook function assists with the creation of a codebook for a given |
4 | 4 | #' data frame. |
5 | 5 | #' |
6 | | -#' @param x A data frame |
7 | | -#' @param label Optional vector of variable labels. Default is NA. |
| 6 | +#' Codebook expects that `df ` is a data frame that you have read into memory |
| 7 | +#' from a saved data file. Please provide the path to the saved data file. This |
| 8 | +#' function gets selected attributes about file saved at `path` and stores |
| 9 | +#' those attributes in a data frame, which is later turned into a flextable and |
| 10 | +#' added to the codebook document. |
8 | 11 | #' |
9 | | -#' @return A list of lists |
| 12 | +#' @param df The saved file at `path`, read into memory as a data frame |
| 13 | +#' @param path The path to the saved dataset of interest |
| 14 | +#' @param title Optional title |
| 15 | +#' @param subtitle Optional subtitle |
| 16 | +#' @param description Text description of the dataset |
| 17 | +#' |
| 18 | +#' @return An rdocx object that can be printed to a Word document |
10 | 19 | #' @export |
11 | 20 | #' |
12 | 21 | #' @examples |
13 | | -#' data("mtcars") |
14 | | -#' |
15 | | -#' # Optional: Create label vector |
16 | | -#' labels <- c("Miles Per Gallon", "Number of cylinders") |
17 | | -#' |
18 | | -#' # Codebook with one variable |
19 | | -#' codebook(mtcars["mpg"], label = labels) |
20 | | -#' |
21 | | -#' # Codebook with multiple variables |
22 | | -#' varlist <- c("mpg", "cyl") |
23 | | -#' codebook(mtcars[varlist], label = labels) |
24 | | -#' |
25 | | -#' # Codebook with all variables |
26 | | -#' codebook(mtcars) |
27 | | -#' |
28 | | -#' # Do not use: |
29 | | -#' # lapply(mtcars[varlist], codebook) |
30 | | -codebook <- function(x, label = NA){ |
31 | | - if (!is.data.frame(x)){ |
32 | | - stop("x must be a data frame") |
| 22 | +#' # codebook_detect_5wk <- codebook( |
| 23 | +#' # df = detect_5wk %>% select(1:2), |
| 24 | +#' # path = "../data/detect_5wk.csv", |
| 25 | +#' # title = "Detection of Elder abuse Through Emergency Care Technicians (DETECT)", |
| 26 | +#' # subtitle = "5-Week Pilot Study", |
| 27 | +#' # description = description |
| 28 | +#' # ) %>% |
| 29 | +#' # print(target = "example_officer_codebook.docx") |
| 30 | +codebook <- function(df, path = NA, title = NA, subtitle = NA, description = NA) { |
| 31 | + |
| 32 | + # =========================================================================== |
| 33 | + # Create an empty Word rdocx object |
| 34 | + # default template contains only an empty paragraph |
| 35 | + # Using cursor_begin and body_remove, we can delete it |
| 36 | + # =========================================================================== |
| 37 | + rdocx <- officer::read_docx() %>% |
| 38 | + officer::cursor_begin() %>% |
| 39 | + officer::body_remove() |
| 40 | + |
| 41 | + # =========================================================================== |
| 42 | + # Optionally add title and subtitle to top of codebook |
| 43 | + # =========================================================================== |
| 44 | + codebook_shell <- codebook_add_title( |
| 45 | + rdocx = rdocx, |
| 46 | + title = title, |
| 47 | + subtitle = subtitle |
| 48 | + ) |
| 49 | + |
| 50 | + # =========================================================================== |
| 51 | + # Add metadata to codebook shell |
| 52 | + # =========================================================================== |
| 53 | + # Get metadata |
| 54 | + df_metadata <- codebook_get_df_attributes(df, path = path) %>% |
| 55 | + flextable::regulartable() %>% |
| 56 | + codebook_theme_df_attributes() |
| 57 | + |
| 58 | + # Add metadata to codebook |
| 59 | + rdocx <- rdocx %>% |
| 60 | + flextable::body_add_flextable(df_metadata) |
| 61 | + |
| 62 | + # =========================================================================== |
| 63 | + # Optionally Add dataset description |
| 64 | + # =========================================================================== |
| 65 | + if (!is.na(description)) { |
| 66 | + # Add Description header |
| 67 | + rdocx <- rdocx %>% |
| 68 | + codebook_add_section_header("Description:") |
| 69 | + |
| 70 | + # Add dataset description to codebook |
| 71 | + rdocx <- rdocx %>% |
| 72 | + codebook_add_description(description) |
33 | 73 | } |
34 | 74 |
|
35 | | - i <- 1 |
36 | | - for (vars in x) { |
37 | | - cat("--------------------------------------------------------------------------------------------------------------", "\n") |
38 | | - cat(names(x)[i], "\n") |
39 | | - cat("--------------------------------------------------------------------------------------------------------------", "\n") |
40 | | - cat("Label:", label[i], "\n") |
41 | | - cat("Class:", class(vars), "\n") |
42 | | - cat("Unique:", length(unique(vars)), "\n") |
43 | | - cat("Miss:", sum(is.na(vars)), "\n") |
44 | | - cat("Summary: \n") |
45 | | - print(summary(vars)) |
46 | | - cat("\n") |
47 | | - cat("\n") |
48 | | - i <- i + 1 |
| 75 | + # =========================================================================== |
| 76 | + # Iterate over every column in df - control with dplyr::select |
| 77 | + # Add column attributes and summary statistics to rdocx object |
| 78 | + # =========================================================================== |
| 79 | + |
| 80 | + # Add column Attributes header |
| 81 | + rdocx <- rdocx %>% |
| 82 | + codebook_add_section_header("Column Attributes:") |
| 83 | + |
| 84 | + # Create vector of column names |
| 85 | + col_nms <- names(df) |
| 86 | + |
| 87 | + # Iterate over all columns |
| 88 | + # ------------------------ |
| 89 | + for (i in seq_along(col_nms)) { |
| 90 | + |
| 91 | + # Get column attributes |
| 92 | + table_var_attributes <- df %>% |
| 93 | + codebook_get_col_attributes(col_nms[[i]]) %>% |
| 94 | + flextable::regulartable() %>% |
| 95 | + codebook_theme_col_attr() |
| 96 | + |
| 97 | + # Add two blank lines above the attributes table |
| 98 | + rdocx <- rdocx %>% |
| 99 | + officer::body_add_par("") %>% |
| 100 | + officer::body_add_par("") |
| 101 | + |
| 102 | + # Add column attributes flextable to the rdocx object |
| 103 | + rdocx <- rdocx %>% |
| 104 | + flextable::body_add_flextable(table_var_attributes) |
| 105 | + |
| 106 | + # Get summary statistics |
| 107 | + summary_stats <- df %>% |
| 108 | + codebook_add_summary_stats(col_nms[[i]]) %>% |
| 109 | + codebook_summary_stats_to_ft() |
| 110 | + |
| 111 | + # Add summary statistics flextable to the codebook object |
| 112 | + rdocx <- rdocx %>% |
| 113 | + flextable::body_add_flextable(summary_stats) |
49 | 114 | } |
| 115 | + |
| 116 | + # =========================================================================== |
| 117 | + # Return rdocx object that can be printed to a Word document |
| 118 | + # =========================================================================== |
| 119 | + rdocx |
50 | 120 | } |
51 | 121 |
|
0 commit comments