Skip to content

circadia-bio/slumbR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›Œ slumbR

Sleep Diaries helper package for R โ€” import, compute, and score.

License: MIT R R CMD CHECK Status


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.


๐Ÿ“– What is slumbR?

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.


โœจ Features

  • ๐Ÿ“ฅ read_export() โ€” parse a single participant's JSON export into a structured slumbr_export object
  • ๐Ÿ“‚ read_study() โ€” batch-read a whole directory of exports into a slumbr_study with long, wide, and scores frames
  • ๐Ÿ“ compute_sleep_vars() โ€” derive TIB, TST, SE, and clinical flags from raw morning diary answers
  • ๐Ÿ”€ diary_wide() โ€” pivot to one row per participant ร— night with m_ / e_ prefixed columns
  • ๐Ÿ“Š study_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 exactly
  • โœ… score_all_questionnaires() โ€” re-score an entire questionnaire data frame in one call

๐Ÿ—‚๏ธ Project Structure

slumbR/
โ”œโ”€โ”€ 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

๐Ÿš€ Getting Started

Prerequisites

  • R โ‰ฅ 4.1
  • The following packages (installed automatically): jsonlite, dplyr, lubridate, purrr, rlang, cli

Installation

# Install from GitHub (requires remotes)
remotes::install_github("circadia-bio/slumbR")

Basic usage

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"

Try it with the bundled example data

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.


๐Ÿ›๏ธ Computed sleep variables

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)

๐Ÿงฎ Supported questionnaires

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 Credits screen lists the relevant permissions for each instrument.


๐Ÿ“ฆ Dependencies

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

๐Ÿ‘ฅ Authors

Role Name Affiliation
Author, maintainer Lucas Franรงa Northumbria University, Circadia Lab
Author Mario Leocadio-Miguel Northumbria University, Circadia Lab

๐Ÿค Related Tools

  • ๐ŸŒ™ SleepDiaries โ€” the cross-platform Sleep Diaries app that generates the exports slumbR reads
  • ๐Ÿ”ฌ circadia-bio โ€” the Circadia Lab GitHub organisation

๐Ÿ“„ Licence

Released under the MIT License.

Copyright ยฉ Lucas Franรงa, Mario Leocadio-Miguel, 2025

About

๐Ÿ›Œ Sleep Diaries helper package for R โ€” import, compute, and score.

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages