| Title: | Sleep Diaries Helper Package for R |
|---|---|
| Description: | A companion package for the Sleep Diaries app (https://sleepdiaries.circadia-lab.uk). Provides tools to import participant JSON exports, compute standard sleep variables (sleep onset latency, total sleep time, sleep efficiency, WASO, etc.), re-score validated sleep questionnaires (ESS, ISI, DBAS-16, MEQ, PSQI, RU-SATED, STOP-BANG, MCTQ), and assemble tidy study-level data frames ready for downstream analysis. |
| Authors: | Lucas França [aut, cre] (ORCID: <https://orcid.org/0000-0003-0853-1319>), Mario Leocadio-Miguel [aut] (ORCID: <https://orcid.org/0000-0002-7248-3529>) |
| Maintainer: | Lucas França <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-07-01 23:56:25 UTC |
| Source: | https://github.com/circadia-bio/slumbR |
List available questionnaire instruments
available_instruments()available_instruments()
A character vector of instrument IDs supported by
score_questionnaire().
Given a morning-entry data frame row (as produced internally by
read_export()), returns additional computed columns:
compute_sleep_vars(df)compute_sleep_vars(df)
df |
A data frame with morning-entry columns (output of |
| Variable | Definition |
tib_min |
Time in bed (rise_time − bed_time), minutes |
tst_min |
Total sleep time (TIB − SOL − WASO), minutes |
se_pct |
Sleep efficiency (TST / TIB × 100), % |
sol_flag |
TRUE if SOL > 30 min (clinically significant) |
se_flag |
TRUE if SE < 85% (below healthy threshold) |
waso_flag |
TRUE if WASO > 30 min (clinically significant) |
tst_flag |
TRUE if TST < 7 h (below recommended)
|
Clock-time fields (bed_time, rise_time) are decimal hours. Overnight wrap is handled by adding 24 when rise_time ≤ bed_time.
The same data frame with additional computed columns appended.
A convenience accessor that returns the diary data frame from a
slumbr_study or slumbr_export object. For a plain data frame it is
returned unchanged.
diary_long(x) ## S3 method for class 'slumbr_study' diary_long(x) ## S3 method for class 'slumbr_export' diary_long(x) ## S3 method for class 'data.frame' diary_long(x)diary_long(x) ## S3 method for class 'slumbr_study' diary_long(x) ## S3 method for class 'slumbr_export' diary_long(x) ## S3 method for class 'data.frame' diary_long(x)
x |
A |
The long-format frame has one row per diary entry (morning or evening).
Morning rows carry sleep timing and quality variables; evening rows carry
daytime behaviour variables. Most analyses will want diary_wide() to
merge them into one row per night.
A data frame with one row per diary entry.
Merges matching morning and evening entries for the same participant and
date into a single row, prefixing morning columns with m_ and evening
columns with e_.
diary_wide(x)diary_wide(x)
x |
A |
A data frame with one row per participant x date.
## Not run: study <- read_study("exports/") wide <- diary_wide(study) head(wide) ## End(Not run)## Not run: study <- read_study("exports/") wide <- diary_wide(study) head(wide) ## End(Not run)
Parses one participant's JSON export file as produced by the Sleep Diaries app and returns a structured list with raw entries, parsed diary data, and questionnaire results.
read_export(path, participant_id = NULL, compute_vars = TRUE)read_export(path, participant_id = NULL, compute_vars = TRUE)
path |
Path to a |
participant_id |
Optional character string used as the |
compute_vars |
Logical. If |
A named list with class "slumbr_export":
participant_idCharacter. The resolved participant identifier.
participant_nameCharacter. Raw name from JSON.
research_codeCharacter or NA. Research code from JSON.
exported_atPOSIXct. Export timestamp.
diaryData frame. One row per diary entry (morning or evening), with all parsed answer columns.
questionnairesData frame. One row per completed questionnaire, with raw answers in a list-column and computed scores.
rawList. The full parsed JSON, for debugging.
read_study(), diary_long(), diary_wide()
## Not run: p <- read_export("exports/P001.json") p$diary p$questionnaires ## End(Not run)## Not run: p <- read_export("exports/P001.json") p$diary p$questionnaires ## End(Not run)
Reads all .json files in a directory (one per participant) and assembles
them into a slumbr_study object containing combined long and wide data
frames plus questionnaire scores.
read_study( dir, pattern = "\\.json$", participant_id_from = c("code", "name", "filename"), compute_vars = TRUE, verbose = TRUE )read_study( dir, pattern = "\\.json$", participant_id_from = c("code", "name", "filename"), compute_vars = TRUE, verbose = TRUE )
dir |
Path to a directory containing |
pattern |
Regex pattern to match filenames. Default |
participant_id_from |
One of |
compute_vars |
Logical. Passed to |
verbose |
Logical. Print a progress summary. Default |
A named list with class "slumbr_study":
diaryLong-format data frame. All participants, all entries.
wideWide-format data frame. One row per participant-night, morning and evening columns merged side by side.
scoresData frame. Questionnaire scores, one row per participant × instrument.
participantsCharacter vector of resolved participant IDs.
exportsNamed list of individual slumbr_export objects.
read_export(), diary_long(), diary_wide()
## Not run: study <- read_study("exports/") study$diary study$wide study$scores ## End(Not run)## Not run: study <- read_study("exports/") study$diary study$wide study$scores ## End(Not run)
Takes the questionnaire data frame (as returned in $questionnaires from
read_export() or read_study()) and adds re-computed score_r and
label_r columns using the R scoring functions. Useful for validation or
when raw answers were imported without scores.
score_all_questionnaires(x)score_all_questionnaires(x)
x |
A |
The questionnaire data frame with two additional columns:
score_r (re-computed score) and label_r (interpretation label).
## Not run: study <- read_study("exports/") score_all_questionnaires(study) ## End(Not run)## Not run: study <- read_study("exports/") score_all_questionnaires(study) ## End(Not run)
Re-scores any of the eight validated instruments included in the Sleep Diaries app. Scoring algorithms exactly match the app's JavaScript implementation.
score_questionnaire(instrument, answers)score_questionnaire(instrument, answers)
instrument |
Character. One of |
answers |
Named list of answers keyed by item ID (e.g. |
A named list with at minimum:
scoreNumeric. The computed total or mean score (for MCTQ: a
list with msf_sc and sjl).
labelCharacter. The clinical interpretation label.
referenceCharacter. Citation for the instrument.
PSQI additionally returns components (a named vector of the 7 component
scores). MCTQ additionally returns msf_sc and sjl as top-level fields.
# ESS score_questionnaire("ess", answers = list( ess1 = 2, ess2 = 1, ess3 = 0, ess4 = 3, ess5 = 1, ess6 = 0, ess7 = 2, ess8 = 1 )) # MCTQ (free-day mid-sleep and social jetlag) score_questionnaire("mctq", answers = list( mctq_wd = 5, mctq_bt_w = list(hour = 23, minute = 0), mctq_sl_w = 15, mctq_wt_w = list(hour = 7, minute = 0), mctq_bt_f = list(hour = 0, minute = 30), mctq_sl_f = 20, mctq_wt_f = list(hour = 9, minute = 0) ))# ESS score_questionnaire("ess", answers = list( ess1 = 2, ess2 = 1, ess3 = 0, ess4 = 3, ess5 = 1, ess6 = 0, ess7 = 2, ess8 = 1 )) # MCTQ (free-day mid-sleep and social jetlag) score_questionnaire("mctq", answers = list( mctq_wd = 5, mctq_bt_w = list(hour = 23, minute = 0), mctq_sl_w = 15, mctq_wt_w = list(hour = 7, minute = 0), mctq_bt_f = list(hour = 0, minute = 30), mctq_sl_f = 20, mctq_wt_f = list(hour = 9, minute = 0) ))
Summarise a study's diary data at the participant level
study_summary(x, na.rm = TRUE)study_summary(x, na.rm = TRUE)
x |
A |
na.rm |
Logical. Remove NAs before computing means. Default |
A data frame with one row per participant and columns:
participant_id, n_morning, n_evening, n_nights,
mean_tst_h, mean_se_pct, mean_sol_min, mean_waso_min,
mean_quality, mean_restedness, pct_early_waking.
## Not run: study <- read_study("exports/") study_summary(study) ## End(Not run)## Not run: study <- read_study("exports/") study_summary(study) ## End(Not run)