Skip to content

Commit

Permalink
changed the behaviour of import.Statechanges to display helpful infos…
Browse files Browse the repository at this point in the history
… after import. Also updated interval2state to work with existing state columns
  • Loading branch information
JZauner committed Oct 18, 2023
1 parent 41b1d37 commit beb907a
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 38 deletions.
2 changes: 1 addition & 1 deletion R/Brown.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Brown2reference <- function(dataset,

#give an error or warning if the reference column is present
if(Brown.rec.colname.str %in% names(dataset) & !overwrite.Reference)
stop("A Reference column with the given (or default) name is already part of the dataset. Please remove the column or choose a different name")
stop("A Reference column with the given (or default) name is already part of the dataset. Please remove the column, choose a different name, or set `overwrite.Reference = TRUE`")
if(Brown.rec.colname.str %in% names(dataset))
warning("A Reference column with the given (or default) name is already part of the dataset. It is overwritten, because `overwrite.Reference = TRUE ` was set.")

Expand Down
23 changes: 15 additions & 8 deletions R/import_States.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ import.Statechanges <- function(filename, path = NULL,
# Rename columns using the {{ }} syntax, remove NA and group by ID, arrange
# it, recode, and set Datetimes as POSIXct
data <- dplyr::rename(data,
{{ ID.newname }} := {{ ID.colname }}) %>%
dplyr::filter(!is.na({{ ID.newname }}), !is.na({{ Datetime.column }})) %>%
{{ ID.newname }} := {{ ID.colname }},
Datetime = {{ Datetime.column }}) %>%
dplyr::filter(!is.na({{ ID.newname }}), !is.na(Datetime)) %>%
dplyr::group_by({{ ID.newname }})

data <-
Expand All @@ -169,13 +170,17 @@ import.Statechanges <- function(filename, path = NULL,
} else data %>%
dplyr::rename({{ State.newname }} := {{ State.colnames }})

if(!lubridate::is.POSIXct(data$Datetime)){
data <- data %>%
dplyr::mutate(Datetime =
lubridate::parse_date_time(Datetime,
orders = Datetime.format, tz
))
}

data <- data %>%
dplyr::mutate(Datetime =
lubridate::parse_date_time({{ Datetime.column }},
orders = Datetime.format, tz
),
{{ ID.newname }} := factor({{ ID.newname }})) %>%
dplyr::arrange({{ Datetime.column }}, .by_group = TRUE)
dplyr::mutate({{ ID.newname }} := factor({{ ID.newname }})) %>%
dplyr::arrange(Datetime, .by_group = TRUE)

# Return the Data ----------------------------------------------------------

Expand All @@ -194,5 +199,7 @@ import.Statechanges <- function(filename, path = NULL,
)
}

import.info(data, "Statechanges", tz, {{ ID.newname }})

return(data)
}
24 changes: 23 additions & 1 deletion R/interval2state.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
#' `Interval` columns. Interval data can be created, e.g., through
#' [sc2interval()].
#' @param State.colname,Interval.colname Column names of the `State` and
#' `Interval` in the `State.interval.dataset`. Expects a `symbol`.
#' `Interval` in the `State.interval.dataset`. Expects a `symbol`. `State`
#' can't be in the `dataset` yet or the function will give an error. You can
#' also set `overwrite.State = TRUE`.
#' @param ID.colname.dataset,ID.colname.interval Column names of the
#' participant's `Id` in both the `dataset` and the `State.interval.dataset`.
#' On the off-chance that there are inconsistencies, the names can be
#' different. If the datasets where imported and preprocessed with
#' [LightLogR], this just works. Both datasets need an `Id`, because the
#' states will be added based not only on the `Datetime`, but also depending
#' on the dataset.
#' @param overwrite.State If `TRUE` (defaults to `FALSE`), the function will
#' overwrite the `State.colname` column if it already exists.
#'
#' @return One of
#' * a `data.frame` object identical to `dataset` but with the state column added
Expand Down Expand Up @@ -65,6 +69,7 @@ interval2state <- function(dataset,
Interval.colname = Interval,
ID.colname.dataset = Id,
ID.colname.interval = Id,
overwrite.State = FALSE,
output.dataset = TRUE) {

# Initial Checks ----------------------------------------------------------
Expand Down Expand Up @@ -93,8 +98,25 @@ interval2state <- function(dataset,
"output.dataset must be a logical" = is.logical(output.dataset)
)

#give an error or warning if the reference column is present
if(all(
State.colname.defused %in% names(dataset),
!overwrite.State,
State.colname.defused %in% names(State.interval.dataset)))
stop("A `State` column with the given (or default) name is already part of the dataset. Please remove the column, rename it, or set `overwrite.State = TRUE`")
if(all(
State.colname.defused %in% names(dataset),
State.colname.defused %in% names(State.interval.dataset)))
warning("A `State` column with the given (or default) name is already part of the dataset. It is overwritten, because `overwrite.State = TRUE ` was set.")


# Manipulation ----------------------------------------------------------

#remove the State.colname from the dataset if it is already present
if(State.colname.defused %in% names(dataset)) {
dataset <- dataset %>% dplyr::select(- {{ State.colname }})
}

#add a marker to the dataset that tells us, what the original Timepoints were
dataset <-
dataset %>%
Expand Down
1 change: 1 addition & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ knitr::opts_chunk$set(

<!-- badges: end -->


Personalized luminous exposure data is progressively gaining importance in various sectors, including research, occupational affairs, and fitness tracking. Data are collected through a proliferating selection of wearable loggers and dosimeters, varying in size, shape, functionality, and output format. Despite or maybe because of numerous use cases, the field lacks a unified framework for collecting, validating, and analyzing the accumulated data. This issue increases the time and expertise necessary to handle such data and also compromises the FAIRness (Findability, Accessibility, Interoperability, Reusability) of the results, especially in meta-analyses.

##### Please note that LightLogR is work in progress! If you are interested in the project and want to know more, please give us a [message](mailto:[email protected])
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ dataset <- import.LYS(filename, tz = "Europe/Berlin")
#> End: 2023-06-22 23:59:48
#> Timespan: 2 days
#> Observation intervals:
#> interval.time n pct
#> 1 15 secs 10015 87.689%
#> 2 16 secs 1367 11.969%
#> 3 17 secs 23 0.201%
#> 4 18 secs 16 0.140%
#> Id interval.time n pct
#> 1 sample_data_LYS 15s 10015 87.689%
#> 2 sample_data_LYS 16s 1367 11.969%
#> 3 sample_data_LYS 17s 23 0.201%
#> 4 sample_data_LYS 18s 16 0.140%

dataset %>% select(Datetime, lux, kelvin, MEDI) %>% slice(8000:8005) %>%
flextable() %>% autofit()
Expand Down
Binary file modified man/figures/README-unnamed-chunk-4-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-5-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-6-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-7-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 14 additions & 14 deletions man/import.Dataset.Rd

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion man/interval2state.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 42 additions & 7 deletions tests/testthat/_snaps/import_States.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
State.colnames = c("sleep", "offset"), State.encoding = c("sleep", "wake"),
ID.colname = record_id, sep = ";", dec = ",")
Output
Successfully read in 14 observations from Statechanges-file
Timezone set is UTC.
The system timezone is Europe/Berlin. Please correct if necessary!
Start: 2023-08-28 23:20:00
End: 2023-09-04 07:25:00
Timespan: 6.3 days
Observation intervals:
Id interval.time n pct
1 205 34860s (~9.68 hours) 1 8%
2 205 35520s (~9.87 hours) 1 8%
3 205 35700s (~9.92 hours) 1 8%
4 205 36000s (~10 hours) 1 8%
5 205 36900s (~10.25 hours) 1 8%
6 205 37020s (~10.28 hours) 1 8%
7 205 37920s (~10.53 hours) 1 8%
8 205 45780s (~12.72 hours) 1 8%
9 205 48480s (~13.47 hours) 1 8%
10 205 49200s (~13.67 hours) 1 8%
11 205 49500s (~13.75 hours) 1 8%
12 205 50040s (~13.9 hours) 1 8%
13 205 50580s (~14.05 hours) 1 8%
# A tibble: 14 x 3
# Groups: Id [1]
Id State Datetime
Expand Down Expand Up @@ -35,15 +56,29 @@
State.colnames = "comments", Datetime.column = sleep, ID.colname = record_id,
sep = ";", dec = ",", structure = "long")
Output
Successfully read in 7 observations from Statechanges-file
Timezone set is UTC.
The system timezone is Europe/Berlin. Please correct if necessary!
Start: 2023-08-28 23:20:00
End: 2023-09-03 21:30:00
Timespan: 5.9 days
Observation intervals:
Id interval.time n pct
1 205 81300s (~22.58 hours) 1 17%
2 205 84900s (~23.58 hours) 1 17%
3 205 85500s (~23.75 hours) 1 17%
4 205 86100s (~23.92 hours) 1 17%
5 205 86400s (~1 days) 1 17%
6 205 87600s (~1.01 days) 1 17%
# A tibble: 7 x 3
# Groups: Id [1]
Id State Datetime
<fct> <chr> <dttm>
1 205 woke Up and could Not Fall asleep. went to the dini~ 2023-09-01 23:10:00
2 205 none 2023-09-02 22:55:00
3 205 no 2023-09-03 21:30:00
4 205 Slept longer than usual since my kids are on Summer~ 2023-08-28 23:20:00
5 205 no 2023-08-29 23:40:00
6 205 Kids slept in my bed 2023-08-30 23:15:00
7 205 none 2023-08-31 23:15:00
1 205 Slept longer than usual since my kids are on Summer~ 2023-08-28 23:20:00
2 205 no 2023-08-29 23:40:00
3 205 Kids slept in my bed 2023-08-30 23:15:00
4 205 none 2023-08-31 23:15:00
5 205 woke Up and could Not Fall asleep. went to the dini~ 2023-09-01 23:10:00
6 205 none 2023-09-02 22:55:00
7 205 no 2023-09-03 21:30:00

2 changes: 1 addition & 1 deletion vignettes/articles/Styling.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ Plot +
annotate("text", x=x, y = 4000, label = "- Daylight Potential",
hjust = 0, col = "#0073C2DD")
ggplot2::ggsave("Brown_et_al_2022.png", width = 7, height = 4, dpi = 600)
# ggplot2::ggsave("Brown_et_al_2022.png", width = 7, height = 4, dpi = 600)
```

Expand Down

0 comments on commit beb907a

Please sign in to comment.