Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a function get_project_values() #24

Open
pbchase opened this issue Feb 12, 2025 · 0 comments
Open

Create a function get_project_values() #24

pbchase opened this issue Feb 12, 2025 · 0 comments

Comments

@pbchase
Copy link
Contributor

pbchase commented Feb 12, 2025

Create a function get_project_values(). The function should accept these parameters:

  • redcap_uri URL to a REDCap host's API interface
  • token An API token for the project of interest
  • forms A vector of forms to fill
  • events A vector of events to fill
  • fields_and_responses a data frame of fields and field responses with weights, means, sds, and other attributes that describe how to fill the fields

The function should copy all the code in

metadata <- REDCapR::redcap_metadata_read(
token = credentials$token,
redcap_uri = credentials$redcap_uri
)$data
# identify the record_id column
record_id_name <- metadata |>
filter(row_number() == 1) |>
pull(field_name)
# identify the forms to fill
forms_to_fill <- metadata |>
distinct(form_name) |>
filter(!form_name %in% c("randomization")) |>
pull(form_name)
field_types_we_know_how_to_fill <- c(
"checkbox",
"dropdown",
"notes",
"radio",
"text",
"yesno",
"truefalse",
"slider"
)
metadata_to_populate <-
metadata |>
# exclude the record_id field from filling
filter(field_name != record_id_name) |>
# Filter for forms we want to fill
filter(form_name %in% forms_to_fill) |>
# Exclude descriptive fields because they are not fillable
filter(field_type != "descriptive") |>
# Exclude calc fields because we don't control them
# (though we might strobe them to trigger writes in a future version)
filter(field_type != "calc") |>
# Exclude file fields
# (Eventually we'll add an option to populate them with a dummy file)
filter(field_type != "file") |>
# Filter for fields not hidden via branching logic.
filter(is.na(branching_logic)) |>
# filter for fields we no how to fill
filter(field_type %in% field_types_we_know_how_to_fill)
# Make a vector of record IDs to populate
# Get highest existing id
read_result <- REDCapR::redcap_read(
token = credentials$token,
redcap_uri = credentials$redcap_uri
)
if (nrow(read_result$data) > 0) {
max_existing_id <- max(read_result$data$record_id)
} else {
max_existing_id <- 0
}
# choose which IDs to use
first_id <- max_existing_id + 1
number_of_records_to_populate <- 5
record_ids <- seq(first_id, first_id + number_of_records_to_populate)
# get the categorical field responses in a long table and populate them
long_categorical_field_responses <- get_long_categorical_field_responses(metadata_to_populate)
long_text_fields <- get_long_text_fields(metadata_to_populate)
long_slider_fields <- get_long_slider_fields(metadata_to_populate)
long_notes_fields <- get_long_notes_fields(metadata_to_populate)
long_fields_and_responses <- bind_rows(
long_categorical_field_responses,
long_text_fields,
long_slider_fields,
long_notes_fields
)
picked_values <-
purrr::map(
record_ids,
get_one_rectangle_of_values,
record_id_name,
forms_to_fill,
long_fields_and_responses
) |>
bind_rows()

Test project attributes

To the above code, add a call to REDCapR::redcap_project_info_read() before metadata <- REDCapR::redcap_metadata_read( to test the attributes of the project to verify that we can fill it. Test each of these returned values independently

  • is_longitudinal
  • has_repeating_instruments_or_events
  • randomization_enabled

They all need to be logical. In this first version of this function, we support only:

  • !has_repeating_instruments_or_events
  • !randomization_enabled

Consider testing these with the methods of the checkmate package.

Get events and event mappings

At picked_values <- (line 111 in the stolen code block), test is_longitudinal. If is_longitudinal,

  1. Call REDCapR::redcap_event_read() and REDCapR::redcap_event_instruments() to get event and event mapping data.
  2. Join it into a single data frame.
  3. Filter that data frame for forms and events in the forms and events vector parameters.
  4. Generate a vector of distinct event names and a same-ordered list of form vectors mapped to each event name.

...Else if !is_longitudinal,

  1. Move
    # identify the forms to fill
    forms_to_fill <- metadata |>
    distinct(form_name) |>
    filter(!form_name %in% c("randomization")) |>
    pull(form_name)
    into this block.
  2. Remove filter(!form_name %in% c("randomization")) |>
  3. Filter the form vector for forms in the forms vector parameter
  4. Set your vector of distinct event names to c(NA_character)
  5. write your single form vector to your list form vectors.

Turn

picked_values <-
purrr::map(
record_ids,
get_one_rectangle_of_values,
record_id_name,
forms_to_fill,
long_fields_and_responses
) |>
bind_rows()
into a function called get_all_rows_in_rectangle().

Iterate through the events

Iterate over your vector of distinct event names with purrr::map() calling get_all_rows_in_rectangle() for each event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant