knitr::opts_chunk$set(echo = TRUE)

# create CLO file required by the Latex template
CTUtemplate::use_ubreportclo(
  # Personal info
  sign = params$author,
  email = params$email,
  job = params$job,
  # Project info
  projnum = params$projnum,
  projname = params$projname,
  # Report info
  version = params$version,
  reporttype = params$reportname
)
# remotes::install_github("CTU-Bern/accrualPlot")
library(accrualPlot)
library(tidyverse)  # for data wrangling, plotting
# secuTrial
library(secuTrialR) # for secutrial databases
zip <- here::here(glue::glue("01_Original_data_{params$projnum}"), "file_name.zip")
raw.data <- read_secuTrial(zip)
dat <- raw.data$cn %>% # might also be called casenodes
  mutate(rando_date = rando_date_var
         , ctr = center_variable
         # , other modifications?
         )
library(httr)       # for redcap
rc.url <- "https://redcap.ctu.unibe.ch/api/"
token <- readLines(file.path(or, "tokens","file-containing-token.txt"))
post.body.list <- list(token = token,
                       content = "record",
                       format = "csv",
                       type = "flat",
                       rawOrLabel = "raw",
                       rawOrLabelHeaders = "raw",
                       eventName = "label",
                       returnFormat = "csv",
                       exportSurveyFields = FALSE,
                       exportDataAccessGroups = FALSE,
                       exportCheckboxLabel = FALSE)
raw.data <- POST(url = rc.url,
                 body = post.body.list)
if(raw.data$status_code == "200"){
  cat(paste0("\n", "Export of records worked!", "\n\n\n"))
  raw.data <- as.character(raw.data)
  con <- textConnection(raw.data)               
  raw.data <- read.csv(con,
                       head = TRUE,
                       na.strings = "",
                       stringsAsFactors = FALSE)
  close(con)
}

dat <- raw.data %>% 
  filter(redcap_event_name == "event-with-rando") %>% 
  mutate(rando_date = as.Date(rando_date_var)
         , ctr = center_variable
         # , other modifications?
         )
# generate demo data
dat <- data.frame(rando_date = Sys.Date() + sample(-100:0, 300, TRUE),
                  center = sample(c("A", "B", "C"), 300, TRUE, prob = c(.5, .4, .1)))

Overall recruitment {-}

acc <- accrual_create_df(dat$rando_date, by = dat$center)

p1 <- plot(acc$Overall, engine = "gg") +
  theme_classic() +
  scale_x_date(labels = function(x) format(x, format = "%d %b %Y")) +
  scale_discrete_manual(values = CTUtemplate::unibeRed(), aesthetics = "colour")

p2 <- plot(acc$Overall, which = "abs", unit = "month", engine = "gg") +
  theme_classic() +
  scale_x_date(labels = function(x) format(x, format = "%b %Y"))

library(patchwork)
p1 + p2

Recruitment by site {-}

p1 <- plot(acc, engine = "gg") +
  theme_classic() +
  scale_x_date(labels = function(x) format(x, format = "%d %b %Y")) +
  scale_color_manual(values = CTUtemplate::unibeRed(seq(.2, 1, length.out = 4))) +
  theme(legend.position = "bottom")

p2 <- plot(acc, which = "abs", unit = "month", engine = "gg") +
  theme_classic() +
  scale_x_date(labels = function(x) format(x, format = "%b %Y")) +
  scale_fill_manual(values = CTUtemplate::unibeRed(seq(.2, 1, length.out = 3))) +
  theme(legend.position = "bottom")

p1 + p2

Predicted end {-}

plot(acc, engine = "gg", which = "pred", target = 400) +
  theme_classic() +
  scale_x_date(labels = function(x) format(x, format = "%d %b %Y")) +
  scale_color_manual(values = CTUtemplate::unibeRed(seq(.2, 1, length.out = 4))) +
  theme(plot.title.position = "plot")


CTU-Bern/CTUtemplate documentation built on Feb. 27, 2025, 7:21 p.m.