Compare commits

...

6 Commits

12 changed files with 170 additions and 14 deletions

View File

@ -1,6 +1,6 @@
Package: REDCapCAST Package: REDCapCAST
Title: REDCap Castellated Data Handling Title: REDCap Castellated Data Handling
Version: 24.1.2 Version: 24.2.1
Authors@R: c( Authors@R: c(
person("Andreas Gammelgaard", "Damsbo", email = "agdamsbo@clin.au.dk", person("Andreas Gammelgaard", "Damsbo", email = "agdamsbo@clin.au.dk",
role = c("aut", "cre"),comment = c(ORCID = "0000-0002-7559-1154")), role = c("aut", "cre"),comment = c(ORCID = "0000-0002-7559-1154")),
@ -34,7 +34,9 @@ Suggests:
ggplot2, ggplot2,
haven, haven,
here, here,
styler styler,
devtools,
roxygen2
License: GPL (>= 3) License: GPL (>= 3)
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true
@ -49,13 +51,17 @@ Imports:
keyring, keyring,
purrr, purrr,
readr, readr,
stats stats,
calendar,
lubridate,
glue
Collate: Collate:
'utils.r' 'utils.r'
'process_user_input.r' 'process_user_input.r'
'REDCap_split.r' 'REDCap_split.r'
'ds2dd.R' 'ds2dd.R'
'ds2dd_detailed.R' 'ds2dd_detailed.R'
'ds2ical.R'
'easy_redcap.R' 'easy_redcap.R'
'read_redcap_instrument.R' 'read_redcap_instrument.R'
'read_redcap_tables.R' 'read_redcap_tables.R'

View File

@ -5,6 +5,7 @@ export(clean_redcap_name)
export(d2w) export(d2w)
export(ds2dd) export(ds2dd)
export(ds2dd_detailed) export(ds2dd_detailed)
export(ds2ical)
export(easy_redcap) export(easy_redcap)
export(focused_metadata) export(focused_metadata)
export(get_api_key) export(get_api_key)

View File

@ -1,13 +1,17 @@
# REDCapCAST 24.1.2 # REDCapCAST 24.2.1
### Functions ### Functions
* Fix: `ds2dd()`: uses correct default dd column names. Will be deprecated. * Fix: `ds2dd()`: uses correct default dd column names. Will be deprecated.
* Fix: `easy_redcap()`: fixed to actually allow project naming. also specifically asks for uri.
* NEW: `ds2dd_detailed()`: extension of the `ds2dd()`, which serves to preserve as much metadata as possible automatically. Depends on a group of helper functions also introduced. Of special note is the `guess_time_only_filter()`, which will try to guess which columns/variables should be formatted as time only formats. Supports hms time format. DETAILED INSTRUCTION AND VIGNETTE IS PENDING. * NEW: `ds2dd_detailed()`: extension of the `ds2dd()`, which serves to preserve as much metadata as possible automatically. Depends on a group of helper functions also introduced. Of special note is the `guess_time_only_filter()`, which will try to guess which columns/variables should be formatted as time only formats. Supports hms time format. DETAILED INSTRUCTION AND VIGNETTE IS PENDING.
* NEW: `read_redcap_instrument()` convenience function to retrieve complete instrument. Goes a little against the focused approach. DETAILED INSTRUCTION IS PENDING. * NEW: `read_redcap_instrument()` convenience function to retrieve complete instrument. Goes a little against the focused approach. DETAILED INSTRUCTION IS PENDING.
* NEW: `ds2ical()` converts data set to ical format with easy glue string for summary and description. Export ics file with `calendar::ic_write()`.
### Other ### Other
I believe `renv` has now been added and runs correctly. After clone, do `renv::restore()` to install all necessary package to modify the package. I believe `renv` has now been added and runs correctly. After clone, do `renv::restore()` to install all necessary package to modify the package.

59
R/ds2ical.R Normal file
View File

@ -0,0 +1,59 @@
utils::globalVariables(c("DTSTART"))
#' Convert data set to ical file
#'
#' @param data data set
#' @param start event start column
#' @param location event location column
#' @param event.length use lubridate functions to generate "Period" class
#' element (default is lubridate::hours(2))
#' @param summary.glue.string character string to pass to glue::glue() for event
#' name (summary). Can take any column from data set.
#' @param description.glue.string character string to pass to glue::glue() for
#' event description. Can take any column from data set.
#'
#' @return tibble of class "ical"
#' @export
#'
#' @examples
#' df <- dplyr::tibble(start = c(Sys.time(), Sys.time() + lubridate::days(2)),
#' id = c("1", 3), assessor = "A", location = "111", note = c(NA, "OBS"))
#' df |> ds2ical(start, location)
#' df |> ds2ical(start, location,
#' summary.glue.string = "ID {id} [{assessor}] {note}")
#' # Export .ics file: (not run)
#' ical <- df |> ds2ical(start, location, description.glue.string = "{note}")
#' # ical |> calendar::ic_write(file=here::here("calendar.ics"))
ds2ical <- function(data,
start,
location,
summary.glue.string = "ID {id} [{assessor}]",
description.glue.string = NULL,
event.length = lubridate::hours(2)) {
ds <- data |>
dplyr::transmute(
SUMMARY = glue::glue(summary.glue.string, .na = ""),
DTSTART = lubridate::ymd_hms({{ start }}, tz = "CET"),
DTEND = DTSTART + event.length,
LOCATION = {{ location }}
)
if (!is.null(description.glue.string)){
ds <- dplyr::tibble(ds,
dplyr::transmute(data,
DESCRIPTION = glue::glue(
description.glue.string,
.na = ""
)
)
)
}
ds |>
(\(x){
x |>
dplyr::mutate(UID = replicate(nrow(x), calendar::ic_guid()))
})() |>
dplyr::filter(!is.na(DTSTART)) |>
calendar::ical()
}

View File

@ -21,19 +21,19 @@ get_api_key <- function(key.name) {
#' @param project.name The name of the current project (for key storage with #' @param project.name The name of the current project (for key storage with
#' `keyring::key_set()`) #' `keyring::key_set()`)
#' @param widen.data argument to widen the exported data #' @param widen.data argument to widen the exported data
#' @param uri REDCap database API uri
#' @param ... arguments passed on to `REDCapCAST::read_redcap_tables()` #' @param ... arguments passed on to `REDCapCAST::read_redcap_tables()`
#' #'
#' @return data.frame or list depending on widen.data #' @return data.frame or list depending on widen.data
#' @importFrom purrr reduce #' @importFrom purrr reduce
#' @importFrom dplyr left_join #' @importFrom dplyr left_join
#' @export #' @export
easy_redcap <- function(project.name, widen.data = TRUE, ...) { easy_redcap <- function(project.name, widen.data = TRUE, uri, ...) {
project.name <- "ENIGMA"
key <- get_api_key(key.name = paste0(project.name, "_REDCAP_API")) key <- get_api_key(key.name = paste0(project.name, "_REDCAP_API"))
out <- read_redcap_tables( out <- read_redcap_tables(
token = key, token = key,
uri = uri,
... ...
) )

View File

@ -1,7 +1,7 @@
#' Convenience function to download complete instrument, using token storage in keyring. #' Convenience function to download complete instrument, using token storage in keyring.
#' #'
#' @param key key name in standard keyring for token retrieval. #' @param key key name in standard keyring for token retrieval.
#' @param uri redcap api url #' @param uri REDCap database API uri
#' @param instrument instrument name #' @param instrument instrument name
#' @param raw_or_label raw or label passed to `REDCapR::redcap_read()` #' @param raw_or_label raw or label passed to `REDCapR::redcap_read()`
#' @param id_name id variable name. Default is "record_id". #' @param id_name id variable name. Default is "record_id".
@ -11,7 +11,7 @@
#' @export #' @export
read_redcap_instrument <- function(key, read_redcap_instrument <- function(key,
uri, uri,
instrument = "rbans", instrument,
raw_or_label = "raw", raw_or_label = "raw",
id_name = "record_id", id_name = "record_id",
records = NULL) { records = NULL) {

View File

@ -5,7 +5,7 @@
#' events using the built-in focused_metadata including some clean-up. #' events using the built-in focused_metadata including some clean-up.
#' Works with classical and longitudinal projects with or without repeating #' Works with classical and longitudinal projects with or without repeating
#' instruments. #' instruments.
#' @param uri REDCap database uri #' @param uri REDCap database API uri
#' @param token API token #' @param token API token
#' @param records records to download #' @param records records to download
#' @param fields fields to download #' @param fields fields to download

47
man/ds2ical.Rd Normal file
View File

@ -0,0 +1,47 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ds2ical.R
\name{ds2ical}
\alias{ds2ical}
\title{Convert data set to ical file}
\usage{
ds2ical(
data,
start,
location,
summary.glue.string = "ID {id} [{assessor}]",
description.glue.string = NULL,
event.length = lubridate::hours(2)
)
}
\arguments{
\item{data}{data set}
\item{start}{event start column}
\item{location}{event location column}
\item{summary.glue.string}{character string to pass to glue::glue() for event
name (summary). Can take any column from data set.}
\item{description.glue.string}{character string to pass to glue::glue() for
event description. Can take any column from data set.}
\item{event.length}{use lubridate functions to generate "Period" class
element (default is lubridate::hours(2))}
}
\value{
tibble of class "ical"
}
\description{
Convert data set to ical file
}
\examples{
df <- dplyr::tibble(start = c(Sys.time(), Sys.time() + lubridate::days(2)),
id = c("1", 3), assessor = "A", location = "111", note = c(NA, "OBS"))
df |> ds2ical(start, location)
df |> ds2ical(start, location,
summary.glue.string = "ID {id} [{assessor}] {note}")
# Export .ics file: (not run)
ical <- df |> ds2ical(start, location, description.glue.string = "{note}")
# ical |> calendar::ic_write(file=here::here("calendar.ics"))
}

View File

@ -4,7 +4,7 @@
\alias{easy_redcap} \alias{easy_redcap}
\title{Secure API key storage and data acquisition in one} \title{Secure API key storage and data acquisition in one}
\usage{ \usage{
easy_redcap(project.name, widen.data = TRUE, ...) easy_redcap(project.name, widen.data = TRUE, uri, ...)
} }
\arguments{ \arguments{
\item{project.name}{The name of the current project (for key storage with \item{project.name}{The name of the current project (for key storage with
@ -12,6 +12,8 @@ easy_redcap(project.name, widen.data = TRUE, ...)
\item{widen.data}{argument to widen the exported data} \item{widen.data}{argument to widen the exported data}
\item{uri}{REDCap database API uri}
\item{...}{arguments passed on to `REDCapCAST::read_redcap_tables()`} \item{...}{arguments passed on to `REDCapCAST::read_redcap_tables()`}
} }
\value{ \value{

View File

@ -7,7 +7,7 @@
read_redcap_instrument( read_redcap_instrument(
key, key,
uri, uri,
instrument = "rbans", instrument,
raw_or_label = "raw", raw_or_label = "raw",
id_name = "record_id", id_name = "record_id",
records = NULL records = NULL
@ -16,7 +16,7 @@ read_redcap_instrument(
\arguments{ \arguments{
\item{key}{key name in standard keyring for token retrieval.} \item{key}{key name in standard keyring for token retrieval.}
\item{uri}{redcap api url} \item{uri}{REDCap database API uri}
\item{instrument}{instrument name} \item{instrument}{instrument name}

View File

@ -18,7 +18,7 @@ read_redcap_tables(
) )
} }
\arguments{ \arguments{
\item{uri}{REDCap database uri} \item{uri}{REDCap database API uri}
\item{token}{API token} \item{token}{API token}

View File

@ -93,6 +93,19 @@
], ],
"Hash": "9fe98599ca456d6552421db0d6772d8f" "Hash": "9fe98599ca456d6552421db0d6772d8f"
}, },
"calendar": {
"Package": "calendar",
"Version": "0.0.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"lubridate",
"methods",
"tibble"
],
"Hash": "ab478f470a5203ff9990e42d916005b0"
},
"checkmate": { "checkmate": {
"Package": "checkmate", "Package": "checkmate",
"Version": "2.3.1", "Version": "2.3.1",
@ -297,6 +310,19 @@
], ],
"Hash": "b8552d117e1b808b09a832f589b79035" "Hash": "b8552d117e1b808b09a832f589b79035"
}, },
"lubridate": {
"Package": "lubridate",
"Version": "1.9.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"generics",
"methods",
"timechange"
],
"Hash": "680ad542fbcf801442c83a6ac5a2126c"
},
"magrittr": { "magrittr": {
"Package": "magrittr", "Package": "magrittr",
"Version": "2.0.3", "Version": "2.0.3",
@ -549,6 +575,17 @@
], ],
"Hash": "79540e5fcd9e0435af547d885f184fd5" "Hash": "79540e5fcd9e0435af547d885f184fd5"
}, },
"timechange": {
"Package": "timechange",
"Version": "0.3.0",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"cpp11"
],
"Hash": "c5f3c201b931cd6474d17d8700ccb1c8"
},
"tzdb": { "tzdb": {
"Package": "tzdb", "Package": "tzdb",
"Version": "0.4.0", "Version": "0.4.0",