diff --git a/articles/REDCapCAST.html b/articles/REDCapCAST.html index 8afd43c..20633c0 100644 --- a/articles/REDCapCAST.html +++ b/articles/REDCapCAST.html @@ -108,10 +108,12 @@ to specify only the fields or instruments you need, and avoid to save any of the data locally, but always source from REDCap to avoid possibly insecure local storage of sensitive data.

-easy_redcap(uri = "YOUR URI", 
-            project.name = "MY_PROJECT", 
-            widen.data = TRUE,
-            fields = c("record_id", "OTHER FIELDS"))
+easy_redcap( + uri = "YOUR URI", + project.name = "MY_PROJECT", + widen.data = TRUE, + fields = c("record_id", "OTHER FIELDS") +)

Splitting the dataset @@ -2232,10 +2234,12 @@ Note: as opposed to the build in datediff() this handles counting years as integ

To save the metadata as labels in the dataset, we can save field labels and the choices from radio buttons and dropdown features:

-labelled_data <- 
-  apply_field_label(data=redcapcast_data,
-                       meta=redcapcast_meta) |> 
-  apply_factor_labels(meta=redcapcast_meta)
+labelled_data <- + apply_field_label( + data = redcapcast_data, + meta = redcapcast_meta + ) |> + apply_factor_labels(meta = redcapcast_meta)

The REDCap_split function splits the data set into a list of data.frames.

@@ -2244,7 +2248,7 @@ list of data.frames.

records = labelled_data, metadata = redcapcast_meta, forms = "all" - ) |> + ) |> # Next steps cleans up and removes generic columns sanitize_split() str(list) @@ -2315,127 +2319,133 @@ widen the data, by transforming the list of data.frames to a single data.frame with one row for each subject/record_id (wide data format):

-wide_data <- redcap_wider(list) 
+wide_data <- redcap_wider(list,
+  event.glue = "{.value}____{redcap_event_name}",
+  inst.glue = "{.value}____{redcap_repeat_instance}"
+)
 #> Joining with `by = join_by(record_id)`
 #> Joining with `by = join_by(record_id)`
 wide_data |> str()
 #> tibble [6 × 49] (S3: tbl_df/tbl/data.frame)
-#>  $ record_id                   : num [1:6] 1 2 3 4 5 6
+#>  $ record_id                         : num [1:6] 1 2 3 4 5 6
 #>   ..- attr(*, "label")= chr "ID"
-#>  $ cpr                         : chr [1:6] "1203401OB4" "0102342303" "2301569823" "0204051342" ...
+#>  $ cpr                               : chr [1:6] "1203401OB4" "0102342303" "2301569823" "0204051342" ...
 #>   ..- attr(*, "label")= chr "CPR (Danish civil registration number)"
-#>  $ inclusion                   : Date[1:6], format: "2023-03-13" "2023-03-01" ...
-#>  $ inclusion_time              : 'hms' num [1:6] 12:38:49 10:38:57 12:01:07 20:39:19 ...
+#>  $ inclusion                         : Date[1:6], format: "2023-03-13" "2023-03-01" ...
+#>  $ inclusion_time                    : 'hms' num [1:6] 12:38:49 10:38:57 12:01:07 20:39:19 ...
 #>   ..- attr(*, "units")= chr "secs"
-#>  $ dob                         : Date[1:6], format: "1940-03-12" "1934-02-01" ...
-#>  $ age                         : num [1:6] 83 89.1 66.1 117.9 126.2 ...
+#>  $ dob                               : Date[1:6], format: "1940-03-12" "1934-02-01" ...
+#>  $ age                               : num [1:6] 83 89.1 66.1 117.9 126.2 ...
 #>   ..- attr(*, "label")= chr "Age\r\nNote: Apparently, the build in datediff() function does not handle counting whole years. This results in"| __truncated__
-#>  $ age_integer                 : num [1:6] 83 89 66 117 126 91
+#>  $ age_integer                       : num [1:6] 83 89 66 117 126 91
 #>   ..- attr(*, "label")= chr "Age integer\r\nNote: as opposed to the build in datediff() this handles counting years as integers very well. C"| __truncated__
-#>  $ sex                         : chr [1:6] "female" "male" "male" "female" ...
+#>  $ sex                               : chr [1:6] "female" "male" "male" "female" ...
 #>   ..- attr(*, "label")= chr "Legal sex"
-#>  $ cohabitation                : 'labelled' chr [1:6] "Yes" "Yes" "No" NA ...
+#>  $ cohabitation                      : 'labelled' chr [1:6] "Yes" "Yes" "No" NA ...
 #>   ..- attr(*, "label")= chr "Cohabitation"
 #>   ..- attr(*, "labels")= Named chr [1:2] "1" "2"
 #>   .. ..- attr(*, "names")= chr [1:2] "Yes" "No"
-#>  $ hypertension                : 'labelled' chr [1:6] "No" "No" "Yes" NA ...
+#>  $ hypertension                      : 'labelled' chr [1:6] "No" "No" "Yes" NA ...
 #>   ..- attr(*, "label")= chr "Hypertension"
 #>   ..- attr(*, "labels")= Named chr [1:2] "1" "2"
 #>   .. ..- attr(*, "names")= chr [1:2] "Yes" "No"
-#>  $ diabetes                    : 'labelled' chr [1:6] "Yes" "No" "Yes" NA ...
+#>  $ diabetes                          : 'labelled' chr [1:6] "Yes" "No" "Yes" NA ...
 #>   ..- attr(*, "label")= chr "Diabetes"
 #>   ..- attr(*, "labels")= Named chr [1:2] "1" "2"
 #>   .. ..- attr(*, "names")= chr [1:2] "Yes" "No"
-#>  $ region                      : 'labelled' chr [1:6] "East" "South" "North" NA ...
+#>  $ region                            : 'labelled' chr [1:6] "East" "South" "North" NA ...
 #>   ..- attr(*, "label")= chr "Region"
 #>   ..- attr(*, "labels")= Named chr [1:4] "1" "2" "3" "4"
 #>   .. ..- attr(*, "names")= chr [1:4] "North" "East" "South" "West"
-#>  $ baseline_data_start_complete: chr [1:6] "Incomplete" "Incomplete" "Incomplete" "Incomplete" ...
-#>  $ mrs_assessed_inclusion      : 'labelled' chr [1:6] "Yes" "Yes" NA NA ...
+#>  $ baseline_data_start_complete      : chr [1:6] "Incomplete" "Incomplete" "Incomplete" "Incomplete" ...
+#>  $ mrs_assessed____inclusion         : 'labelled' chr [1:6] "Yes" "Yes" NA NA ...
 #>   ..- attr(*, "label")= chr "Assesed"
 #>   ..- attr(*, "labels")= Named chr [1:2] "1" "2"
 #>   .. ..- attr(*, "names")= chr [1:2] "Yes" "No"
-#>  $ mrs_assessed_follow1        : 'labelled' chr [1:6] NA "Yes" "Yes" NA ...
+#>  $ mrs_assessed____follow1           : 'labelled' chr [1:6] NA "Yes" "Yes" NA ...
 #>   ..- attr(*, "label")= chr "Assesed"
 #>   ..- attr(*, "labels")= Named chr [1:2] "1" "2"
 #>   .. ..- attr(*, "names")= chr [1:2] "Yes" "No"
-#>  $ mrs_assessed_follow2        : 'labelled' chr [1:6] NA NA "Yes" NA ...
+#>  $ mrs_assessed____follow2           : 'labelled' chr [1:6] NA NA "Yes" NA ...
 #>   ..- attr(*, "label")= chr "Assesed"
 #>   ..- attr(*, "labels")= Named chr [1:2] "1" "2"
 #>   .. ..- attr(*, "names")= chr [1:2] "Yes" "No"
-#>  $ mrs_date_inclusion          : Date[1:6], format: "2023-03-13" "2023-03-07" ...
-#>  $ mrs_date_follow1            : Date[1:6], format: NA "2023-03-09" ...
-#>  $ mrs_date_follow2            : Date[1:6], format: NA NA ...
-#>  $ mrs_score_inclusion         : 'labelled' num [1:6] 1 1 NA NA NA NA
+#>  $ mrs_date____inclusion             : Date[1:6], format: "2023-03-13" "2023-03-07" ...
+#>  $ mrs_date____follow1               : Date[1:6], format: NA "2023-03-09" ...
+#>  $ mrs_date____follow2               : Date[1:6], format: NA NA ...
+#>  $ mrs_score____inclusion            : 'labelled' num [1:6] 1 1 NA NA NA NA
 #>   ..- attr(*, "label")= chr "mRS score"
 #>   ..- attr(*, "labels")= Named chr [1:6] "0" "1" "2" "3" ...
 #>   .. ..- attr(*, "names")= chr [1:6] "0" "1" "2" "3" ...
-#>  $ mrs_score_follow1           : 'labelled' num [1:6] NA 3 2 NA NA NA
+#>  $ mrs_score____follow1              : 'labelled' num [1:6] NA 3 2 NA NA NA
 #>   ..- attr(*, "label")= chr "mRS score"
 #>   ..- attr(*, "labels")= Named chr [1:6] "0" "1" "2" "3" ...
 #>   .. ..- attr(*, "names")= chr [1:6] "0" "1" "2" "3" ...
-#>  $ mrs_score_follow2           : 'labelled' num [1:6] NA NA 1 NA NA NA
+#>  $ mrs_score____follow2              : 'labelled' num [1:6] NA NA 1 NA NA NA
 #>   ..- attr(*, "label")= chr "mRS score"
 #>   ..- attr(*, "labels")= Named chr [1:6] "0" "1" "2" "3" ...
 #>   .. ..- attr(*, "names")= chr [1:6] "0" "1" "2" "3" ...
-#>  $ mrs_complete_inclusion      : chr [1:6] "Incomplete" "Incomplete" NA NA ...
-#>  $ mrs_complete_follow1        : chr [1:6] NA "Incomplete" "Incomplete" NA ...
-#>  $ mrs_complete_follow2        : chr [1:6] NA NA "Incomplete" NA ...
-#>  $ event_datetime_1_follow1    : POSIXct[1:6], format: NA "2024-01-18 12:49:42" ...
-#>  $ event_datetime_1_follow2    : POSIXct[1:6], format: NA NA ...
-#>  $ event_age_1_follow1         : num [1:6] NA NA NA 96 127 NA
+#>  $ mrs_complete____inclusion         : chr [1:6] "Incomplete" "Incomplete" NA NA ...
+#>  $ mrs_complete____follow1           : chr [1:6] NA "Incomplete" "Incomplete" NA ...
+#>  $ mrs_complete____follow2           : chr [1:6] NA NA "Incomplete" NA ...
+#>  $ event_datetime____1____follow1    : POSIXct[1:6], format: NA "2024-01-18 12:49:42" ...
+#>  $ event_datetime____1____follow2    : POSIXct[1:6], format: NA NA ...
+#>  $ event_age____1____follow1         : num [1:6] NA NA NA 96 127 NA
 #>   ..- attr(*, "label")= chr "Age at event"
-#>  $ event_age_1_follow2         : num [1:6] NA NA NA 118 NA NA
+#>  $ event_age____1____follow2         : num [1:6] NA NA NA 118 NA NA
 #>   ..- attr(*, "label")= chr "Age at event"
-#>  $ event_type_1_follow1        : 'labelled' chr [1:6] NA "TIA" "AIS" "TIA" ...
+#>  $ event_type____1____follow1        : 'labelled' chr [1:6] NA "TIA" "AIS" "TIA" ...
 #>   ..- attr(*, "label")= chr "Neurovascular event"
 #>   ..- attr(*, "labels")= Named chr [1:5] "1" "2" "3" "4" ...
 #>   .. ..- attr(*, "names")= chr [1:5] "TIA" "AIS" "ICH" "SAH" ...
-#>  $ event_type_1_follow2        : 'labelled' chr [1:6] NA NA "ICH" "AIS" ...
+#>  $ event_type____1____follow2        : 'labelled' chr [1:6] NA NA "ICH" "AIS" ...
 #>   ..- attr(*, "label")= chr "Neurovascular event"
 #>   ..- attr(*, "labels")= Named chr [1:5] "1" "2" "3" "4" ...
 #>   .. ..- attr(*, "names")= chr [1:5] "TIA" "AIS" "ICH" "SAH" ...
-#>  $ new_event_complete_1_follow1: chr [1:6] NA "Incomplete" "Incomplete" "Complete" ...
-#>  $ new_event_complete_1_follow2: chr [1:6] NA NA "Incomplete" "Complete" ...
-#>  $ event_datetime_2_follow1    : POSIXct[1:6], format: NA NA ...
-#>  $ event_datetime_2_follow2    : POSIXct[1:6], format: NA NA ...
-#>  $ event_datetime_3_follow1    : POSIXct[1:6], format: NA NA ...
-#>  $ event_datetime_3_follow2    : POSIXct[1:6], format: NA NA ...
-#>  $ event_age_2_follow1         : num [1:6] NA NA NA 105 127 NA
+#>  $ new_event_complete____1____follow1: chr [1:6] NA "Incomplete" "Incomplete" "Complete" ...
+#>  $ new_event_complete____1____follow2: chr [1:6] NA NA "Incomplete" "Complete" ...
+#>  $ event_datetime____2____follow1    : POSIXct[1:6], format: NA NA ...
+#>  $ event_datetime____2____follow2    : POSIXct[1:6], format: NA NA ...
+#>  $ event_datetime____3____follow1    : POSIXct[1:6], format: NA NA ...
+#>  $ event_datetime____3____follow2    : POSIXct[1:6], format: NA NA ...
+#>  $ event_age____2____follow1         : num [1:6] NA NA NA 105 127 NA
 #>   ..- attr(*, "label")= chr "Age at event"
-#>  $ event_age_2_follow2         : num [1:6] NA NA NA 118 NA NA
+#>  $ event_age____2____follow2         : num [1:6] NA NA NA 118 NA NA
 #>   ..- attr(*, "label")= chr "Age at event"
-#>  $ event_age_3_follow1         : num [1:6] NA NA NA NA NA NA
+#>  $ event_age____3____follow1         : num [1:6] NA NA NA NA NA NA
 #>   ..- attr(*, "label")= chr "Age at event"
-#>  $ event_age_3_follow2         : num [1:6] NA NA NA 118 NA NA
+#>  $ event_age____3____follow2         : num [1:6] NA NA NA 118 NA NA
 #>   ..- attr(*, "label")= chr "Age at event"
-#>  $ event_type_2_follow1        : 'labelled' chr [1:6] NA NA "ICH" "TIA" ...
+#>  $ event_type____2____follow1        : 'labelled' chr [1:6] NA NA "ICH" "TIA" ...
 #>   ..- attr(*, "label")= chr "Neurovascular event"
 #>   ..- attr(*, "labels")= Named chr [1:5] "1" "2" "3" "4" ...
 #>   .. ..- attr(*, "names")= chr [1:5] "TIA" "AIS" "ICH" "SAH" ...
-#>  $ event_type_2_follow2        : 'labelled' chr [1:6] NA NA "TIA" "ICH" ...
+#>  $ event_type____2____follow2        : 'labelled' chr [1:6] NA NA "TIA" "ICH" ...
 #>   ..- attr(*, "label")= chr "Neurovascular event"
 #>   ..- attr(*, "labels")= Named chr [1:5] "1" "2" "3" "4" ...
 #>   .. ..- attr(*, "names")= chr [1:5] "TIA" "AIS" "ICH" "SAH" ...
-#>  $ event_type_3_follow1        : 'labelled' chr [1:6] NA NA NA NA ...
+#>  $ event_type____3____follow1        : 'labelled' chr [1:6] NA NA NA NA ...
 #>   ..- attr(*, "label")= chr "Neurovascular event"
 #>   ..- attr(*, "labels")= Named chr [1:5] "1" "2" "3" "4" ...
 #>   .. ..- attr(*, "names")= chr [1:5] "TIA" "AIS" "ICH" "SAH" ...
-#>  $ event_type_3_follow2        : 'labelled' chr [1:6] NA NA "AIS" "Unknown" ...
+#>  $ event_type____3____follow2        : 'labelled' chr [1:6] NA NA "AIS" "Unknown" ...
 #>   ..- attr(*, "label")= chr "Neurovascular event"
 #>   ..- attr(*, "labels")= Named chr [1:5] "1" "2" "3" "4" ...
 #>   .. ..- attr(*, "names")= chr [1:5] "TIA" "AIS" "ICH" "SAH" ...
-#>  $ new_event_complete_2_follow1: chr [1:6] NA NA "Incomplete" "Complete" ...
-#>  $ new_event_complete_2_follow2: chr [1:6] NA NA "Incomplete" "Incomplete" ...
-#>  $ new_event_complete_3_follow1: chr [1:6] NA NA NA NA ...
-#>  $ new_event_complete_3_follow2: chr [1:6] NA NA "Incomplete" "Complete" ...
+#> $ new_event_complete____2____follow1: chr [1:6] NA NA "Incomplete" "Complete" ... +#> $ new_event_complete____2____follow2: chr [1:6] NA NA "Incomplete" "Incomplete" ... +#> $ new_event_complete____3____follow1: chr [1:6] NA NA NA NA ... +#> $ new_event_complete____3____follow2: chr [1:6] NA NA "Incomplete" "Complete" ...
+

Transfer suffixes to labels:

+
+wide_data_suffixes <- wide_data |> suffix2label()

Creating a nice table

-
-wide_data |> 
-  dplyr::select(sex,hypertension, diabetes) |> 
+
+wide_data_suffixes |>
+  dplyr::select(sex, hypertension, diabetes,mrs_score____follow2) |>
   gtsummary::tbl_summary()