-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
18 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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))) | ||
|
@@ -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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.