Skip to content

Commit

Permalink
Merge pull request #101 from habitus-eu/issue16_tidyup_countcalculation
Browse files Browse the repository at this point in the history
Tidy up code now count calculation is removed
  • Loading branch information
vincentvanhees authored Mar 7, 2024
2 parents 16731be + 4ebf7ce commit f2ae094
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 56 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ Authors@R:
email = "[email protected]"))
License: Apache License version 2.0 | file LICENSE
Imports: shiny, shinyFiles, GGIR, bslib, methods, jsonlite, DT,
magrittr, shinyjs, callr, hbGPS, hbGIS, actilifecounts, ActCR,
GGIRread, read.gt3x, readxl
magrittr, shinyjs, callr, hbGPS, hbGIS
Remotes: habitus-eu/hbGPS, habitus-eu/hbGIS
LazyData: true
Suggests: testthat, covr, rmarkdown
Expand Down
24 changes: 8 additions & 16 deletions R/GGIRshiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,31 @@
#' @param outputdir Path to output directory
#' @param sleepdiary Path to sleep diary
#' @param configfile Configfile path
#' @param do.Counts Boolean to indicate whether BrondCounts should be derived
#' @return no object is returned, only a new file is created in the output directory
#' @import GGIR
#' @importFrom utils write.table
#' @export
#'

GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c(),
do.Counts = FALSE) {
GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c()) {
if (length(sleepdiary) == 0) sleepdiary = c()
if (length(configfile) == 0) configfile = c()

# create R script with the code to run the data analysis via a command line call
# in this way turning off or restarting the app will not kill the data analysis
fileConn <- file(paste0(outputdir, "/ggir_cmdline.R"))
writeLines(c("#!/usr/bin/env Rscript",
"args = commandArgs(trailingOnly = TRUE)",
"if (length(args) < 4) {",
"stop(\"At least four arguments are expected\", call. = FALSE)",
"if (length(args) < 3) {",
"stop(\"At least three arguments are expected\", call. = FALSE)",
"}",
"if (length(args) == 5) {",
"if (length(args) == 4) {",
"GGIR::GGIR(datadir = args[1], outputdir = args[2], ",
" do.neishabouricounts = as.logical(args[3]),",
"configfile = args[4], loglocation = args[5],",
"configfile = args[3], loglocation = args[4],",
"do.parallel = TRUE)",
"} else {",
"GGIR::GGIR(datadir = args[1], outputdir = args[2], ",
" do.neishabouricounts = as.logical(args[3]),",
"configfile = args[4], do.parallel = TRUE)",
"}",
"if (as.logical(args[3]) == TRUE) {",
"HabitusGUI::Counts2csv(outputdir = paste0(args[2], \"/output_\", basename(args[1])), configfile = args[4])",
"configfile = args[3], do.parallel = TRUE)",
"}"),
fileConn)
close(fileConn)
Expand All @@ -49,7 +44,6 @@ GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c(),
basecommand = paste0(outputdir, "/ggir_cmdline.R ",
rawaccdir, " ",
outputdir, " ",
do.Counts, " ",
configfile, " ",
sleepdiary)
system2(command = "Rscript", args = basecommand,
Expand All @@ -66,11 +60,9 @@ GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c(),
basecommand = paste0("cd ", outputdir, " ; nohup Rscript ggir_cmdline.R ",
rawaccdir, " ",
outputdir, " ",
do.Counts, " ",
configfile, " ",
sleepdiary, " > ", outputdir, "/GGIR.log 2>&1 &")
system2(command = "cd", args = gsub(pattern = "cd ", replacement = "", x = basecommand),
stdout = "", stderr = "", wait = TRUE)
}

}
54 changes: 25 additions & 29 deletions R/myApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
# pkgload::load_all("."); HabitusGUI::myApp(homedir="~/projects/fontys")
# HabitusGUI::myApp(homedir="~/projects")
# pkgload::load_all("."); myApp(homedir="D:/Dropbox/Work/sharedfolder/DATA/Habitus")
# myApp(homedir="D:/Dropbox/Work/sharedfolder/DATA/Habitus/GPSprocessing/NBBB2010")
# myApp(homedir="D:/Dropbox/Work/sharedfolder/DATA/Habitus/GPSprocessing")


# roxygen2::roxygenise()

Expand Down Expand Up @@ -799,38 +800,29 @@ myApp <- function(homedir=getwd(), ...) {
# Apply GGIR after button is pressed
#========================================================================
runGGIR <- eventReactive(input$start_ggir, {
GGIRCounts_message = ""
GGIR_message = ""

if ("GGIR" %in% input$tools) {
GGIRCounts_message = ""
GGIR_message = ""
# Basic check before running function:
ready_to_run_ggirCounts = FALSE
ready_to_run_ggir = FALSE
if (dir.exists(global$raw_acc_in)) {
acc_files_available = length(dir(path = global$raw_acc_in, pattern = "csv|bin|gt3x|bin|cwa|wav", recursive = FALSE, full.names = FALSE)) > 0
if (acc_files_available == TRUE) {
ready_to_run_ggirCounts = TRUE
ready_to_run_ggir = TRUE
} else {
GGIRCounts_message = paste0("No count files found in ", global$raw_acc_in)
GGIR_message = paste0("No files found in ", global$raw_acc_in)
}
} else {
GGIRCounts_message = paste0("Folder that is supposed to hold acceleration files does not exist: ", global$raw_acc_in)
GGIR_message = paste0("Folder that is supposed to hold acceleration files does not exist: ", global$raw_acc_in)
}
# Only run function when checks are met:
if (ready_to_run_ggirCounts == TRUE) {
if (ready_to_run_ggir == TRUE) {
shinyjs::hide(id = "start_ggir")
# this line makes that if user is trying to use a config defined in a previous
# run, the data path is correctly defined
config = read.csv(as.character(configfileGGIR()))
config.Counts = config$value[which(config$argument == "do.neishabouricounts")]
if (as.logical(config.Counts) == TRUE) {
# if counts was not selected as a tool, but acc.metric was defined as
# NeishabouriCount, then turn do.Counts to TRUE
id_ggir = showNotification("GGIR and CountConverter in progress ...", type = "message", duration = NULL, closeButton = FALSE)
do.Counts = TRUE
} else {
id_ggir = showNotification("GGIR in progress ...", type = "message", duration = NULL, closeButton = FALSE)
do.Counts = FALSE
}
# this line makes that if user is trying to use a config defined in a previous
# run, the data path is correctly defined
config = read.csv(as.character(configfileGGIR()))
id_ggir = showNotification("GGIR in progress ...", type = "message", duration = NULL, closeButton = FALSE)

if (file.exists(paste0(global$data_out, "/sleepdiary.csv"))) { # because this is not a global variable
sleepdiaryfile_local = paste0(global$data_out, "/sleepdiary.csv")
Expand All @@ -848,16 +840,15 @@ myApp <- function(homedir=getwd(), ...) {
})
# Start GGIR
x_ggir <- r_bg(func = function(GGIRshiny, rawaccdir, outputdir,
sleepdiary, configfile, do.Counts){
sleepdiary, configfile){
GGIRshiny(rawaccdir, outputdir,
sleepdiary, configfile, do.Counts)
sleepdiary, configfile)
},
args = list(GGIRshiny = GGIRshiny,
rawaccdir = isolate(global$raw_acc_in),
outputdir = global$data_out,
sleepdiary = sleepdiaryfile_local,
configfile = cleanPath(paste0(global$data_out, "/config.csv")),
do.Counts = do.Counts),
configfile = cleanPath(paste0(global$data_out, "/config.csv"))),
stdout = "",
stderr = "")

Expand All @@ -867,11 +858,15 @@ myApp <- function(homedir=getwd(), ...) {
observe({
if (x_ggir$poll_io(0)[["process"]] != "ready") {
# Copy local log file to server to update progress log
if (file.exists(logfile)) file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE)
if (file.exists(logfile)) {
file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE)
}
invalidateLater(2000)
} else {
# Copy local log file to server to update progress log
if (file.exists(logfile)) file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE)
if (file.exists(logfile)) {
file.copy(from = logfile, to = stdout_GGIR_tmp, overwrite = TRUE)
}
on.exit(removeNotification(id_ggir), add = TRUE)

# Delete Rscript that is created by GGIRshiny because user does not need this
Expand All @@ -884,14 +879,14 @@ myApp <- function(homedir=getwd(), ...) {
expected_outputdir_ggir = paste0(global$data_out, "/output_", basename(global$raw_acc_in))
expected_ggiroutput_file = paste0(global$data_out, "/output_", basename(global$raw_acc_in), "/results/part2_daysummary.csv")
if (file.exists(expected_ggiroutput_file) == TRUE) { # checks whether ggir output was created
GGIRCounts_message = paste0(#"GGIR successfully completed at ", Sys.time(),
GGIR_message = paste0(#"GGIR successfully completed at ", Sys.time(),
"Output is stored in: ", #<br/>
expected_outputdir_ggir,
"<br/>The table below shows the content of part2_daysummary.csv:")
GGIRpart2 = read.csv(expected_ggiroutput_file, nrow = 100)
output$GGIRpart2 <- DT::renderDataTable(GGIRpart2, options = list(scrollX = TRUE))
output$ggir_end_message <- renderUI({
HTML(paste0(GGIRCounts_message))
HTML(paste0(GGIR_message))
})
}
}
Expand Down Expand Up @@ -976,6 +971,7 @@ myApp <- function(homedir=getwd(), ...) {
configfile = paste0(global$data_out, "/config_hbGPS.csv")),
stdout = stdout_hbGPS_tmp,
stderr = "2>&1")

observe({
if (x_hbGPS$poll_io(0)[["process"]] != "ready") {
invalidateLater(5000)
Expand Down
14 changes: 14 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
comment: false

coverage:
status:
project:
default:
target: auto
threshold: 1%
informational: true
patch:
default:
target: auto
threshold: 1%
informational: true
Binary file modified inst/testfiles_ggir/example_config_files_GGIR.zip
Binary file not shown.
10 changes: 1 addition & 9 deletions man/GGIRshiny.Rd

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

0 comments on commit f2ae094

Please sign in to comment.