---
title: Results of the measurement of physical behavior
output: 
  html_document:
    theme: cosmo
    toc: yes
    toc_depth: 3
    toc_float: yes
    number_sections: yes
    df_print: paged
toc-title: "Summary"
params:
  assessor_title: NA
  assessor_name: NA
  assessor_surname: NA
  patient_title: NA
  patient_name: NA
  patient_surname: NA
  sex: NA
  age: NA
  weight: NA
  epoch: NA
  start_date: NA
  end_date: NA
  device: NA
  position: NA
  side: NA
  sampling_rate: NA
  filter: NA
  start_day_analysis: NA
  end_day_analysis: NA
  axis_weartime: NA
  frame_size: NA
  allowanceFrame_size: NA
  streamFrame_size: NA
  equation_mets: NA
  bmr_kcal_d: NA
  axis_sed: NA
  axis_mvpa: NA
  sed_cutpoint: NA
  mpa_cutpoint: NA
  vpa_cutpoint: NA
  minimum_wear_time_for_analysis: NA
  results_by_day: NA
  results_summary_means: NA
  results_summary_medians: NA
  mvpa_lines: NA
  sed_lines: NA
  ratio_lines: NA
  df_with_computed_metrics: NA
  graph_int_distri_bands: NA
  graph_int_distri_models: NA
  mean_breaks_sed: NA
  p_breaks_sed: NA
  p_alpha_sed: NA 
  p_MBD_sed: NA
  p_UBD_sed: NA 
  p_gini_sed: NA
  mean_breaks_pa: NA
  p_breaks_pa: NA 
  p_alpha_pa: NA
  p_MBD_pa: NA
  p_UBD_pa: NA
  p_gini_pa: NA
  rendered_by_shiny: FALSE
  geometry: "left=2cm, right=2cm, top=1cm, bottom=1cm"

---

```{css, echo=FALSE}
.title {
  font-weight: bold;
}

h1 {
  font-size: 25px;
  font-weight: bold;
}
h2 {
  font-size: 23px;
  font-weight: bold;
}
h3 {
  font-size: 21px;
}
p.caption {
  color: #777;
  margin-top: 10px;
}
p code {
  white-space: inherit;
}
pre {
  word-break: normal;
  word-wrap: normal;
}
pre code {
  white-space: inherit;
}
```

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning=FALSE, echo = FALSE)
library(magrittr)
library(ggplot2)
library(patchwork)

df1_means <- params$results_summary_means
df1_medians <- params$results_summary_medians
df2 <- params$results_by_day
axis_mvpa_graph <- ifelse(params$axis_mvpa == "vertical axis", "axis1", 
              ifelse(params$axis_mvpa == "vector magnitude", "vm",
                     "na"))

height_factor1 = nlevels(as.factor(params$df_with_computed_metrics$date)) * 0.75
height_factor1 <- ifelse(height_factor1 <= 6, nlevels(as.factor(params$df_with_computed_metrics$date)) * 0.9, height_factor1)
height_factor2 = params$results_summary_means$valid_days * 0.75
height_factor2 <- ifelse(height_factor2 <= 6, nlevels(as.factor(params$df_with_computed_metrics$date)) * 0.9, height_factor2)
height_factor3 = nlevels(as.factor(params$df_with_computed_metrics$date)) * 1.5

options("scipen"=100, "digits"=4)
```


# Information
<hr>

**Patient:** `r params$patient_surname` `r params$patient_name`  | **Date:**  `r Sys.Date()` | **Assessor:**  `r params$assessor_surname` `r params$assessor_name`

**Age:** `r params$age` yr | **Sex:**  `r params$sex` | **Weight:** `r params$weight` kg | **Measurement period:** `r format(params$start_date, "%Y-%m-%d %H:%M:%S")` to `r format(params$end_date, "%Y-%m-%d %H:%M:%S")` | **Estimated Basal Metabolic Rate (BMR):** `r format(round(params$bmr_kcal_d, 2), nsmall = 2)` kcal/day (Estimation based on Henry equations [2005, doi: 10.1079/PHN2005801])

**Device:** `r params$device` | **Position:** `r params$position` | **Side:** `r params$side` | **Sampling rate:** `r params$sampling_rate` Hz | **Filter:** `r params$filter`

**Epoch:** `r params$epoch` s | **Nonwear time:** Based on `r params$axis_weartime`, interval of `r params$frame_size` min with zero count for nonwear time detection, interval of `r params$allowanceFrame_size` min with nonzero counts allowed during a nonwear period, interval of `r params$streamFrame_size` min with zero count around detected activity to confirm nonwear time | **MET equation:** `r params$equation_mets` | **Axis used for PA intensity categorization:** `r params$axis_mvpa` | **Cut-points:** <`r params$sed_cutpoint` counts/min for SED, $\geqslant$ `r params$mpa_cutpoint` counts/min for MPA, $\geqslant$ `r params$vpa_cutpoint` counts/min for VPA

**Period of the day considered to count wear time:** from `r params$start_day_analysis` to `r params$end_day_analysis`

**Minimum number of hours with wear time to validate a day:** `r params$minimum_wear_time_for_analysis` hours

**Abbreviations:** SED = sedentary, LPA =  light physical activity, MPA = moderate physical activity, VPA = vigorous physical activity, MVPA = moderate-to-vigorous physical activity, PAL = physical activity level


# Results
<hr>

## Activity volume  metrics

### Temporal distribution of activity intensity
```{r,  fig.height = height_factor1, fig.width=13}
plot_data_with_intensity(
  data = params$df_with_computed_metrics, 
  metric = axis_mvpa_graph,
  valid_wear_time_start = params$start_day_analysis,
  valid_wear_time_end = params$end_day_analysis
  )
if (params$rendered_by_shiny)
  shiny::setProgress(0.25)  # set progress to 25%
```

### Results by day: Tabular view
```{r}
reactable::reactable(params$results_by_day %>% dplyr::select(date:total_steps) %>%
                       dplyr::rename(
                        Date = date,
                        "Wear time (min)"                =  wear_time,
                        "Axis 1 total counts"            =  total_counts_axis1,
                        "VM total counts"                =  total_counts_vm,
                        "Axis 1 mean (counts/min)"       =  axis1_per_min,
                        "VM mean (counts/min)"           =  vm_per_min,
                        "SED time (min)"                 =  minutes_SED, 
                        "LPA time (min)"                 =  minutes_LPA,
                        "MPA time (min)"                 =  minutes_MPA,
                        "VPA time (min)"                 =  minutes_VPA,
                        "MVPA time (min)"                =  minutes_MVPA,
                        "SED wear time proportion (%)"   =  percent_SED,
                        "LPA wear time proportion (%)"   =  percent_LPA,
                        "MPA wear time proportion (%)"   =  percent_MPA,
                        "VPA wear time proportion (%)"   =  percent_VPA,
                        "MVPA wear time proportion (%)"  =  percent_MVPA,
                        "Ratio MVPA / SED"               =  ratio_mvpa_sed,
                        "Total MVPA MET-hr"              =  mets_hours_mvpa, 
                        "Total kcal"                     =  total_kcal,
                        "PAL"                            =  pal,
                        "Total steps"                    =  total_steps
                       ),  
                striped = TRUE,
                list(
                     "Date" = reactable::colDef(
                       style = list(position = "sticky", left = 0, background = "#CCCCCC", zIndex = 1),
                       headerStyle = list(position = "sticky", left = 0,  background = "#fff", zIndex = 1)
                     ),
                     "Wear time (min)"               = reactable::colDef(minWidth = 140),
                     "Axis 1 total counts"           = reactable::colDef(minWidth = 160),
                     "VM total counts"               = reactable::colDef(minWidth = 160),
                     "Axis 1 mean (counts/min)"      = reactable::colDef(minWidth = 160),
                     "VM mean (counts/min)"          = reactable::colDef(minWidth = 160),
                     "SED time (min)"                 = reactable::colDef(minWidth = 160),
                     "LPA time (min)"                = reactable::colDef(minWidth = 160),
                     "MPA time (min)"                = reactable::colDef(minWidth = 160),
                     "VPA time (min)"                = reactable::colDef(minWidth = 160),
                     "MVPA time (min)"               = reactable::colDef(minWidth = 160),
                     "SED wear time proportion (%)"  = reactable::colDef(minWidth = 160),
                     "LPA wear time proportion (%)"  = reactable::colDef(minWidth = 160),
                     "MPA wear time proportion (%)"  = reactable::colDef(minWidth = 160),
                     "VPA wear time proportion (%)"  = reactable::colDef(minWidth = 160),
                     "MVPA wear time proportion (%)" = reactable::colDef(minWidth = 160),
                     "Ratio MVPA / SED"              = reactable::colDef(minWidth = 160),
                     "Total MVPA MET-hr"             = reactable::colDef(minWidth = 160),
                     "Total kcal"                    = reactable::colDef(minWidth = 160),
                     "PAL"                           = reactable::colDef(minWidth = 160),
                     "Total steps"                   = reactable::colDef(minWidth = 160)
                     )
                )
```

### Results by day: Graphical view
```{r, fig.height=10, fig.width = 17, warning=FALSE, message=FALSE}
create_fig_res_by_day(
  df2, 
  params$minimum_wear_time_for_analysis, 
  params$start_day_analysis, 
  params$end_day_analysis, 
  language = "en",
  metrics = "volume"
  ) + theme(plot.margin = margin(1, 1, 1, 1, "cm"))
```

### Means | Medians computed using valid days
```{r, out.width='100%'}
reactable::reactable(
  tibble::tibble(
          Metric = c("Number of valid days",
                     "Wear time (min)",              
                     "Axis 1 total counts",          
                     "VM total counts",              
                     "Axis 1 mean (counts/min)",     
                     "VM mean (counts/min)",         
                     "SED time (min)",               
                     "LPA time (min)",               
                     "MPA time (min)",               
                     "VPA time (min)",               
                     "MVPA time (min)",              
                     "SED wear time proportion (%)", 
                     "LPA wear time proportion (%)", 
                     "MPA wear time proportion (%)", 
                     "VPA wear time proportion (%)", 
                     "MVPA wear time proportion (%)",
                     "Ratio MVPA / SED",             
                     "Total MVPA MET-hr",            
                     "Total kcal",                   
                     "PAL",                          
                     "Total steps"                  
          ),
          
          "Daily mean | median" = c(
            
          # Number of valid days
            paste0(df1_means[["valid_days"]]),
            
          # Wear time
            paste0(format(round(df1_means[["wear_time"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_means[["wear_time"]]), ") | ",
                   format(round(df1_medians[["wear_time"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_medians[["wear_time"]]), ")"),  
            
          # Total counts Axis 1
            paste0(format(round(df1_means[["total_counts_axis1"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["total_counts_axis1"]], 1), nsmall = 1)), 
          
          # Total counts VM
            paste0(format(round(df1_means[["total_counts_vm"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["total_counts_vm"]], 1), nsmall = 1)), 
          
          # Axis 1 per min
            paste0(format(round(df1_means[["axis1_per_min"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["axis1_per_min"]], 1), nsmall = 1)), 
          
          # VM per min
            paste0(format(round(df1_means[["vm_per_min"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["vm_per_min"]], 1), nsmall = 1)), 
          
          # SED time
            paste0(format(round(df1_means[["minutes_SED"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_means[["minutes_SED"]]), ") | ",  
                   format(round(df1_medians[["minutes_SED"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_medians[["minutes_SED"]]), ")"),  
           
          # LPA time 
            paste0(format(round(df1_means[["minutes_LPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_means[["minutes_LPA"]]), ") | ",  
                   format(round(df1_medians[["minutes_LPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_medians[["minutes_LPA"]]), ")"), 
            
          # MPA time 
            paste0(format(round(df1_means[["minutes_MPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_means[["minutes_MPA"]]), ") | ",  
                   format(round(df1_medians[["minutes_MPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_medians[["minutes_MPA"]]), ")"), 
          
          # VPA time 
            paste0(format(round(df1_means[["minutes_VPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_means[["minutes_VPA"]]), ") | ",  
                   format(round(df1_medians[["minutes_VPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_medians[["minutes_VPA"]]), ")"), 
          
          # MVPA time
            paste0(format(round(df1_means[["minutes_MVPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_means[["minutes_MVPA"]]), ") | ",
                   format(round(df1_medians[["minutes_MVPA"]], 1), nsmall = 1), " (", hms::hms(minutes = df1_medians[["minutes_MVPA"]]), ")"),  
           
          # Percent time SED 
            paste0(format(round(df1_means[["percent_SED"]], 1), nsmall = 1), " | ", format(round(df1_medians[["percent_SED"]], 1), nsmall = 1)),
          
          # Percent time LPA 
            paste0(format(round(df1_means[["percent_LPA"]], 1), nsmall = 1), " | ", format(round(df1_medians[["percent_LPA"]], 1), nsmall = 1)),
          
          # Percent time MPA 
            paste0(format(round(df1_means[["percent_MPA"]], 1), nsmall = 1), " | ", format(round(df1_medians[["percent_MPA"]], 1), nsmall = 1)),      
         
          # Percent time VPA 
            paste0(format(round(df1_means[["percent_VPA"]], 1), nsmall = 1), " | ", format(round(df1_medians[["percent_VPA"]], 1), nsmall = 1)),  
         
          # Percent time MVPA  
            paste0(format(round(df1_means[["percent_MVPA"]], 1), nsmall = 1), " | ", format(round(df1_medians[["percent_MVPA"]], 1), nsmall = 1)),                 
          
          # Ratio MVPA/SED
            paste0(format(round(df1_means[["ratio_mvpa_sed"]], 2), nsmall = 2), " | ", format(round(df1_medians[["ratio_mvpa_sed"]], 2), nsmall = 2)),
          
          # MET-hr MVPA
            paste0(format(round(df1_means[["mets_hours_mvpa"]], 2), nsmall = 2), " | ", format(round(df1_medians[["mets_hours_mvpa"]], 2), nsmall = 2)),         
          # Total kilocalories
            paste0(format(round(df1_means[["total_kcal"]], 2), nsmall = 2),   " | ", format(round(df1_medians[["total_kcal"]], 2), nsmall = 2)),
          
          # Physical activity level (PAL) 
            paste0(format(round(df1_means[["pal"]], 2), nsmall = 2),  " | ", format(round(df1_medians[["pal"]], 2), nsmall = 2)),  
            
          # Total number of steps
            paste0(round(df1_means[["total_steps"]], 0), " | ", round(df1_medians[["total_steps"]], 0))

          )

        ),   striped = TRUE, defaultPageSize = 21
)

if (params$rendered_by_shiny)
  shiny::setProgress(0.5)  # set progress to 50%
```

### Comparisons with norms and recommendations
```{r, fig.height=9, fig.width = 17, warning=FALSE, message=FALSE}
# PAL
g_pal <- create_fig_pal(score = df1_means[["pal"]], "en") + theme(plot.margin = margin(2, 1, 0.5, 1, "cm"))
  
# Steps
g_steps <- create_fig_steps(score = df1_means[["total_steps"]], "en") + theme(plot.margin = margin(0, 1, 0.5, 1, "cm"))

# MVPA
g_mvpa <- create_fig_mvpa(score = df1_means[["minutes_MVPA"]], "en") + theme(plot.margin = margin(0, 1, 0, 1, "cm"))

# SED
g_sed <- create_fig_sed(score = df1_means[["minutes_SED"]], "en") + theme(plot.margin = margin(0, 1, 0, 1, "cm"))

# MVPA/SED ratio
g_ratio <- create_fig_ratio_mvpa_sed(score = df1_means[["ratio_mvpa_sed"]], "en") + theme(plot.margin = margin(0, 1, 1, 1, "cm"))

# Whole figure
(g_pal + theme(legend.position = "top")) / g_steps / (g_mvpa | g_sed | g_ratio) + 
    plot_layout(heights = c(0.8, 0.7, 1.5)) & theme(legend.justification = "center")
```

### Comments
```{r, echo = F}
# Determining physical activity guidelines status
guidelines_status <- get_guidelines_status(df1_means[["mets_hours_mvpa"]], language = "en")

# Determining PAL status
pal_status <- get_pal_status(df1_means[["pal"]], language = "en")
```

According to the [Food and Agriculture Organization of the United Nations (FAO)](http://www.fao.org/3/y5686e/y5686e.pdf), the PAL of the patient could be characterized as **`r pal_status`**. After converting moderate and vigorous physical activity minutes to MET-hours, the estimate of the daily mean of MVPA volume for the patient is `r paste0(format(round(df1_means[["mets_hours_mvpa"]], 1), nsmall = 1))` MET-hr, which could correspond to falling **`r guidelines_status`** (i.e., 7.5-15.0 MET-hr a week, or 1.07-2.14 MET-hr a day).


## Step accumulation metrics

### Results by day: Tabular view
```{r}
reactable::reactable(params$results_by_day %>% dplyr::select(date, max_steps_60min:peak_steps_1min) %>%
                       dplyr::rename(
                        Date = date,
                        "Max step acc. 60 min (steps/min)"  = max_steps_60min,
                        "Max step acc. 30 min (steps/min)"  = max_steps_30min,
                        "Max step acc. 20 min (steps/min)"  = max_steps_20min,
                        "Max step acc. 5 min (steps/min)"   = max_steps_5min,
                        "Max step acc. 1 min (steps/min)"   = max_steps_1min,
                        "Peak step acc. 60 min (steps/min)" = peak_steps_60min,
                        "Peak step acc. 30 min (steps/min)" = peak_steps_30min,
                        "Peak step acc. 20 min (steps/min)" = peak_steps_20min,
                        "Peak step acc. 5 min (steps/min)"  = peak_steps_5min,
                        "Peak step acc. 1 min (steps/min)"  = peak_steps_1min
                       ),  
                striped = TRUE,
                list(
                     "Date" = reactable::colDef(
                       style = list(position = "sticky", left = 0, background = "#CCCCCC", zIndex = 1),
                       headerStyle = list(position = "sticky", left = 0,  background = "#fff", zIndex = 1)
                     ),
                     "Max step acc. 60 min (steps/min)"   = reactable::colDef(minWidth = 160),
                     "Max step acc. 30 min (steps/min)"   = reactable::colDef(minWidth = 160),
                     "Max step acc. 20 min (steps/min)"   = reactable::colDef(minWidth = 160),
                     "Max step acc. 5 min (steps/min)"    = reactable::colDef(minWidth = 160),
                     "Max step acc. 1 min (steps/min)"    = reactable::colDef(minWidth = 160),
                     "Peak step acc. 60 min (steps/min)"  = reactable::colDef(minWidth = 160),
                     "Peak step acc. 30 min (steps/min)"  = reactable::colDef(minWidth = 160),
                     "Peak step acc. 20 min (steps/min)"  = reactable::colDef(minWidth = 160),
                     "Peak step acc. 5 min (steps/min)"   = reactable::colDef(minWidth = 160),
                     "Peak step acc. 1 min (steps/min)"   = reactable::colDef(minWidth = 160)
                     )
                )
```

### Results by day: Graphical view
```{r, fig.height=7, fig.width = 17, warning=FALSE, message=FALSE}
create_fig_res_by_day(
  df2, 
  params$minimum_wear_time_for_analysis, 
  params$start_day_analysis, 
  params$end_day_analysis, 
  language = "en",
  metrics = "step_acc"
  ) + theme(plot.margin = margin(1, 1, 1, 1, "cm"))
```

### Means | Medians computed using valid days
```{r, out.width='100%'}
reactable::reactable(
  tibble::tibble(
          Metric = c("Number of valid days",
                     "Max step acc. 60 min (steps/min)",
                     "Max step acc. 30 min (steps/min)",
                     "Max step acc. 20 min (steps/min)",
                     "Max step acc. 5 min (steps/min)",
                     "Max step acc. 1 min (steps/min)",
                     "Peak step acc. 60 min (steps/min)",
                     "Peak step acc. 30 min (steps/min)",
                     "Peak step acc. 20 min (steps/min)",
                     "Peak step acc. 5 min (steps/min)",
                     "Peak step acc. 1 min (steps/min)"
          ),
          
          "Daily mean | median" = c(
            
          # Number of valid days
            paste0(df1_means[["valid_days"]]),
            
          # Max step accum 60min
            paste0(format(round(df1_means[["max_steps_60min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["max_steps_60min"]], 2), nsmall = 2)),
          
          # Max step accum 30min
            paste0(format(round(df1_means[["max_steps_30min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["max_steps_30min"]], 2), nsmall = 2)),
          
          # Max step accum 20min
            paste0(format(round(df1_means[["max_steps_20min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["max_steps_20min"]], 2), nsmall = 2)),
          
          # Max step accum 5min
            paste0(format(round(df1_means[["max_steps_5min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["max_steps_5min"]], 2), nsmall = 2)),
          
          # Max step accum 1min
            paste0(format(round(df1_means[["max_steps_1min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["max_steps_1min"]], 2), nsmall = 2)),
          
          # Peak step accum 60min
            paste0(format(round(df1_means[["peak_steps_60min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["peak_steps_60min"]], 2), nsmall = 2)),
          
          # Peak step accum 30min
            paste0(format(round(df1_means[["peak_steps_30min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["peak_steps_30min"]], 2), nsmall = 2)),
          
          # Peak step accum 20min
            paste0(format(round(df1_means[["peak_steps_20min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["peak_steps_20min"]], 2), nsmall = 2)),
          
          # Peak step accum 5min
            paste0(format(round(df1_means[["peak_steps_5min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["peak_steps_5min"]], 2), nsmall = 2)),
          
          # Peak step accum 1min
            paste0(format(round(df1_means[["peak_steps_1min"]], 2), nsmall = 2), " | ", format(round(df1_medians[["peak_steps_1min"]], 2), nsmall = 2))
          )

        ),   striped = TRUE, defaultPageSize = 17
)
```


## Intensity distribution metrics

### Distribution of time spent in intensity bins
```{r, fig.height = height_factor3, fig.width=13}
params$graph_int_distri_bands
```

### Log-log models
```{r, fig.height = height_factor3, fig.width=13}
params$graph_int_distri_models
```

### Results by day: Tabular view
```{r}
epoch_label <- as.name(paste0(params$epoch,"s"))
reactable::reactable(params$results_by_day %>% dplyr::select(date, ig:M5) %>%
                       dplyr::rename(
                        Date = date,
                        "Intensity gradient"   = ig,
                        "M1/3 (counts/{{epoch_label}})"  :=  "M1/3",
                        "M120 (counts/{{epoch_label}})"  := M120,
                        "M60 (counts/{{epoch_label}})"   := M60, 
                        "M30 (counts/{{epoch_label}})"   := M30,
                        "M15 (counts/{{epoch_label}})"   := M15,
                        "M5 (counts/{{epoch_label}})"    := M5 
                       ),  
                striped = TRUE,
                list(
                     "Date" = reactable::colDef(
                       style = list(position = "sticky", left = 0, background = "#CCCCCC", zIndex = 1),
                       headerStyle = list(position = "sticky", left = 0,  background = "#fff", zIndex = 1)
                     )
                     )
                )
if (params$rendered_by_shiny)
  shiny::setProgress(0.75)  # set progress to 75%
```

### Results by day: Graphical view
```{r, fig.height=9, fig.width = 17, warning=FALSE, message=FALSE}
create_fig_res_by_day(
  df2, 
  params$minimum_wear_time_for_analysis, 
  params$start_day_analysis, 
  params$end_day_analysis, 
  language = "en",
  metrics = "int_distri",
  epoch_label = paste0(params$epoch, "s")
  ) + theme(plot.margin = margin(1, 1, 1, 1, "cm"))
```

### Means | Medians computed using valid days
```{r, out.width='100%'}
reactable::reactable(
  tibble::tibble(
          Metric = c("Number of valid days",
                     "Intensity gradient",
                     paste0("M1/3", " (counts/", params$epoch, "s)"),
                     paste0("M120", " (counts/", params$epoch, "s)"),
                     paste0("M60", " (counts/", params$epoch, "s)"),
                     paste0("M30", " (counts/", params$epoch, "s)"),
                     paste0("M15", " (counts/", params$epoch, "s)"),
                     paste0("M5", " (counts/", params$epoch, "s)")
          ),
          
          "Daily mean | median" = c(
            
          # Number of valid days
            paste0(df1_means[["valid_days"]]),
            
          # Intensity gradient
            paste0(format(round(df1_means[["ig"]], 2), nsmall = 2), " | ", format(round(df1_medians[["ig"]], 2), nsmall = 2)),
                    
          # M1/3
            paste0(format(round(df1_means[["M1/3"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["M1/3"]], 1), nsmall = 1)), 

          # M120
            paste0(format(round(df1_means[["M120"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["M120"]], 1), nsmall = 1)), 
                    
          # M60
            paste0(format(round(df1_means[["M60"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["M60"]], 1), nsmall = 1)), 
                    
          # M30
            paste0(format(round(df1_means[["M30"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["M30"]], 1), nsmall = 1)),
                    
          # M15
            paste0(format(round(df1_means[["M15"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["M15"]], 1), nsmall = 1)), 
                    
          # M5
            paste0(format(round(df1_means[["M5"]], 1), nsmall = 1), " | ",
                   format(round(df1_medians[["M5"]], 1), nsmall = 1))

          )

        ),   striped = TRUE, defaultPageSize = 17
)
```

### Radar view based on the means of the valid days
```{r, fig.align = "center", out.width='50%', out.height='50%', fig.width=8, fig.height=8}
cor_factor <- 60 / params$epoch

create_fig_mx_summary(
data = df1_means,
labels = NULL,
mpa_cutpoint = params$mpa_cutpoint / cor_factor, 
vpa_cutpoint = params$vpa_cutpoint / cor_factor
)
```

## Sedentary behaviour accumulation metrics based on valid days

### Temporal distribution of sedentary bouts
```{r, fig.height = height_factor2, fig.width=13}
if(length(params$p_breaks_sed) == 1) {
  cat("No results. Results can be obtained using 60-s epochs only.")
} else {
  params$p_breaks_sed
}
```

### Alpha coefficient, median bout duration (MBD), usual bout duration (UBD), and Gini index
```{r, out.width='100%', out.height='10%', fig.height=6, fig.width=20}
if(length(params$p_breaks_sed) == 1) {
  cat("No results. Results can be obtained using 60-s epochs only.")
} else {
  
  p1 <- params$p_alpha_sed + guides(color = "none", fill = "none")
  p2 <- params$p_MBD_sed   + guides(color = "none", fill = "none")
  p3 <- params$p_UBD_sed
  p4 <- params$p_gini_sed
 
  (p1  | p2 | p3 | p4) + plot_layout(guides = "collect") & 
    theme(legend.position = 'bottom',
          axis.title = element_text(size = 15),
          legend.title = element_text(size = 17),
          legend.text = element_text(size = 15)
          )
}
```

## Physical activity accumulation metrics based on valid days

### Temporal distribution of physical activity bouts
```{r, fig.height = height_factor2, fig.width=13}
if(length(params$p_breaks_pa) == 1) {
  cat("No results. Results can be obtained using 60-s epochs only.")
} else {
  params$p_breaks_pa
}
if (params$rendered_by_shiny)
  shiny::setProgress(1)  # set progress to 100%
```

### Alpha coefficient, median bout duration (MBD), usual bout duration (UBD), and Gini index
```{r, out.width='100%', out.height='10%', fig.height=6, fig.width=20}
if(length(params$p_breaks_pa) == 1) {
  cat("No results. Results can be obtained using 60-s epochs only.")
} else {
  p1 <- params$p_alpha_pa + guides(color = "none", fill = "none")
  p2 <- params$p_MBD_pa   + guides(color = "none", fill = "none")
  p3 <- params$p_UBD_pa
  p4 <- params$p_gini_pa
 
  (p1  | p2 | p3 | p4) + plot_layout(guides = "collect") & 
    theme(legend.position = 'bottom',
          axis.title = element_text(size = 15),
          axis.text = element_text(size = 10),
          legend.title = element_text(size = 17),
          legend.text = element_text(size = 15)
          )
}
```
