Skip to content

Commit bccf767

Browse files
committed
WIP jjbarstats
1 parent ec8f2af commit bccf767

File tree

13 files changed

+160
-197
lines changed

13 files changed

+160
-197
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Suggests:
7575
Remotes:
7676
ndphillips/FFTrees,
7777
easystats/report,
78-
spgarbet/tangram@0.3.2,
78+
spgarbet/tangram,
7979
cran/rmngb
8080
VignetteBuilder:
8181
knitr

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ export(tree)
3434
export(vartree)
3535
import(ggplot2)
3636
import(jmvcore)
37-
import(patchwork)
3837
importFrom(R6,R6Class)
3938
importFrom(crayon,blue)
4039
importFrom(crayon,green)

R/crosstable.b.R

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ crosstableClass <- if (requireNamespace('jmvcore')) R6::R6Class(
1818
# ToDo Message ----
1919

2020
todo <- glue::glue("
21+
<br>
22+
21:02
2123
<br>Welcome to ClinicoPath.<br>
2224
This tool will help you form a Cross Table.<br>
2325
The functions select hypothesis tests automatically. You may see different results with different tables. Please verify your data distribution and appropriateness of the test accordingly. You may find Statkat module useful.<br>
@@ -155,35 +157,22 @@ crosstableClass <- if (requireNamespace('jmvcore')) R6::R6Class(
155157

156158
} else if (sty %in% c("nejm", "lancet", "hmisc")) {
157159

158-
# sty <- jmvcore::composeTerm(components = self$options$sty)
160+
sty <- jmvcore::composeTerm(components = self$options$sty)
159161

160162

161163
# tangram ----
162164

163-
# Recent version, not working ----
164-
165-
# tabletangram <-
166-
# tangram::html5(
167-
# tangram::tangram(
168-
# formula,
169-
# mydata,
170-
# id = "tbl3"
171-
# ),
172-
# style = sty,
173-
# caption = paste0("Cross Table for Dependent ", self$options$group)
174-
# )
175-
176-
# spgarbet/tangram@0.3.2
177-
178-
sty <- self$options$sty
179-
sty <- paste0(sty, ".css")
180165

181166
tabletangram <-
182167
tangram::html5(
183168
tangram::tangram(
184-
formula, mydata),
169+
formula,
170+
mydata,
171+
transform=tangram::hmisc,
172+
id = "tbl3"
173+
),
185174
fragment = TRUE,
186-
inline = sty,
175+
style = sty,
187176
caption = paste0(
188177
"Cross Table for Dependent ",
189178
self$options$group),

R/jjbarstats.b.R

Lines changed: 70 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#'
55
#' @importFrom R6 R6Class
66
#' @import jmvcore
7-
#' @import patchwork
87
#'
98

109

@@ -16,14 +15,15 @@ jjbarstatsClass <- if (requireNamespace('jmvcore')) R6::R6Class(
1615
.run = function() {
1716

1817
# Initial Message ----
19-
if ( is.null(self$options$dep) ) {
20-
# TODO ----
18+
if ( is.null(self$options$dep) || is.null(self$options$group)) {
19+
20+
# TODO ----
2121

2222
todo <- glue::glue(
2323
"
2424
<br>Welcome to ClinicoPath
2525
<br><br>
26-
This tool will help you generate Bar Plots.
26+
This tool will help you generate Bar Charts.
2727
<br><br>
2828
This function uses ggplot2 and ggstatsplot packages. See documentations <a href = 'https://indrajeetpatil.github.io/ggstatsplot/reference/ggbarstats.html' target='_blank'>here</a> and <a href = 'https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggbarstats.html' target='_blank'>here</a>.
2929
<br>
@@ -46,26 +46,6 @@ jjbarstatsClass <- if (requireNamespace('jmvcore')) R6::R6Class(
4646
if (nrow(self$data) == 0)
4747
stop('Data contains no (complete) rows')
4848

49-
50-
51-
52-
53-
54-
mydata <- self$data
55-
mydep <- self$options$dep
56-
57-
mydata <- jmvcore::select(mydata, c(mydep))
58-
59-
60-
61-
62-
63-
todo2 <- head(mydata)
64-
65-
66-
self$results$todo2$setContent(todo2)
67-
68-
6949
}
7050
}
7151

@@ -76,7 +56,7 @@ jjbarstatsClass <- if (requireNamespace('jmvcore')) R6::R6Class(
7656
# the plot function ----
7757
# Error messages ----
7858

79-
if ( is.null(self$options$dep) )
59+
if ( is.null(self$options$dep) || is.null(self$options$group))
8060
return()
8161

8262
if (nrow(self$data) == 0)
@@ -120,27 +100,14 @@ jjbarstatsClass <- if (requireNamespace('jmvcore')) R6::R6Class(
120100

121101
mydep <- mydata[[self$options$dep]]
122102

123-
mydep <- unlist(mydep)
124-
125103
mygroup <- mydata[[self$options$group]]
126104

127-
if ( !is.null(self$options$grvar) ) {
128-
mygrvar <- mydata[[self$options$grvar]]
129-
}
130-
131-
132-
133105

134106
# ggbarstats ----
135107
# bar charts for categorical data
136108
# https://indrajeetpatil.github.io/ggstatsplot/reference/ggbarstats.html
137109

138110

139-
140-
141-
if (length(self$options$dep) == 1 && !is.null(self$options$group)) {
142-
143-
144111
plotData <- data.frame(gr = mygroup,
145112
dp = mydep)
146113

@@ -182,130 +149,93 @@ jjbarstatsClass <- if (requireNamespace('jmvcore')) R6::R6Class(
182149
y = NULL
183150
)
184151

185-
}
186152

153+
# Print Plot ----
187154

155+
print(plot)
156+
TRUE
188157

158+
}
189159

190160

161+
, .plot2 = function(image, ...) {
162+
# the plot function ----
163+
# Error messages ----
191164

165+
if ( is.null(self$options$dep) || is.null(self$options$group) || is.null(self$options$grvar))
166+
return()
192167

193-
# if ((length(self$options$dep) > 1) && !is.null(self$options$group)) {
194-
#
195-
#
196-
#
197-
# # running the same analysis on two different columns (creates a list of plots)
198-
# plotlist <-
199-
# purrr::pmap(
200-
# .l = list(
201-
# data = plotData,
202-
# x = gr,
203-
# y = dp,
204-
#
205-
# messages = FALSE
206-
# ),
207-
# .f = ggstatsplot::ggbarstats
208-
# )
209-
#
210-
# # combine plots using `patchwork`
211-
#
212-
# plot <- plotlist[[1]] + plotlist[[2]]
213-
#
214-
#
215-
# }
168+
if (nrow(self$data) == 0)
169+
stop('Data contains no (complete) rows')
216170

217171

172+
# Prepare Data ----
218173

219174

220175

176+
mydata <- self$data
221177

222178

179+
# Exclude NA ----
223180

181+
excl <- self$options$excl
224182

183+
if (excl) {mydata <- jmvcore::naOmit(mydata)}
225184

226185

227186

228-
# ggbarstats ----
229-
# https://indrajeetpatil.github.io/ggstatsplot/reference/ggbarstats.html
187+
mydep <- mydata[[self$options$dep]]
188+
189+
mygroup <- mydata[[self$options$group]]
190+
191+
# grouped_ggbarstats ----
192+
# https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggbarstats.html
193+
194+
195+
196+
if ( !is.null(self$options$grvar) ) {
197+
mygrvar <- mydata[[self$options$grvar]]
198+
}
199+
200+
201+
if ( !is.null(self$options$grvar) ) {
202+
203+
plotData <- data.frame(gr = mygroup,
204+
dp = mydep,
205+
grvar = mygrvar)
206+
207+
208+
plot2 <- ggstatsplot::grouped_ggbarstats(
209+
data = plotData,
210+
main = dp,
211+
condition = gr,
212+
counts = NULL,
213+
grouping.var = grvar,
214+
title.prefix = NULL,
215+
output = "plot",
216+
x = NULL,
217+
y = NULL,
218+
plotgrid.args = list(),
219+
title.text = NULL,
220+
title.args = list(size = 16, fontface = "bold"),
221+
caption.text = NULL,
222+
caption.args = list(size = 10),
223+
sub.text = NULL,
224+
sub.args = list(size = 12)
225+
)
226+
227+
}
228+
229+
# Print Plot ----
230+
231+
print(plot2)
232+
TRUE
233+
234+
}
235+
230236

231-
# ggbarstats(
232-
# data,
233-
# main,
234-
# condition,
235-
# counts = NULL,
236-
# ratio = NULL,
237-
# paired = FALSE,
238-
# results.subtitle = TRUE,
239-
# sample.size.label = TRUE,
240-
# label = "percentage",
241-
# perc.k = 0,
242-
# label.args = list(alpha = 1, fill = "white"),
243-
# bf.message = TRUE,
244-
# sampling.plan = "indepMulti",
245-
# fixed.margin = "rows",
246-
# prior.concentration = 1,
247-
# title = NULL,
248-
# subtitle = NULL,
249-
# caption = NULL,
250-
# conf.level = 0.95,
251-
# nboot = 100,
252-
# legend.title = NULL,
253-
# xlab = NULL,
254-
# ylab = NULL,
255-
# k = 2,
256-
# proportion.test = TRUE,
257-
# ggtheme = ggplot2::theme_bw(),
258-
# ggstatsplot.layer = TRUE,
259-
# package = "RColorBrewer",
260-
# palette = "Dark2",
261-
# ggplot.component = NULL,
262-
# output = "plot",
263-
# messages = TRUE,
264-
# x = NULL,
265-
# y = NULL,
266-
# ...
267-
# )
268-
269-
270-
271-
# grouped_ggbarstats ----
272-
# https://indrajeetpatil.github.io/ggstatsplot/reference/grouped_ggbarstats.html
273-
274-
275-
# if ( !is.null(self$options$grvar) ) {
276-
#
277-
# plotData <- data.frame(gr = mygroup,
278-
# dp = mydep,
279-
# grvar = mygrvar )
280-
#
281-
#
282-
#
283-
# plot <- ggstatsplot::grouped_ggbarstats(
284-
# data = plotData,
285-
# main = gr,
286-
# condition = dp,
287-
# counts = NULL,
288-
# grouping.var = grvar,
289-
# title.prefix = NULL,
290-
# output = "plot",
291-
# x = NULL,
292-
# y = NULL,
293-
# plotgrid.args = list(),
294-
# title.text = NULL,
295-
# title.args = list(size = 16, fontface = "bold"),
296-
# caption.text = NULL,
297-
# caption.args = list(size = 10),
298-
# sub.text = NULL,
299-
# sub.args = list(size = 12)
300-
# )
301-
#
302-
# }
303237

304-
# Print Plot ----
305238

306-
print(plot)
307-
TRUE
308239

309-
}
310240
)
311241
)

0 commit comments

Comments
 (0)