Skip to content

Commit

Permalink
make record_id_col optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ljwoodley committed Sep 6, 2023
1 parent 07844cb commit 12ffdd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 54 deletions.
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,11 @@ Imports:
Suggests:
RSQLite,
digest,
dotenv,
duckdb,
fs,
knitr (>= 1.18),
rmarkdown (>= 2.0),
testthat (>= 3.0.0),
tidyselect
testthat (>= 3.0.0)
VignetteBuilder: knitr
Config/testthat/edition: 3
RoxygenNote: 7.2.2
Expand Down
47 changes: 13 additions & 34 deletions R/dataframe_to_redcap_dictionary.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#' Create a REDCap data dictionary from a dataframe
#'
#' @param df the dataframe to generate the data dictionary for
#' @param record_id_col a column in the dataframe that uniquely identifies each record
#' @param form_name the form name to display in REDCap
#' @param write_to_csv If TRUE will write the data dictionary to a csv.
#' @param filename A string specifying the filename for the CSV.
#' @param record_id_col a column in the dataframe that uniquely identifies each record
#'
#' @return A redcap data dictionary
#' @export
Expand All @@ -22,17 +20,12 @@
#' email_col = c("[email protected]", "[email protected]", "[email protected]")
#' )
#'
#' redcap_data_dictionary <- dataframe_to_redcap_dictionary(df, "pk_col", "test_form")
#' redcap_data_dictionary <- dataframe_to_redcap_dictionary(
#' df, "pk_col", "test_form"
#' TRUE, "<output_path>.csv"
#' )
#' redcap_data_dictionary <- dataframe_to_redcap_dictionary(df, "test_form")
#' redcap_data_dictionary <- dataframe_to_redcap_dictionary(df, "test_form"m "character_col")
#' }
dataframe_to_redcap_dictionary <- function(df,
record_id_col,
form_name,
write_to_csv = FALSE,
filename = NULL) {
record_id_col = NULL) {
contains_emails <- function(col) {
email_pattern <- "^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$"
return(any(grepl(email_pattern, col)))
Expand All @@ -54,40 +47,26 @@ dataframe_to_redcap_dictionary <- function(df,
)
}

if (!record_id_col %in% names(df)) {
stop("The provided record_id_col does not exist in the input dataframe.")
# Rearrange the dataframe if a record_id_col is provided
if (!is.null(record_id_col)) {
ordered_df <- df |>
dplyr::select(record_id_col, dplyr::everything())
} else {
ordered_df <- df
}

# create the record_id and make it the first column in df
df_with_record_id <- df |>
dplyr::rename(record_id = tidyselect::all_of(record_id_col)) |>
dplyr::select("record_id", dplyr::everything())

df_with_ordered_cols <- df |>
dplyr::select(tidyselect::all_of(record_id_col), dplyr::everything())

redcap_data_dictionary <- data.frame(
field_name = names(df_with_record_id),
field_name = names(ordered_df),
form_name = form_name,
section_header = as.character(NA),
field_type = "text",
field_label = names(df_with_ordered_cols),
field_label = names(ordered_df),
select_choices_or_calculations = as.character(NA),
field_note = as.character(NA),
text_validation_type_or_show_slider_number = sapply(df_with_record_id, get_validation_type)
text_validation_type_or_show_slider_number = sapply(ordered_df, get_validation_type)
)

rownames(redcap_data_dictionary) <- NULL

if (write_to_csv) {
if (is.null(filename)) {
stop("Please provide a filename if you want to write to CSV.")
}
utils::write.csv(redcap_data_dictionary,
filename,
na = "",
row.names = FALSE)
}

return(redcap_data_dictionary)
}
21 changes: 4 additions & 17 deletions man/dataframe_to_redcap_dictionary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12ffdd5

Please sign in to comment.