diff --git a/DESCRIPTION b/DESCRIPTION index 54d4b2d..c624175 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/R/dataframe_to_redcap_dictionary.R b/R/dataframe_to_redcap_dictionary.R index 7ab4dd1..3dce032 100644 --- a/R/dataframe_to_redcap_dictionary.R +++ b/R/dataframe_to_redcap_dictionary.R @@ -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("test@example.com", "test.2@example.edu", "3test@example.net") #' ) #' -#' 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, ".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) } diff --git a/man/dataframe_to_redcap_dictionary.Rd b/man/dataframe_to_redcap_dictionary.Rd index 89f071e..b2e4cfc 100644 --- a/man/dataframe_to_redcap_dictionary.Rd +++ b/man/dataframe_to_redcap_dictionary.Rd @@ -4,24 +4,14 @@ \alias{dataframe_to_redcap_dictionary} \title{Create a REDCap data dictionary from a dataframe} \usage{ -dataframe_to_redcap_dictionary( - df, - record_id_col, - form_name, - write_to_csv = FALSE, - filename = NULL -) +dataframe_to_redcap_dictionary(df, form_name, record_id_col = NULL) } \arguments{ \item{df}{the dataframe to generate the data dictionary for} -\item{record_id_col}{a column in the dataframe that uniquely identifies each record} - \item{form_name}{the form name to display in REDCap} -\item{write_to_csv}{If TRUE will write the data dictionary to a csv.} - -\item{filename}{A string specifying the filename for the CSV.} +\item{record_id_col}{a column in the dataframe that uniquely identifies each record} } \value{ A redcap data dictionary @@ -42,10 +32,7 @@ df <- data.frame( email_col = c("test@example.com", "test.2@example.edu", "3test@example.net") ) -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, ".csv" -) +redcap_data_dictionary <- dataframe_to_redcap_dictionary(df, "test_form") +redcap_data_dictionary <- dataframe_to_redcap_dictionary(df, "test_form"m "character_col") } }