| Title: | Load Data from the MeLiDos Field Study |
|---|---|
| Description: | In the MeLiDos field study, personal light exposure data were collected in 9 sites, 7 countries, and 196 participants following the Guidolin et al. (2024) <doi:10.1186/s12889-024-20206-4> protocol. Data originate from wearable devices collecting personal light exposure at the eye level, chest, and the wrist. Questionnaires were collected via 'REDCap' and contain demographic information as well as chronotype, current conditions, sleep diaries, wear logs, and many more. This package makes loading the data from the respective repositories (<https://github.com/MeLiDosProject>) into R a breeze. It further contains some quality of life functions for label handling and data from 'REDCap'. |
| Authors: | Johannes Zauner [aut, cre] (ORCID: <https://orcid.org/0000-0003-2171-4566>), Manuel Spitschan [aut] (ORCID: <https://orcid.org/0000-0002-8572-9268>), European Partnership on Metrology [fnd] (The project (22NRM05 MeLiDos) has received funding from the European Partnership on Metrology, co-financed by the European Union's Horizon Europe Research and Innovation Programme, EURAMET, and the Participating States. Views and opinions expressed are those of the authors and do not necessarily reflect those of the European Union or EURAMET.), Translational Sensory and Circadian Neuroscience Unit (MPS/TUM/TUMCREATE) [cph] (URL: www.tscnlab.org) |
| Maintainer: | Johannes Zauner <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.6 |
| Built: | 2026-05-23 07:30:15 UTC |
| Source: | https://github.com/melidosproject/melidosdata |
label attribute to a vectorAdd a label attribute to a vector
add_label(data, label)add_label(data, label)
data |
Vector-like object. |
label |
Label text. |
data with a label attribute.
add_label(1:3, "Example variable")add_label(1:3, "Example variable")
Add labels to selected data frame columns
add_labels(data, labels)add_labels(data, labels)
data |
A data frame. |
labels |
Named character vector or named list of labels. |
A tibble with column-level label attributes where names matched.
dat <- data.frame(a = 1:2, b = 3:4) labelled <- add_labels(dat, c(a = "Column A")) attr(labelled$a, "label") attr(labelled$b, "label")dat <- data.frame(a = 1:2, b = 3:4) labelled <- add_labels(dat, c(a = "Column A")) attr(labelled$a, "label") attr(labelled$b, "label")
Codebook strings to ignore, as it contains HTML formatting
codebook_strings_to_ignorecodebook_strings_to_ignore
A data frame.
Retrieves the "label" attribute from each element in a list and returns
a character vector of labels.
extract_labels(data)extract_labels(data)
data |
A list of objects. Each element is expected to have a |
A character vector containing the "label" attribute of each
element in data. If an element does not have a "label" attribute,
NA_character_ is returned for that element.
# Create example data with labels x1 <- 1:3 attr(x1, "label") <- "First variable" x2 <- 4:6 attr(x2, "label") <- "Second variable" x3 <- 7:9 # No label set for x3 data <- list(x1, x2, x3) # Extract labels extract_labels(data) # Works for data frames as well data <- data.frame (x1, x2, x3) extract_labels(data)# Create example data with labels x1 <- 1:3 attr(x1, "label") <- "First variable" x2 <- 4:6 attr(x2, "label") <- "Second variable" x3 <- 7:9 # No label set for x3 data <- list(x1, x2, x3) # Extract labels extract_labels(data) # Works for data frames as well data <- data.frame (x1, x2, x3) extract_labels(data)
flatten_data() combines the named list returned by load_data() into one
tibble and keeps site provenance in a site column.
flatten_data(melidos_data, tz = "UTC", label_from = 1)flatten_data(melidos_data, tz = "UTC", label_from = 1)
melidos_data |
A list returned by |
tz |
Time zone to enforce for all |
label_from |
indice of the dataset that is used to apply labels to the output. |
If date-time columns are present (POSIXct), their timezone is overwritten
using lubridate::force_tz().
A tibble with all rows stacked and a site column.
example_multi_site <- structure( list( TUM = data.frame(id = 1, bedtime = as.POSIXct("2024-01-01 22:00:00", tz = "UTC")), UCR = data.frame(id = 2, bedtime = as.POSIXct("2024-01-02 22:30:00", tz = "UTC")) ), class = c("melidos_data", "list") ) flatten_data(example_multi_site, tz = "Europe/Berlin")example_multi_site <- structure( list( TUM = data.frame(id = 1, bedtime = as.POSIXct("2024-01-01 22:00:00", tz = "UTC")), UCR = data.frame(id = 2, bedtime = as.POSIXct("2024-01-02 22:30:00", tz = "UTC")) ), class = c("melidos_data", "list") ) flatten_data(example_multi_site, tz = "Europe/Berlin")
load_data() is the main entry point of the package. It downloads one
modality from one or more MeLiDos sites and returns either a single data
frame (one site) or a named list with class "melidos_data" (multiple sites).
load_data( modality = c("light_glasses", "light_chest", "light_wrist", "light_glasses_1minute", "light_chest_1minute", "light_wrist_1minute", "acceptability", "ase", "chronotype", "demographics", "evaluation", "health", "leba", "trial_times", "vlsq8", "currentconditions", "exercisediary", "experiencelog", "lightexposurediary", "sleepdiaries", "wearlog", "wellbeingdiary"), site = c("all", "BAUA", "FUSPCEU", "IZTECH", "KNUST", "MPI", "RISE", "THUAS", "TUM", "UCR") )load_data( modality = c("light_glasses", "light_chest", "light_wrist", "light_glasses_1minute", "light_chest_1minute", "light_wrist_1minute", "acceptability", "ase", "chronotype", "demographics", "evaluation", "health", "leba", "trial_times", "vlsq8", "currentconditions", "exercisediary", "experiencelog", "lightexposurediary", "sleepdiaries", "wearlog", "wellbeingdiary"), site = c("all", "BAUA", "FUSPCEU", "IZTECH", "KNUST", "MPI", "RISE", "THUAS", "TUM", "UCR") )
modality |
Dataset to load. |
site |
Site(s) to load. Use |
Use flatten_data() to stack multi-site results into one tibble with a
site column.
See the README of the package for a description of sites and modalities.
A data frame when one site is selected, or a melidos_data list for multiple
sites.
https://github.com/MeLiDosProject
# load one questionnaire modality for two sites sleep_all <- load_data("sleepdiaries", site = c("TUM", "RISE")) # flatten to a single tibble with a site column sleep_flat <- flatten_data(sleep_all, tz = "UTC") head(sleep_flat) # load one site only (returns a data frame) sleep_tum <- load_data("sleepdiaries", site = "TUM")# load one questionnaire modality for two sites sleep_all <- load_data("sleepdiaries", site = c("TUM", "RISE")) # flatten to a single tibble with a site column sleep_flat <- flatten_data(sleep_all, tz = "UTC") head(sleep_flat) # load one site only (returns a data frame) sleep_tum <- load_data("sleepdiaries", site = "TUM")
Named character vector of city names keyed by study site abbreviation.
melidos_citiesmelidos_cities
A named character vector.
MeLiDos study metadata.
Named character vector of hex colors keyed by study site abbreviation.
melidos_colorsmelidos_colors
A named character vector.
MeLiDos study metadata.
Named list of latitude/longitude numeric pairs keyed by study site abbreviation.
melidos_coordinatesmelidos_coordinates
A named list.
MeLiDos study metadata.
Named character vector of country names keyed by study site abbreviation.
melidos_countriesmelidos_countries
A named character vector.
MeLiDos study metadata.
Named character vector of Olson time zone identifiers keyed by study site abbreviation.
melidos_tzsmelidos_tzs
A named character vector.
MeLiDos study metadata.
Converts times before cutoff to the next day before applying .fun, which
helps summarise nighttime values spanning midnight (for example, median sleep
time).
nighttime_switch( datetime, .fun = stats::median, cutoff = 12 * 60 * 60, hms = TRUE )nighttime_switch( datetime, .fun = stats::median, cutoff = 12 * 60 * 60, hms = TRUE )
datetime |
A date-time vector coercible to |
.fun |
Summary function applied after the date shift. |
cutoff |
Cutoff in seconds since midnight. Values below cutoff are treated as belonging to the next day. |
hms |
Logical; if |
A summarised time as hms (default) or date-time.
x <- as.POSIXct(c("2024-01-01 23:00:00", "2024-01-02 01:00:00"), tz = "UTC") nighttime_switch(x)x <- as.POSIXct(c("2024-01-01 23:00:00", "2024-01-02 01:00:00"), tz = "UTC") nighttime_switch(x)
Replaces a column with a logical pass/fail value based on membership in
condition, sets a label attribute, and moves the column to the end.
REDCap_attention_check( data, check.column, condition, label = "Attention check successful" )REDCap_attention_check( data, check.column, condition, label = "Attention check successful" )
data |
A data frame. |
check.column |
Unquoted column to evaluate. |
condition |
Vector of accepted responses. |
label |
Label to attach to the resulting logical column. |
data with an updated attention check column.
dat <- data.frame(attention = c("A", "B", "A")) REDCap_attention_check(dat, attention, condition = "A")dat <- data.frame(attention = c("A", "B", "A")) REDCap_attention_check(dat, attention, condition = "A")
Cleans HTML formatting from codebook labels and optionally filters the codebook to selected REDCap forms.
REDCap_codebook_prepare( codebook, strings_to_ignore = codebook_strings_to_ignore, form.filter = NULL, field.label = `Field Label`, form.col = `Form Name` )REDCap_codebook_prepare( codebook, strings_to_ignore = codebook_strings_to_ignore, form.filter = NULL, field.label = `Field Label`, form.col = `Form Name` )
codebook |
A REDCap data dictionary as a data frame. |
strings_to_ignore |
A regular expression used in
|
form.filter |
Optional character vector of REDCap form names to keep. |
field.label |
Unquoted column containing question labels. |
form.col |
Unquoted column containing the form name (for filtering) |
A cleaned codebook tibble/data frame.
codebook_path <- system.file("ext", "DataDictionary_sleepdiary.csv", package = "melidosData" ) codebook <- utils::read.csv(codebook_path, check.names = FALSE) cleaned <- REDCap_codebook_prepare(codebook) cleaned$`Field Label` #compate the original one codebook$`Field Label`codebook_path <- system.file("ext", "DataDictionary_sleepdiary.csv", package = "melidosData" ) codebook <- utils::read.csv(codebook_path, check.names = FALSE) cleaned <- REDCap_codebook_prepare(codebook) cleaned$`Field Label` #compate the original one codebook$`Field Label`
Sets the label attribute on matching columns in data.
REDCap_col_labels( data, lookup, var_col = `Variable / Field Name`, label_col = `Field Label`, warn = TRUE )REDCap_col_labels( data, lookup, var_col = `Variable / Field Name`, label_col = `Field Label`, warn = TRUE )
data |
A data frame. |
lookup |
A data frame with variable names and labels. |
var_col |
Unquoted column in |
label_col |
Unquoted column in |
warn |
Logical; warn when labels are provided for variables not present
in |
data with label attributes added.
dict_path <- system.file("ext", "DataDictionary_chronotype.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) dict <- REDCap_codebook_prepare(dict, form.filter = "mctq") labelled <- REDCap_col_labels(REDCap_example_chronotype, dict) attr(labelled[[5]], "label")dict_path <- system.file("ext", "DataDictionary_chronotype.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) dict <- REDCap_codebook_prepare(dict, form.filter = "mctq") labelled <- REDCap_col_labels(REDCap_example_chronotype, dict) attr(labelled[[5]], "label")
Uses REDCap codebook metadata to infer expected classes and compares these to
classes in data.
REDCap_coltype_check( codebook, indicator_POSIXct = "datetime_dmy", indicator_date = "Date", indicator_time = "time", indicator_logical = "yesno", indicator_numeric.val_col = c("number", "integer"), indicator_numeric.type_col = c("radio", "dropdown"), label_col = `Field Label`, name_col = `Variable / Field Name`, type_col = `Field Type`, val_col = `Text Validation Type OR Show Slider Number`, data )REDCap_coltype_check( codebook, indicator_POSIXct = "datetime_dmy", indicator_date = "Date", indicator_time = "time", indicator_logical = "yesno", indicator_numeric.val_col = c("number", "integer"), indicator_numeric.type_col = c("radio", "dropdown"), label_col = `Field Label`, name_col = `Variable / Field Name`, type_col = `Field Type`, val_col = `Text Validation Type OR Show Slider Number`, data )
codebook |
REDCap data dictionary. |
indicator_POSIXct |
Indicator in |
indicator_date |
Pattern used in labels to identify date variables. |
indicator_time |
Indicator in |
indicator_logical |
Indicator in |
indicator_numeric.val_col |
Indicators in |
indicator_numeric.type_col |
Indicators in |
label_col |
Unquoted codebook label column. |
name_col |
Unquoted codebook variable-name column. |
type_col |
Unquoted codebook field-type column. |
val_col |
Unquoted codebook validation/type-hint column. |
data |
Data frame to validate. |
A list with ok, summary, and per-column details.
library(gt) dict_path <- system.file("ext", "DataDictionary_sleepdiary.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) coltype_check <- REDCap_coltype_check(dict, data = REDCap_example_sleep) coltype_check$ok coltype_check$summary coltype_check$details |> gt() dict_path <- system.file("ext", "DataDictionary_chronotype.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) dict <- REDCap_codebook_prepare(dict, form.filter = "mctq") coltype_check <- REDCap_coltype_check(dict, data = REDCap_example_chronotype) coltype_check$ok coltype_check$summary coltype_check$detailslibrary(gt) dict_path <- system.file("ext", "DataDictionary_sleepdiary.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) coltype_check <- REDCap_coltype_check(dict, data = REDCap_example_sleep) coltype_check$ok coltype_check$summary coltype_check$details |> gt() dict_path <- system.file("ext", "DataDictionary_chronotype.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) dict <- REDCap_codebook_prepare(dict, form.filter = "mctq") coltype_check <- REDCap_coltype_check(dict, data = REDCap_example_chronotype) coltype_check$ok coltype_check$summary coltype_check$details
Example export containing chronotype questionnaire responses.
REDCap_example_chronotypeREDCap_example_chronotype
A data frame.
inst/ext/example_chronotype.csv.
Example export containing sleep diary responses.
REDCap_example_sleepREDCap_example_sleep
A data frame.
inst/ext/example_sleepdiary.csv.
Applies factor levels and labels using REDCap dictionary definitions for
radio and checkbox fields (configurable via radio_value).
REDCap_factors( data, lookup, var_col = `Variable / Field Name`, type_col = `Field Type`, levels_col = `Choices, Calculations, OR Slider Labels`, radio_value = c("checkbox", "radio"), warn = TRUE )REDCap_factors( data, lookup, var_col = `Variable / Field Name`, type_col = `Field Type`, levels_col = `Choices, Calculations, OR Slider Labels`, radio_value = c("checkbox", "radio"), warn = TRUE )
data |
Data frame containing REDCap records. |
lookup |
Data frame containing variable metadata. |
var_col |
Unquoted column in |
type_col |
Unquoted column in |
levels_col |
Unquoted column in |
radio_value |
Character vector of field types to convert. |
warn |
Logical; warn when variables are in |
data with selected columns converted to factors.
dict_path <- system.file("ext", "DataDictionary_chronotype.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) dict <- REDCap_codebook_prepare(dict, form.filter = "mctq") chronotype_with_factors <- REDCap_factors( data = REDCap_example_chronotype, lookup = dict ) chronotype_with_factors$mctq_work_travel #original: REDCap_example_chronotype$mctq_work_traveldict_path <- system.file("ext", "DataDictionary_chronotype.csv", package = "melidosData" ) dict <- utils::read.csv(dict_path, check.names = FALSE) dict <- REDCap_codebook_prepare(dict, form.filter = "mctq") chronotype_with_factors <- REDCap_factors( data = REDCap_example_chronotype, lookup = dict ) chronotype_with_factors$mctq_work_travel #original: REDCap_example_chronotype$mctq_work_travel