Skip to content

Commit

Permalink
Merge pull request #79 from habitus-eu/jhmigueles-issue42_tsvGGIR
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvanhees authored May 1, 2023
2 parents 21f74b7 + d44fdac commit 32cb856
Show file tree
Hide file tree
Showing 6 changed files with 288 additions and 212 deletions.
54 changes: 40 additions & 14 deletions R/GGIRshiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c(),
do.Counts = FALSE) {
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"))
Expand All @@ -24,26 +23,53 @@ GGIRshiny = function(rawaccdir, outputdir, sleepdiary = c(), configfile = c(),
"}",
"if (length(args) == 5) {",
"GGIR::GGIR(datadir = args[1], outputdir = args[2], ",
" overwrite = FALSE, do.neishabouricounts = args[3],",
" do.neishabouricounts = as.logical(args[3]),",
"configfile = args[4], loglocation = args[5],",
"do.parallel = TRUE)",
"} else {",
"GGIR::GGIR(datadir = args[1], outputdir = args[2], ",
" overwrite = FALSE, do.neishabouricounts = as.logical(args[3]),",
" do.neishabouricounts = as.logical(args[3]),",
"configfile = args[4], do.parallel = TRUE)",
"}",
"HabitusGUI::Counts2csv(outputdir = paste0(args[2], \"/output_\", basename(args[1])), configfile = args[4])"),
"if (as.logical(args[3]) == TRUE) {",
"HabitusGUI::Counts2csv(outputdir = paste0(args[2], \"/output_\", basename(args[1])), configfile = args[4])",
"}"),
fileConn)
close(fileConn)

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)

if (.Platform$OS.type == "windows") {
logFile = paste0(outputdir, "/GGIR.log")
fileConn = file(logFile)
writeLines(c("Hello, this Shiny app is primarily designed for Linux.",
"In Windows OS live progress of the analysis can be followed in the RStudio console.",
"In Unix-like systems the progress would be shown here inside this window in the Shiny app."), fileConn)
close(fileConn)

basecommand = paste0(outputdir, "/ggir_cmdline.R ",
rawaccdir, " ",
outputdir, " ",
do.Counts, " ",
configfile, " ",
sleepdiary)
system2(command = "Rscript", args = basecommand,
stdout = "",
stderr = "", wait = TRUE)
write.table(x = paste0(""),
file = paste0(outputdir, "/GGIR.log"))

fileConn = file(logFile)
writeLines(c(""), fileConn)
close(fileConn)

} else {
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)
}

}
16 changes: 12 additions & 4 deletions R/myApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# HabitusGUI::myApp(homedir="~/projects")
# pkgload::load_all("."); myApp(homedir="~/projects/fontys")
# roxygen2::roxygenise()
#/Member Files: LineMatthiesen#8897


# create temp log file
Expand Down Expand Up @@ -610,8 +609,17 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
id_ggir = showNotification("GGIR and Counts 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
config = read.csv(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 Counts 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
}
}
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 @@ -625,7 +633,7 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
output$mylog_GGIR <- renderText({
paste(mylog_GGIR(), collapse = '\n')
})

print
# Start GGIR
x_ggir <- r_bg(func = function(GGIRshiny, rawaccdir, outputdir,
sleepdiary, configfile, do.Counts){
Expand Down
20 changes: 20 additions & 0 deletions R/update_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ update_params = function(new_params = c(), file = c(), format="json_palmspy") {
ind = which(rownames(params) %in% rownames(new_params)[j] == TRUE)
params$value[ind] = new_params$value[j]
}
# match acc.metric with do.metric arguments
do_metrics = c("do.zcx", "do.zcy", "do.zcz",
"do.en", "do.enmo", "do.enmoa",
"do.lfen", "do.lfenmo",
"do.bfen", "do.hfen", "do.hfenplus",
"do.mad", "do.brondcounts", "do.neishabouricounts",
"do.roll_med_acc_x", "do.roll_med_acc_y", "do.roll_med_acc_z",
"do.dev_roll_med_acc_x", "do.dev_roll_med_acc_y", "do.dev_roll_med_acc_z",
"do.lfx", "do.lfy", "do.lfz", "do.hfx", "do.hfy", "do.hfz",
"do.bfx", "do.bfy", "do.bfz")
acc.metric = params["acc.metric", "value"]
# if neishabouri counts, remove reference to axis
if (grepl("Neishabouri", acc.metric)) acc.metric = gsub("_x|_y|_z|_vm", "", acc.metric)
do_argument = grep(acc.metric, rownames(params), value = TRUE, ignore.case = TRUE)
# to avoid detecting also enmoa and lfenmo when selecting enmo
# (this does not occur with any other metric):
if (acc.metric == "ENMO") do_argument = "do.enmo"
# set metric of interest to TRUE and rest to FALSE
params[do_argument, "value"] = TRUE
params[do_metrics[-which(do_metrics == do_argument)], "value"] = FALSE
write.csv(x = params, file = file, row.names = FALSE)
} else if (format == "csv_palmsplusr") {
params = read.csv(file = file, sep = ",")
Expand Down
7 changes: 7 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
\title{News for Package \pkg{HabitusGUI}}
\newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}

\section{Changes in version 0.1.6 (GitHub-only-release date: ?-?-2023)}{
\itemize{
\item Revised tsv file for GGIR configuration
\item Now able to handle acc.metric in GGIR
\item tsv GGIR file revised and testfile for config GGIR is updated
}
}
\section{Changes in version 0.1.6 (GitHub-only-release date: 15-03-2023)}{
\itemize{
\item GGIR config file is now checked when it is loaded
Expand Down
Loading

0 comments on commit 32cb856

Please sign in to comment.