Skip to content

Commit

Permalink
Merge pull request #95 from habitus-eu/issue94_issues_Teun
Browse files Browse the repository at this point in the history
Address variety of issues identified while testing
  • Loading branch information
vincentvanhees authored Sep 28, 2023
2 parents a6b6e4c + 31a542b commit ab5c04a
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 307 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: HabitusGUI
Title: R Shiny App for Processing Behavioural Data
Description: Shiny app to ease processing behavioural data with research software such as GGIR, activityCounts, PALMSpy,and palmsplusr.
Version: 0.2.0
Date: 2022-09-30
Version: 0.3.0
Date: 2022-09-28
Authors@R:
c(person(given = "Vincent",
family = "van Hees",
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ importFrom(methods,new)
importFrom(methods,setClass)
importFrom(purrr,reduce)
importFrom(readr,read_csv)
importFrom(readr,write_csv)
importFrom(rlang,UQ)
importFrom(rlang,parse_expr)
importFrom(stats,aggregate)
Expand Down
32 changes: 17 additions & 15 deletions R/check_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,31 @@ check_params = function(params = c(), tool = c()) {
cnt = 1
if (length(numi) > 0) {
for (i in numi) {

val = params$value[i]
# Consider vectors specified with c()
num_value = unlist(lapply(strsplit(x = as.character(val), "[(]|[)]|[,]|c"), function(x){x[!x == ""]}))
# Consider vectors specified with :
val2expand = grep(pattern = ":", x = num_value)
if (length(val2expand) > 0) {
for (vi in 1:length(val2expand)) {
tmp = as.numeric(unlist(strsplit(num_value[val2expand[vi]], ":")))
num_value = c(num_value, tmp[1]:tmp[2])
}
num_value = num_value[-val2expand]
}
# Attempt to turn into numeric
try(expr = {
num_value = suppressWarnings(
as.numeric(unlist(lapply(strsplit(x = as.character(params$value[i]), "[(]|[)]|[,]|c"), function(x){x[!x == ""]})))
as.numeric(num_value)
)
}, silent = TRUE)
test_na = any(is.na(num_value))
if (test_na == TRUE) {
if (test_na == TRUE || length(num_value) == 0) {
blocked_params$name[cnt] = rowNames[i]
blocked_params$error[cnt] = "is not numeric"
cnt = cnt + 1
} else {
if (length(num_value) > 1) {
if (tool == "GGIR") {
nc = nchar(params$value[i])
if (substr(params$value[i], 1, 2) != "c(" | substr(params$value[i], nc, nc) != ")") {
blocked_params$name[cnt] = rowNames[i]
blocked_params$error[cnt] = "numeric vector needs to start with c( and end with ), with values separated by a comma"
cnt = cnt + 1
}
}
}
}
if (test_na == FALSE) {
} else if (test_na == FALSE) {
if (params$class[i] == "integer") {
if (any(round(num_value) != num_value)) {
blocked_params$name[cnt] = rowNames[i]
Expand Down
5 changes: 5 additions & 0 deletions R/identify_tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,10 @@ identify_tools = function(datatypes = c("AccRaw", "ACount", "GPS", "GIS",
tools_needed = tools_needed[-which(tools_needed == "CountConverter")]
}
}
# Mask tools that will be deprecated
if (any(c("CountConverter", "PALMSpy") %in% tools_needed == TRUE)) {
tools_needed = tools_needed[-which(tools_needed %in% c("CountConverter", "PALMSpy"))]
}

invisible(list(tools_needed = tools_needed, iotools = iotools[which(names(iotools) %in% tools_needed)]))
}
6 changes: 3 additions & 3 deletions R/modConfigServer.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ modConfigServer = function(id, tool, homedir = getwd()) {
contentType = "application/json")
} else if (tool() == "GGIR") {
output$download = downloadHandler(
filename = "config.csv",
filename = "example_config_files_GGIR.zip",
content <- function(file) {
config_default = system.file("testfiles_ggir/config.csv", package = "HabitusGUI")[1]
config_default = system.file("testfiles_ggir/example_config_files_GGIR.zip", package = "HabitusGUI")[1]
if (config_default != file) file.copy(config_default, file)
},
contentType = "text/csv")
contentType = "zip")
} else if (tool() == "hbGPS") {
output$download = downloadHandler(
filename = "config_hbGPS.csv",
Expand Down
90 changes: 52 additions & 38 deletions R/myApp.R

Large diffs are not rendered by default.

34 changes: 18 additions & 16 deletions R/palmsplusr_shiny.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @return palms_to_clean_lower object
#' @importFrom stats end start formula as.formula
#' @importFrom tidyr pivot_wider
#' @importFrom readr read_csv
#' @importFrom readr write_csv read_csv
#' @import palmsplusr
#' @import dplyr
#' @importFrom utils head tail
Expand Down Expand Up @@ -69,6 +69,8 @@ palmsplusr_shiny <- function(gisdir = "",
sf::sf_use_s2(FALSE)
# identify palms csv output files in palmsdir:
palms_country_files <- list.files(path = palmsdir, pattern = "*.csv", full.names = TRUE)
# skip the combined file that hbGPS generates
palms_country_files = grep(pattern = "combined.csv", x = palms_country_files, invert = TRUE, value = TRUE)
# read and combine palms csv output files
csv_palms <- lapply(palms_country_files, FUN = readr::read_csv, col_types = list(
identifier = readr::col_character(),
Expand All @@ -81,31 +83,29 @@ palmsplusr_shiny <- function(gisdir = "",
tripType = readr::col_integer(),
tripMOT = readr::col_integer(),
activity = readr::col_double()
))
), show_col_types = FALSE)
PALMS_combined <- bind_rows(csv_palms)
# Data cleaning:
if (verbose) cat("\nstart cleaning\n")
# if (verbose) cat("\nstart cleaning...\n")
PALMS_reduced <- subset(PALMS_combined, lon > -180)
palms_reduced_cleaned <- check_and_clean_palms_data(PALMS_reduced, dataset_name, outputdir)
if (verbose) cat("\ncleaning completed\n")

# if (verbose) cat("\ncleaning completed\n")
PALMS_reduced$dateTime = as.POSIXct(PALMS_reduced$dateTime, format = "%d/%m/%Y %H:%M:%S", tz = "")

# Write to csv and read using read_palms to format the object as expected from the rest of the code
PALMS_reduced_file = normalizePath(paste0(palmsplus_folder, "/", stringr::str_interp("PALMS_${dataset_name}_reduced.csv")))
if (verbose) cat(paste0("\nCheck PALMS_reduced_file: ", PALMS_reduced_file))
# if (verbose) cat(paste0("\nCheck PALMS_reduced_file: ", PALMS_reduced_file))
write.csv(palms_reduced_cleaned, PALMS_reduced_file)
palms = palmsplusr::read_palms(PALMS_reduced_file)
palms = palmsplusr::read_palms(PALMS_reduced_file, verbose = FALSE)
palms$datetime = as.POSIXct(palms$datetime, format = "%d/%m/%Y %H:%M:%S", tz = "")

# Helper function to find shape files
find_file = function(path, namelowercase) {
allcsvfiles = dir(path, recursive = TRUE, full.names = TRUE)
file_of_interest = allcsvfiles[which(tolower(basename(allcsvfiles)) == namelowercase)]
return(file_of_interest)
}
if (verbose) cat("\nreading basis file\n")
participant_basis = read_csv(gislinkfile)
# if (verbose) cat("\nreading basis file\n")
participant_basis = read_csv(gislinkfile, show_col_types = FALSE)
# Load all shape files ----------------------------------------------------
#----------------
# NEW CODE
Expand Down Expand Up @@ -145,7 +145,9 @@ palmsplusr_shiny <- function(gisdir = "",
participant_basis = withoutMissingId$participant_basis
loca = withoutMissingId$loca
write.csv(participant_basis, paste0(palmsplus_folder, "/", stringr::str_interp("participant_basis_${dataset_name}.csv"))) # store file for logging purposes only

if (length(participant_basis) == 0 || nrow(participant_basis) == 0) {
stop("\nParticipant basis file does not include references for the expected recording IDs")
}

#===========================================================================================
# Create field tables
Expand Down Expand Up @@ -242,7 +244,7 @@ palmsplusr_shiny <- function(gisdir = "",
loca = loca,
participant_basis = participant_basis,
verbose = verbose)
data.table::fwrite(palmsplus, file = fns[1])
write_csv(palmsplus, file = fns[1])
if (verbose) cat(">>>\n")
} else {
if (verbose) cat("skipped because insufficient input data>>>\n")
Expand All @@ -259,7 +261,7 @@ palmsplusr_shiny <- function(gisdir = "",

if (length(days) > 0) {
if (verbose) cat(paste0(" N rows in days object: ", nrow(days)))
data.table::fwrite(x = days, file = fns[2])
write_csv(x = days, file = fns[2])
} else {
if (verbose) cat(paste0(" WARNING: no days object produced."))
}
Expand All @@ -278,7 +280,7 @@ palmsplusr_shiny <- function(gisdir = "",
trajectory_fields = trajectory_fields,
trajectory_locations = trajectory_locations)
if (length(trajectories) > 0) {
data.table::fwrite(trajectories, file = fns[3])
write_csv(trajectories, file = fns[3])
shp_file = paste0(palmsplus_folder, "/", dataset_name, "_trajecories.shp")
if (file.exists(shp_file)) file.remove(shp_file) # remove because st_write does not know how to overwrite

Expand All @@ -302,15 +304,15 @@ palmsplusr_shiny <- function(gisdir = "",
verbose = verbose)

if (length(multimodal) > 0) {
data.table::fwrite(multimodal, file = fns[4])
write_csv(multimodal, file = fns[4])
shp_file = paste0(palmsplus_folder, "/", dataset_name, "_multimodal.shp")
if (file.exists(shp_file)) file.remove(shp_file) # remove because st_write does not know how to overwrite
sf::st_write(obj = multimodal, dsn = shp_file)
if (verbose) cat(paste0(" N rows in multimodal object: ", nrow(multimodal)))
} else {
if (verbose) cat(paste0(" WARNING: no multimodal object produced."))
}
if (verbose) cat(">>>\n")
if (verbose) cat(">>>\n\n")
} else {
if (verbose) cat("skipped because insufficient input data>>>\n")
}
Expand Down
5 changes: 5 additions & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
\name{NEWS}
\title{News for Package \pkg{HabitusGUI}}
\newcommand{\cpkg}{\href{http://CRAN.R-project.org/package=#1}{\pkg{#1}}}
\section{Changes in version 0.3.0 (GitHub-only-release date: 28-09-2023)}{
\itemize{
\item Series of fixes based on initial testing phase, see issue #94.
}
}
\section{Changes in version 0.2.0 (GitHub-only-release date: 15-09-2023)}{
\itemize{
\item Embed hbGPS #90
Expand Down
Loading

0 comments on commit ab5c04a

Please sign in to comment.