knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

library(tidyverse)

credentials <- readRDS("tests/testthat/testing_credentials.Rds")

Castor API R Package

The intent of this package is to provide convenient access to study data in Castor via R.

Package Structure

Access to data in the API comes in three forms:

  1. Access to individual items (getStudyDataPoint, getStudy, getRecord, etc.)
  2. Access to pages of items in a list (getStudyDataPoints, getStudies, getRecords, etc.)
  3. Processed into a data frame for straightforward analysis (getStudyData)

Installation

# install.packages("remotes")
remotes::install_github("castoredc/castoRedc")

Missing data

In the package, user missing data is handled in the following way. For date variables, they are represented by 31-12-29XX, where XX (95-99) indicates the type of missing data. For numeric variables, they are represented by the values -99 to -95, where 95 to 99 represent the type of missing data in Castor. For non-numeric variables, they are represented by ##_USER_MISSING_XX## where XX (95-99) indicates the type of missing data.

Testing

If you want to test the package, for example when developing, please fill in create_testing_credentials.R.
You should supply an example for each of the study, report, etc. Please make sure that the record has a value in the field. And that the report step is a child of the report.

Usage

See https://helpdesk.castoredc.com/article/124-application-programming-interface-api about generating your credentials.

Note: It is recommended that you read https://cran.r-project.org/web/packages/httr/vignettes/secrets.html about managing your credentials.

library(castoRedc)

castor_api <- CastorData$new(key = credentials$client_id, 
                             secret = credentials$client_secret, 
                             base_url = "https://data.castoredc.com")
studies <- castor_api$getStudies()
knitr::kable(studies)
(example_study_id <- studies[["study_id"]][1])
study <- castor_api$getStudyData(example_study_id)
study[["Study"]] <- study[["Study"]][,1:10]
study[["Repeating data"]] <-
  lapply(study[["Repeating data"]], function(x)
    x %>%
      rowwise() %>%
      mutate(missing = sum(is.na(
        c_across(everything())
      ))) %>% ungroup() %>% slice_min(missing, n = 5, with_ties = F) %>% select(!missing))
study[["Surveys"]] <-
  lapply(study[["Surveys"]], function(x)
    x %>%
      rowwise() %>%
      mutate(missing = sum(is.na(
        c_across(everything())
      ))) %>% ungroup() %>% slice_min(missing, n = 5, with_ties = F) %>% select(!missing))

knitr::kable(study)
fields <- castor_api$getFields(example_study_id)
knitr::kable(head(subset(fields, select = -field_summary_template), 3))
forms <- castor_api$getForms(example_study_id)
knitr::kable(forms)
visits <- castor_api$getVisits(example_study_id)
knitr::kable(visits)


castoredc/castoRedc documentation built on Aug. 4, 2024, 5:24 a.m.