File: shift_table.Rd

package info (click to toggle)
r-cran-flextable 0.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,264 kB
  • sloc: sh: 15; makefile: 2
file content (165 lines) | stat: -rw-r--r-- 5,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/clintables.R
\name{shift_table}
\alias{shift_table}
\title{Create a shift table}
\usage{
shift_table(
  x,
  cn_visit = "VISIT",
  cn_visit_num = "VISITNUM",
  cn_grade = "LBNRIND",
  cn_usubjid = "USUBJID",
  cn_lab_cat = NA_character_,
  cn_is_baseline = "LBBLFL",
  baseline_identifier = "Y",
  cn_treatment = NA_character_,
  grade_levels = c("LOW", "NORMAL", "HIGH"),
  grade_labels = c("Low", "Normal", "High")
)
}
\arguments{
\item{x}{Laboratory Tests Results data frame.}

\item{cn_visit}{column name containing visit names, default to "VISIT".}

\item{cn_visit_num}{column name containing visit numbers, default to "VISITNUM".}

\item{cn_grade}{column name containing reference range indicators, default to "LBNRIND".}

\item{cn_usubjid}{column name containing unique subject inditifiers, default to "USUBJID".}

\item{cn_lab_cat}{column name containing lab tests or examination names, default to "LBTEST".}

\item{cn_is_baseline}{column name containing baseline flags, default to "LBBLFL".}

\item{baseline_identifier}{baseline flag value to use for baseline
identification. Its default is "Y".}

\item{cn_treatment}{column name containing treatment names, default to \code{NA}.}

\item{grade_levels}{levels to use for reference range indicators}

\item{grade_labels}{labels to use for reference range indicators}
}
\value{
the shift table as a data.frame. Additionnal elements are provided
in attributes:
\itemize{
\item "VISIT_N": count of unique subject id per visits, labs and eventually
treatments. This element is supposed to be used as value for argument
\code{hidden_data} of function \code{tabulator()}.
\item "FUN_VISIT": a utility function to easily turn \emph{visit} column as a factor
column. It should be applied after the shift table creation.
\item "FUN_GRADE": a utility function to easily turn \emph{grade} column as a factor
column. It adds "MISSING/Missing" and "SUM/Sum" at the end of the set of values specified
in arguments \code{grade_levels} and \code{grade_labels}. It should be applied after the shift
table creation.
}
}
\description{
Create a shift table ready to be used with \code{tabulator()}.

The function is transforming a dataset representing some
'Laboratory Tests Results' structured as \emph{CDISC clinical trial
data sets} format to a dataset representing the shift table.

Shift tables are tables used in clinical trial analysis.
They show the progression of change from the baseline, with the progression
often being along time; the number of subjects is displayed in different
range (e.g. low, normal, or high) at baseline and at selected time points
or intervals.
}
\examples{
library(data.table)
library(flextable)

# data simulation ----
USUBJID <- sprintf("01-ABC-\%04.0f", 1:200)
VISITS <- c("SCREENING 1", "WEEK 2", "MONTH 3")
LBTEST <- c("Albumin", "Sodium")

VISITNUM <- seq_along(VISITS)
LBBLFL <- rep(NA_character_, length(VISITNUM))
LBBLFL[1] <- "Y"

VISIT <- data.frame(VISIT = VISITS, VISITNUM = VISITNUM,
  LBBLFL = LBBLFL, stringsAsFactors = FALSE)
labdata <- expand.grid(USUBJID = USUBJID,  LBTEST = LBTEST,
                    VISITNUM = VISITNUM,
                    stringsAsFactors = FALSE)
setDT(labdata)

labdata <- merge(labdata, VISIT, by = "VISITNUM")

subject_elts <- unique(labdata[, .SD, .SDcols = "USUBJID"])
subject_elts <- unique(subject_elts)
subject_elts[, c("TREAT") := list(
  sample(x = c("Treatment", "Placebo"), size = .N, replace = TRUE))]
subject_elts[, c("TREAT"):= list(
  factor(.SD$TREAT, levels = c("Treatment", "Placebo")))]
setDF(subject_elts)
labdata <- merge(labdata, subject_elts,
  by = "USUBJID", all.x = TRUE, all.y = FALSE)
labdata[, c("LBNRIND"):= list(
  sample(x = c("LOW", "NORMAL", "HIGH"), size = .N,
         replace = TRUE, prob = c(.03, .9, .07)))]

setDF(labdata)




# shift table calculation ----

SHIFT_TABLE <- shift_table(
  x = labdata, cn_visit = "VISIT",
  cn_grade = "LBNRIND",
  cn_usubjid = "USUBJID",
  cn_lab_cat = "LBTEST",
  cn_treatment = "TREAT",
  cn_is_baseline = "LBBLFL",
  baseline_identifier = "Y",
  grade_levels = c("LOW", "NORMAL", "HIGH"))

# get attrs for post treatment ----
SHIFT_TABLE_VISIT <- attr(SHIFT_TABLE, "VISIT_N")
visit_as_factor <- attr(SHIFT_TABLE, "FUN_VISIT")
range_as_factor <- attr(SHIFT_TABLE, "FUN_GRADE")

# post treatments ----
SHIFT_TABLE$VISIT = visit_as_factor(SHIFT_TABLE$VISIT)
SHIFT_TABLE$BASELINE = range_as_factor(SHIFT_TABLE$BASELINE)
SHIFT_TABLE$LBNRIND = range_as_factor(SHIFT_TABLE$LBNRIND)

SHIFT_TABLE_VISIT$VISIT = visit_as_factor(SHIFT_TABLE_VISIT$VISIT)

# tabulator ----

my_format <- function(z) {
  formatC(z * 100, digits = 1, format = "f",
          flag = "0", width = 4)
}

tab <- tabulator(
  x = SHIFT_TABLE,
  hidden_data = SHIFT_TABLE_VISIT,
  row_compose = list(
    VISIT = as_paragraph(VISIT, "\n(N=", N_VISIT, ")")
  ),
  rows = c("LBTEST", "VISIT", "BASELINE"),
  columns = c("TREAT", "LBNRIND"),
  `n` = as_paragraph(N),
  `\%` = as_paragraph(as_chunk(PCT, formatter = my_format))
)

# as_flextable ----

ft_1 <- as_flextable(
  x = tab, separate_with = "VISIT",
  label_rows = c(LBTEST = "Lab Test", VISIT = "Visit",
                 BASELINE = "Reference Range Indicator"))

ft_1
}
\concept{tools for clinical reporting}