Skip to content

Commit

Permalink
added some tests and improved description and vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
larann901 committed Sep 26, 2024
1 parent d2f87d0 commit 779c387
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 22 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: mpathr
Title: Handling Data From The M-Path Platform
Title: Handling Data from the 'M-Path' Platform
Version: 0.0.0.9000
Authors@R: c(
person("Merijn", "Mestdagh", email = "merijn.mestdagh@m-path.io", role = c("aut", "cre"),
Expand All @@ -9,7 +9,7 @@ Authors@R: c(
person("Lara", "Navarrete", email = "larann901@gmail.com", role = c("aut"))
)
data: image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAAWElEQVR42mNgGPTAxsZmJsVqQApgmGw1yApwKcQiT7phRBuCzzCSDSHGMKINIeDNmWQlA2IigKJwIssQkHdINgxfmBBtGDEBS3KCxBc7pMQgMYE5c/AXPwAwSX4lV3pTWwAAAABJRU5ErkJggg==
Description: The goal of `mpathr` is to provide with a few utility functions to be able to read and perform some common operations in ESM data collected through the 'm-Path' platform (<https://m-path.io/landing/>).
Description: The goal of `mpathr` is to provide with a few utility functions to be able to read and perform some common operations in ESM data collected through the 'm-Path' platform (<https://m-path.io/landing/>). The package provides functions to read data from 'm-Path', and to calculate response rate in data from Experience Sampling studies.
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/read_mpath.R
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ read_mpath <- function(

cli_warn(c(
"There were problems when reading in the data:",
problems[1:100]
problems
))
}

Expand Down
9 changes: 6 additions & 3 deletions R/response_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#' was answered or not
#' @param participant_col name of the column that stores the participant id
#' (or equivalent)
#' @param time_col optional: name of the column that stores the time of the beep
#' @param period_start optional: period start
#' @param period_end optional: period end
#' @param time_col optional: name of the column that stores the time of the
#' beep, as a 'POSIXct' object.
#' @param period_start string representing the starting date to
#' calculate response rates (optional). Accepts dates in the following
#' formats: \code{yyyy-mm-dd} or\code{yyyy/mm/dd}.
#' @param period_end period end to calculate response rates (optional).
#'
#' @return a data frame with the response rate for each participant,
#' and the number of beeps used to calculate the response rate
Expand Down
Binary file modified data/example_data.rda
Binary file not shown.
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ CMD
ESM
Github
ORCID
POSIXct
SWLS
ggplot
preprocessed
th
yyyy
4 changes: 2 additions & 2 deletions man/mpathr-package.Rd

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

9 changes: 6 additions & 3 deletions man/response_rate.Rd

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

63 changes: 57 additions & 6 deletions tests/testthat/test-read_mpath.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,10 @@ test_that('List columns are being read as lists', {
test_that('List columns are being read as the correct type', {
for (col in meta_list_cols) {
col_type <- meta[meta$columnName == col,]$typeAnswer
if (col_type == 'intList') {
if (col_type %in% c('intList', 'doubleList')) {
expect_true(all(vapply(data[[col]],
function(x) is.integer(x) || is.numeric(x),
function(x) is.numeric(x),
FUN.VALUE = logical(1))))
} else if (col == 'doubleList') {
expect_true(all(vapply(data[[col]],
is.numeric),
FUN.VALUE = logical(1)))
} else if (col == 'stringList') {
expect_true(all(vapply(data[[col]],
is.character,
Expand All @@ -142,3 +138,58 @@ test_that('List columns are being read as the correct type', {
}
})

# Test that problems are being printed when they occur:
# Make small dataset that will result in parsing problems
basic <- data.frame(connectionId = 228325.76, # this should be an int
code = '',
alias = '"example_alias"',
questionListName = '"example_questions"',
timeStampSent = 1722427206,
consent_yesno = 1,
slider_happy = 99)

# Create metadata for the dataset above
meta <- data.frame(columnName = c('"consent_yesno"', '"slider_happy"'),
fullQuestion = c('"Do you consent to participate in this study?"',
'"How happy are you right now?"'),
typeQuestion = c('"yesno"', '"sliderNegPos"'),
typeAnswer = c('"int"', '"int"'),
fullQuestion_mixed = c(0,0),
typeQuestion_mixed = c(0,0),
typeAnswer_mixed = c(0,0))

basic_file <- tempfile(fileext = ".csv")
meta_file <- tempfile(fileext = ".csv")

write.table(basic, basic_file, row.names = FALSE, sep = ";", quote = FALSE)
write.table(meta, meta_file, row.names = FALSE, sep = ";", quote = FALSE)

test_that("Problem with integer is printed", {

expect_warning(read_mpath(file = basic_file,
meta_data = meta_file),
"In row 2 column 1, expected an integer but got 228325.76.")

})

# Problems in meta_data
meta <- data.frame(columnName = c('"consent_yesno"', '"slider_happy"'),
fullQuestion =
c('"Do you consent to participate in this study?"',
'"How happy are you right now?"'),
typeQuestion = c('"yesno"', '"sliderNegPos"'),
typeAnswer = c('"int"', '"int"'),
fullQuestion_mixed = c(0,0),
typeQuestion_mixed = c(10,0),
typeAnswer_mixed = c(0,0))

meta_file <- tempfile(fileext = ".csv")
write.table(meta, meta_file, row.names = FALSE, sep = ";", quote = FALSE)

test_that("Problem with meta_data is printed", {

expect_warning(mpathr:::read_meta_data(meta_file),
"In row 2 column 6, expected 1/0/T/F/TRUE/FALSE but got 10.")

})

14 changes: 9 additions & 5 deletions vignettes/mpathr.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ knitr::opts_chunk$set(
library(mpathr)
```

# mpathr

The main goal of `mpathr` is to provide functions to import data from the m-Path
platform, as well as provide functions for common manipulations for
ESM data.
Expand Down Expand Up @@ -58,7 +56,11 @@ participants' response rate. We provide a function `response_rate` that
calculates the response_rate per participant for the entire duration of the
study, or for a specific time frame.\n

For this, we can use the `example_data`. Which contains data from the same
This function takes as argument a `valid_col`, that takes a logical column that
stores whether the beep was answered by the participant, or not, as well as a
`participant_col`, that identifies each separate participant.

We will show how to use this function with the `example_data`, that contains data from the same
study as the `example_basic.csv` file, but after some cleaning.\n

```{r}
Expand All @@ -85,8 +87,10 @@ response_rates[response_rates$response_rate < 0.5,]

We could also be interested in seeing the participants' response rate during
a specific period of time (for example, if we think a participant's compliance
significantly dropped a certain date). In this case, we can specify the date
period that we are interested in:
significantly dropped a certain date). In this case, we should supply the
function with the (otherwise optional) argument `time_col`, that should contain
times stored as 'POSIXct' objects, and specify the date period that we are
interested in (in the format 'yyyy-mm-dd' or 'yyyy/mm/dd'):

```{r}
response_rates_after_15 <- response_rate(data = example_data,
Expand Down

0 comments on commit 779c387

Please sign in to comment.