---
title: "REDCapCAST"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{REDCapCAST}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(REDCapCAST)
```
This vignette covers the basics to get you started with the two basic features of REDCapCAST:
- Casting REDCap metadata to create a new REDCap database or extend an existing with a new instrument
- Reading REDCap data in a convenient and focused way, by only getting the data you need, while preserving as much metadata as possible.
## Casting meta data
The easiest way is to use the `shiny_cast()`. You can access a [hosted version here](https://agdamsbo.shinyapps.io/redcapcast/) or launch it locally like this:
```{r eval=FALSE}
shiny_cast()
```
## Reading data from REDCap
To get you started, the easiest way possible, you can use the `easy_redcap()` function (example below).
You will need an API-key for your REDCap server, the uri/URL/address for the API connection (usually the adress used for accessing your institutions REDCap servar, with an appended "/api/").
This function includes a few convenience features to ease your further work.
If your project uses repeating instruments possible as a longitudinal project, you can choose to widen the data. If not, the result will be a list of each instrument you have chosen to extract data from. Make sure 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.
```{r eval=FALSE}
easy_redcap(
uri = "YOUR URI",
project.name = "MY_PROJECT",
widen.data = TRUE,
fields = c("record_id", "OTHER FIELDS")
)
```
## Splitting the dataset
The `easy_redcap()` function does a few things under the hood. Below are a few examples to show how the nicely formatted output is achieved.
A sample dataset and Data Dictionary/metadata is provided for this demonstration:
```{r}
redcapcast_data |> gt::gt()
```
```{r}
redcapcast_meta |> gt::gt()
```
To save the metadata as labels in the dataset, we can save field labels and the choices from radio buttons and dropdown features:
```{r}
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.
```{r}
list <-
REDCap_split(
records = labelled_data,
metadata = redcapcast_meta,
forms = "all"
) |>
# Next steps cleans up and removes generic columns
sanitize_split()
str(list)
```
The `easy_redcap()` will then (optionally) continue to 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):
```{r}
wide_data <- redcap_wider(list,
event.glue = "{.value}____{redcap_event_name}",
inst.glue = "{.value}____{redcap_repeat_instance}"
)
wide_data |> str()
```
Transfer suffixes to labels:
```{r}
wide_data_suffixes <- wide_data |> suffix2label()
```
## Creating a nice table
```{r}
wide_data_suffixes |>
dplyr::select(sex, hypertension, diabetes,mrs_score____follow2) |>
gtsummary::tbl_summary()
```