Sleep Diaries helper package for R โ import, compute, and score.
[!WARNING] slumbR is in early development and has not been formally tested. The API may change without notice, scoring algorithms have not yet been validated against reference implementations, and the package has not undergone peer review. Use with caution and verify outputs independently before using in any research context.
slumbR is the official R companion to the Sleep Diaries app. When a study ends, participants export their data as a JSON file. slumbR reads those files โ one per participant or a whole directory at once โ and hands you a clean, analysis-ready data frame in seconds.
It computes standard sleep variables (TIB, TST, SE, SOL, WASO) directly from the raw diary answers, re-scores all eight validated questionnaires bundled in the app (ESS, ISI, DBAS-16, MEQ, PSQI, RU-SATED, STOP-BANG, MCTQ), and assembles both long and wide study-level frames ready for downstream statistics or visualisation.
read_export() โ parse a single participant's JSON export into a structured slumbr_export objectread_study() โ batch-read a whole directory of exports into a slumbr_study with long, wide, and scores framescompute_sleep_vars() โ derive TIB, TST, SE, and clinical flags from raw morning diary answersdiary_wide() โ pivot to one row per participant ร night with m_ / e_ prefixed columnsstudy_summary() โ participant-level summary table (mean TST, SE, SOL, quality, etc.)score_questionnaire() โ re-score any of the 8 instruments from raw answers; algorithms match the app exactlyscore_all_questionnaires() โ re-score an entire questionnaire data frame in one callslumbR/
โโโ R/
โ โโโ slumbR-package.R # package-level docs and imports
โ โโโ import.R # read_export(), read_study(), compute_sleep_vars()
โ โโโ tidy.R # diary_long(), diary_wide(), study_summary()
โ โโโ questionnaires.R # score_questionnaire(), score_all_questionnaires()
โโโ inst/extdata/ # bundled example exports (3 simulated participants)
โโโ man/figures/
โ โโโ logo.svg # hex sticker
โโโ man/ # roxygen2-generated documentation
โโโ vignettes/
โ โโโ getting-started.Rmd # end-to-end worked example
โโโ tests/testthat/
โ โโโ test-questionnaires.R
โ โโโ test-import.R
โโโ .github/workflows/
โ โโโ R-CMD-check.yaml
โโโ DESCRIPTION
โโโ LICENSE
โโโ slumbR.Rproj
jsonlite, dplyr, lubridate, purrr, rlang, cli
# Install from GitHub (requires remotes)
# remotes::install_github("circadia-bio/slumbR")
install.packages("slumbR", repos = c('https://circadia-bio.r-universe.dev', 'https://cloud.r-project.org'))
library(slumbR)
# โโ Single participant โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
p <- read_export("exports/P001.json")
p$diary # long-format: one row per diary entry
p$questionnaires # questionnaire results with raw answers
# โโ Whole study โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
study <- read_study("exports/")
study$diary # long-format, all participants combined
study$wide # wide-format: one row per participant ร night
study$scores # questionnaire scores, one row per participant ร instrument
# โโ Participant-level summary โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
study_summary(study)
#> participant_id n_morning n_evening n_nights mean_tst_h mean_se_pct ...
# โโ Re-score a questionnaire โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
score_questionnaire("ess", answers = list(
ess1 = 2, ess2 = 1, ess3 = 0, ess4 = 3,
ess5 = 1, ess6 = 0, ess7 = 2, ess8 = 1
))
#> $score
#> [1] 10
#> $label
#> [1] "Excessive"
[!NOTE] The bundled dataset is entirely simulated. The three participants (Sarah Chen, James Okonkwo, Priya Mehta) and all their diary entries and questionnaire responses are artificially generated for demonstration purposes. They do not represent real people or real study data.
extdata <- system.file("extdata", package = "slumbR")
study <- read_study(extdata)
study_summary(study)
See vignette("getting-started", package = "slumbR") for a full worked example.
Morning diary entries are automatically enriched with the following derived variables when using read_export() or read_study():
| Variable | Definition | Reference threshold |
|---|---|---|
tib_min |
Time in bed (rise โ bed), minutes | โ |
tst_min |
Total sleep time (TIB โ SOL โ WASO), minutes | โฅ 420 min (7 h) |
se_pct |
Sleep efficiency (TST / TIB ร 100), % | โฅ 85% |
sol_flag |
TRUE if SOL > 30 min |
Ohayon et al. (2017) |
se_flag |
TRUE if SE < 85% |
Morin (1993) |
waso_flag |
TRUE if WASO > 30 min |
Ohayon et al. (2017) |
tst_flag |
TRUE if TST < 7 h |
Hirshkowitz et al. (2015) |
| ID | Full name | Score range | Reference |
|---|---|---|---|
ess |
Epworth Sleepiness Scale | 0โ24 | Johns (1991) |
isi |
Insomnia Severity Index | 0โ28 | Morin et al. (2011) |
dbas16 |
Dysfunctional Beliefs and Attitudes about Sleep | 0โ10 (mean) | Morin et al. (2007) |
meq |
MorningnessโEveningness Questionnaire | 16โ86 | Horne & รstberg (1976) |
psqi |
Pittsburgh Sleep Quality Index | 0โ21 | Buysse et al. (1989) |
rusated |
RU-SATED Sleep Health Scale | 0โ24 | Buysse (2014) |
stopbang |
STOP-BANG Questionnaire | 0โ8 | Chung et al. (2016) |
mctq |
Munich Chronotype Questionnaire | MSFsc + SJL | Roenneberg et al. (2003) |
Note: Some instruments require institutional permission for research use. Please verify licensing before use. The app's
Settings โ Questionnaire Creditsscreen lists the relevant permissions for each instrument.
| Package | Version | Purpose |
|---|---|---|
| jsonlite | โฅ 1.8.0 | JSON parsing |
| dplyr | โฅ 1.1.0 | Data manipulation |
| lubridate | โฅ 1.9.0 | Date/time handling |
| purrr | โฅ 1.0.0 | Functional iteration |
| rlang | โฅ 1.1.0 | Error/warning infrastructure |
| cli | โฅ 3.6.0 | Progress and messages |
| Role | Name | Affiliation |
|---|---|---|
| Author, maintainer | Lucas Franรงa | Northumbria University, Circadia Lab |
| Author | Mario Leocadio-Miguel | Northumbria University, Circadia Lab |
Released under the MIT License.
Copyright ยฉ Lucas Franรงa, Mario Leocadio-Miguel, 2025