## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(wrictools)

## ----eval = FALSE-------------------------------------------------------------
# library(remotes)
# install_github("NinaZiegenbein/wrictools")

## ----eval = FALSE-------------------------------------------------------------
# install.packages("wrictools")

## -----------------------------------------------------------------------------
library(wrictools)

## -----------------------------------------------------------------------------
data_txt <- system.file("extdata", "data.txt", package = "wrictools") # loading example data
result <- preprocess_wric_file(data_txt)

r1_metadata <- result$metadata$r1
r2_metadata <- result$metadata$r2
df_room1 <- result$dfs$room1
df_room2 <- result$dfs$room2

## -----------------------------------------------------------------------------
head(r1_metadata)
head(df_room1)

## -----------------------------------------------------------------------------
data_v2_txt <- system.file("extdata", "data_v2.txt", package = "wrictools") # loading example data
result <- preprocess_wric_file(data_v2_txt)

# For version 2 we only have data from one of the rooms, so only one metadata and one wric-data data.frame
metadata <- result$metadata$metadata
df<- result$dfs$data

# Let's look at the first rows (head) of the data.frame
head(df)

## -----------------------------------------------------------------------------
result <- preprocess_wric_file(
  filepath = data_txt, 
  code = "id", 
  manual = NULL, 
  save_csv = FALSE, 
  path_to_save = NULL, 
  combine = TRUE, 
  method = "mean", 
  start = NULL, 
  end = NULL, 
  notefilepath = NULL, 
  keywords_dict = NULL, 
  entry_exit_dict = NULL
)

## -----------------------------------------------------------------------------
?preprocess_wric_file

## -----------------------------------------------------------------------------
note_txt <- system.file("extdata", "note.txt", package = "wrictools") # loading example data
result <- preprocess_wric_file(data_txt, 
                            notefilepath=note_txt)
head(result$dfs$room1)

## -----------------------------------------------------------------------------
# Example how to specify your own keywords in German
entry_exit_dict <- list(
  end = c("aus", "raus", "Ausgang", "Ende"),
  start = c("rein", "in der Kammer", "innen", "hinein")
)
preprocess_wric_file(data_txt, entry_exit_dict = entry_exit_dict)

## -----------------------------------------------------------------------------
keywords_dict <- list(
  sleeping = list(keywords = list(c("seng", "sleeping", "bed", "sove", "soeve", "godnat", "night", "sleep")), value = 1),
  eating = list(keywords = list(c("start", "begin", "began"), c("maaltid", "eat", "meal", "food", "spis", "maal", "mad", "frokost", "morgenmad", "middag", "snack", "aftensmad")), value = 2),
  stop_sleeping = list(keywords = list(c("vaagen", "vaekke", "wake", "woken", "vaagnet")), value = 0),
  stop_anything = list(keywords = list(c("faerdig", "stop", "end ", "finished", "slut")), value = 0),
  activity = list(keywords = list(c("start", "begin", "began"), c("step", "exercise", "physical activity", "active", "motion", "aktiv")), value = 3),
  ree_start = list(keywords = list(c("start", "begin", "began"), c("REE", "BEE", "BMR", "RMR", "RER")), value = 4)
)

## ----eval = FALSE-------------------------------------------------------------
# # Specify the folder with the wric_data
# data_folder <- "./example_data/my_project"
# 
# # Find all files in the folder that start with "Results_"
# data_files <- list.files(data_folder, pattern = "^Results_", full.names = TRUE)
# 
# # Iterate over all files, call the function and save the csv-files in the same folder
# for (data_file in data_files) {
#   preprocess_wric_file(data_file, path_to_save = data_folder, code = "id+comment")
# }

## ----eval = FALSE-------------------------------------------------------------
# data_folder <- "./example_data/my_project"
# 
# data_files <- list.files(data_folder, pattern = "^Results_.*_(\\d{12})\\.txt$",
#                         full.names = TRUE)
# note_files <- list.files(data_folder, pattern = "^note_(\\d{12})\\.txt$",
#                         full.names = TRUE)
# 
# # Create a lookup table by extracting the 12-digit date from the filenames
# note_lookup <- setNames(note_files, sub("^(note_)(\\d{12})\\.txt$", "\\2",
#                         basename(note_files)))
# 
# # Loop through the data files and match the date with the note_lookup
# for (data_file in data_files) {
#   date <- sub(".*_(\\d{12})\\.txt$", "\\1", basename(data_file))
#   print(date)
#   if (date %in% names(note_lookup)) {
#     preprocess_wric_file(data_file, notefilepath = note_lookup[date],
#                           path_to_save = data_folder, code = "id+comment")
#     message("Processed: ", data_file)
#   }
# }

## ----eval=FALSE---------------------------------------------------------------
# # Manually specify the pairs of data files and note files (these are made up examples)
# filename_pairs <- list(
#   list(
#     data_file = "./example_data/my_project/Results_1m_0101_202501130800.txt",
#     note_file = "./example_data/my_project/note_202501130800.txt"
#   ),
#   list(
#     data_file = "./example_data/my_project/Results_1m_0101_202501190800.txt",
#     note_file = "./example_data/my_project/note_202501190800.txt"
#   ),
#   list(
#     data_file = "./example_data/my_project/Results_1m_0101_202501250800.txt",
#     note_file = "./example_data/my_project/note_202501250800.txt"
#   )
# )
# 
# # Loop through the filename pairs and process them
# for (pair in filename_pairs) {
#   preprocess_wric_file(pair$data_file, notefilepath = pair$note_file,
#                         path_to_save = "./example_data/my_project",
#                         code = "id+comment")
#   message("Processed: ", pair$data_file, " and ", pair$note_file)
# }

## ----eval = FALSE-------------------------------------------------------------
# data <- read.csv("./example_data/my_project/XXXX_comment_WRIC_data.csv")
# head(data)

## -----------------------------------------------------------------------------
result <- preprocess_wric_file(data_txt, notefilepath = note_txt)
data <- result$dfs$room1
head(data)

## -----------------------------------------------------------------------------
# we take the first (1) instance where the protocol is 2 (eating)
breakfast_index <- which(data$protocol == 2)[1] 
print(breakfast_index)
# we create a new data.frame where we take the next 14 rows after the start_index
data_breakfast <- data[breakfast_index:(breakfast_index + 14),] 
head(data_breakfast) #Let's look at the data to check wether it worked correctly

## -----------------------------------------------------------------------------
# we additionally check wether the row right before (lag) is not 2 and 
# then take the second instance (2) to get the dinner time
dinner_index <- which(data$protocol == 2 & dplyr::lag(data$protocol) != 2)[2] 
print(dinner_index)
data_dinner <- data[dinner_index:(dinner_index + 14),]
head(data_dinner) #Let's look at the data to check wether it worked correctly

## -----------------------------------------------------------------------------
t.test(data_breakfast$RER, data_dinner$RER, paired = TRUE)

## ----eval=FALSE---------------------------------------------------------------
# files <- list.files(folder_path, pattern = "_data\\.csv$", full.names = TRUE)
# for (file in files) {
#     data <- read.csv(file)
#     breakfast_index <- which(data$protocol == 2)[1]
#     data_breakfast <- data[breakfast_index:(breakfast_index + 59),]
#     dinner_index <- which(data$protocol == 2 & dplyr::lag(data$protocol) != 2)[2]
#     data_dinner <- data[dinner_index:(dinner_index + 59),]
#     message("T-Test Result for : ", file)
#     t.test(data_breakfast$RER, data_dinner$RER, paired = TRUE)
# }

## -----------------------------------------------------------------------------
example_csv <- system.file("extdata", "example.csv", package = "wrictools") # loading example data
visualize_with_protocol(example_csv) 

## ----eval = FALSE-------------------------------------------------------------
# # Path to the folder containing the files
# folder_path <- "example_data/my_project"
# 
# # Get all files ending with "_data.csv"
# csv_files <- list.files(folder_path, pattern = "_data.csv", full.names = TRUE)
# dataframes <- list()
# 
# protocol_colors_labels <- data.frame(
#   protocol = c(0, 1, 2, 3, 4),
#   color = c("white", "purple", "#4b3302", "#48c5a6", "#d0a4c6"),
#   label = c("Normal", "Something", "Nothing", "Third Thing", "?")
# )
# 
# for (file in csv_files) {
#   visualize_with_protocol(file, plot="Energy Expenditure (kcal/min)",
#                           protocol_colors_labels = protocol_colors_labels,
#                           save_png = TRUE)
# }

