Token

To retrieve your NSRR token, go to https://sleepdata.org/dashboard, and sign in. This token will allow you access to any data sets you have requested access to. If you do not have access, then it will allow you to download files that are publicly available.

Set the token by adding this to your ~/.Renviron file:

NSRR_TOKEN="YOUR TOKEN GOES HERE"

The token is accessible via token = Sys.getenv("NSRR_TOKEN"). Each nsrr function also has the argument token to pass through if you do not wish to set it.

To determine if you are authenticated, you can use:

library(nsrr)
nsrr_auth()
#> $authenticated
#> [1] FALSE

Examples

NSRR data sets

Here is how you can access the NSRR datasets list:

NSRR data set files

Here we first get a list of the files in the datasets sub-directory from the shhs data set:

df = nsrr_dataset_files("shhs", path = "datasets")
head(df)
#>   dataset                         full_path    folder                file_name
#> 1    shhs                  datasets/archive datasets/                  archive
#> 2    shhs           datasets/eeg-biomarkers datasets/           eeg-biomarkers
#> 3    shhs             datasets/hrv-analysis datasets/             hrv-analysis
#> 4    shhs             datasets/CHANGELOG.md datasets/             CHANGELOG.md
#> 5    shhs           datasets/KNOWNISSUES.md datasets/           KNOWNISSUES.md
#> 6    shhs datasets/shhs1-dataset-0.21.0.csv datasets/ shhs1-dataset-0.21.0.csv
#>   is_file file_size                file_checksum_md5 archived
#> 1   FALSE         0                             <NA>    FALSE
#> 2   FALSE         0                             <NA>    FALSE
#> 3   FALSE      4096                             <NA>    FALSE
#> 4    TRUE     12747 0bd4ecbc33bf82c0e75c05225a9c7e23    FALSE
#> 5    TRUE     11421 8598129123baa60e16977dc24aa780af    FALSE
#> 6    TRUE  24265466 667bde74ab986a23fa9b9b9d02d013c2    FALSE

Downloading NSRR data set files

We can then download the CHANGELOG.md file as it’s publicly accessible.

url = nsrr_download_url("shhs", path = "datasets/CHANGELOG.md", token = "")
url # print URL
#> [1] "https://sleepdata.org/datasets/shhs/files/m/nsrr-r-v0-2-1/datasets/CHANGELOG.md"
dl = nsrr_download_file("shhs", path = "datasets/CHANGELOG.md", token = "")
dl$outfile
#> [1] "/tmp/Rtmp54SRVG/file76b87311a4a9.md"
cat(head(readLines(dl$outfile)), sep = "\n")
#> ## 0.21.0 (July 2, 2024)
#> 
#> - Exclude certain datasets (HRV 5min) from Spout deploy process
#> - Clean up metadata on some variables
#> - The CSV datasets generated from a SAS export are located here:
#>   - `\\rfawin\bwh-sleepepi-shhs\nsrr-prep\_releases\0.21.0\`

Listing All NSRR data set files

To list all the files, recursively, you would run:

but it may take some time.