Skip to content

Commit

Permalink
Merge pull request #5 from jhmigueles/issue54_normalizepath_tidyupcode
Browse files Browse the repository at this point in the history
improve cleanPath function and condense code
  • Loading branch information
vincentvanhees authored Jun 30, 2023
2 parents 9884ffc + a6db0df commit d2646b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
11 changes: 11 additions & 0 deletions R/cleanPath.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ cleanPath = function(path) {
empty = which(split_tmp == "")
if (length(empty) > 0) split_tmp = split_tmp[-empty]
newPath = paste(split_tmp, collapse = .Platform$file.sep)
# In Linux the first character of the path can be a forward
# slash, which needs to be kept
firstChar = substring(path, 1, 1)
if (firstChar == "/") {
newPath = paste0("/", newPath)
}
# In both Linux and Windows ~ signs may be used as shortcut to the users home
# directory. To avoid inconsistencies we replace them by the full path.
# If we do not do this myApp may copy ~/config.csv to /home/user/config.csv which
# essentially is the same file and would then result in an empty file.
newPath = normalizePath(newPath)
return(newPath)
}
59 changes: 32 additions & 27 deletions R/myApp.R
Original file line number Diff line number Diff line change
Expand Up @@ -458,23 +458,31 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
}
if (configs_ready == TRUE) {
showNotification("Saving configuration file(s) to output folder", type = "message", duration = 2)
if ("GGIR" %in% input$tools) {
if (length(configfileGGIR()) > 0) {
if (cleanPath(configfileGGIR()) != cleanPath(paste0(global$data_out, "/config.csv"))) {
file.copy(from = configfileGGIR(), to = paste0(global$data_out, "/config.csv"),

copyFile = function(from, to) {
# Copies configuration file to output folder if:
# - from and to are not the same path
# - filesize of from is larger than 0
if (from != to) {
fileSize = file.info(from)$size
if (fileSize > 0) { # only copy if filesize is more than 0
file.copy(from = from, to = to,
overwrite = TRUE, recursive = FALSE, copy.mode = TRUE)
}
values$configfileGGIR = paste0(global$data_out, "/config.csv")
}
return(to)
}
if ("GGIR" %in% input$tools) {
config_from = cleanPath(configfileGGIR())
config_to = cleanPath(paste0(global$data_out, "/config.csv"))
if (length(config_from) > 0) {
values$configfileGGIR = copyFile(from = config_form, to = config_to)
}
# ---
if (length(sleepdiaryfile()) > 0) {
current_sleepdiaryfile = as.character(parseFilePaths(c(home = homedir), sleepdiaryfile())$datapath)
if (length(current_sleepdiaryfile) > 0) {
if (current_sleepdiaryfile != paste0(global$data_out, "/sleepdiary.csv")) {
file.copy(from = current_sleepdiaryfile, to = paste0(global$data_out, "/sleepdiary.csv"),
overwrite = TRUE, recursive = FALSE, copy.mode = TRUE)
}
sleepdiaryfile_local = paste0(global$data_out, "/sleepdiary.csv")
diary_from = cleanPath(as.character(parseFilePaths(c(home = homedir), sleepdiaryfile())$datapath))
diary_to = cleanPath(paste0(global$data_out, "/sleepdiary.csv"))
if (length(config_from) > 0) {
sleepdiaryfile_local = copyFile(from = diary_form, to = diary_to)
} else {
sleepdiaryfile_local = c()
}
Expand All @@ -484,21 +492,17 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
}
}
if ("PALMSpy" %in% input$tools) {
if (length(configfilePALMSpy()) > 0) {
if (configfilePALMSpy() != paste0(global$data_out, "/config.json")) {
file.copy(from = configfilePALMSpy(), to = paste0(global$data_out, "/config.json"),
overwrite = TRUE, recursive = FALSE, copy.mode = TRUE)
}
values$configfilePALMSpy = paste0(global$data_out, "/config.json")
config_from = cleanPath(configfilePALMSpy())
config_to = cleanPath(paste0(global$data_out, "/config.json"))
if (length(config_from) > 0) {
values$configfilePALMSpy = copyFile(from = config_from, to = config_to)
}
}
if ("palmsplusr" %in% input$tools) {
if (length(configfilepalmsplusr()) > 0) {
if (configfilepalmsplusr() != paste0(global$data_out, "/config_palmsplusr.csv")) {
file.copy(from = configfilepalmsplusr(), to = paste0(global$data_out, "/config_palmsplusr.csv"),
overwrite = TRUE, recursive = FALSE, copy.mode = TRUE)
}
values$configfilepalmsplusr = paste0(global$data_out, "/config_palmsplusr.csv")
config_from = cleanPath(configfilepalmsplusr())
config_to = cleanPath(paste0(global$data_out, "/config.json"))
if (length(config_from) > 0) {
values$configfilepalmsplusr = copyFile(from = config_from, to = config_to)
}
}
save(values, file = "./HabitusGUIbookmark.RData")
Expand Down Expand Up @@ -788,6 +792,7 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
configfilePALMSpy <- modConfigServer("edit_palmspy_config", tool = reactive("PALMSpy"), homedir = homedir, prevConfig = prevConfigPALMSpy)
configfileGGIR <- modConfigServer("edit_ggir_config", tool = reactive("GGIR"), homedir = homedir, prevConfig = prevConfigGGIR)
configfilepalmsplusr <- modConfigServer("edit_palmsplusr_config", tool = reactive("palmsplusr"), homedir = homedir, prevConfig = prevConfigPalmsplusr)


#========================================================================
# Apply GGIR / Counts after button is pressed
Expand Down Expand Up @@ -853,9 +858,9 @@ overflow-y:scroll; max-height: 300px; background: ghostwhite;}")),
rawaccdir = isolate(global$raw_acc_in),
outputdir = global$data_out,
sleepdiary = sleepdiaryfile_local,
configfile = paste0(global$data_out, "/config.csv"), #isolate(configfileGGIR()),
configfile = cleanPath(paste0(global$data_out, "/config.csv")),
do.Counts = do.Counts),
stdout = "", #,
stdout = "",
stderr = "")

# Expected location of log file
Expand Down

0 comments on commit d2646b0

Please sign in to comment.