Skip to content

Commit 83e5fad

Browse files
authored
Merge pull request CTU-Basel#2 from markomi/refmodules
Refactor modules
2 parents 7ef52e7 + 229579b commit 83e5fad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+534
-356
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Package: shinysse
22
Type: Package
3-
Title: What the Package Does (Title Case)
4-
Version: 0.1.2
3+
Title: Sample size estimation demo shiny app
4+
Version: 0.1.3
55
Author: Milica Markovic
66
Maintainer: Milica Markovic <[email protected]>
77
Description: Shiny package providing a sample size estimation (sse) web app
8-
License: What license is it under?
8+
License: MIT License
99
Depends:
1010
shiny,
1111
shinydashboard,

NAMESPACE

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
export(app_server)
44
export(app_ui)
5-
export(dash)
6-
export(fun_calc_prop)
7-
export(fun_calc_t)
5+
export(calc_prop)
6+
export(calc_t)
87
export(get_header_mint)
8+
export(get_module_registry)
99
export(run_shiny)

R/app.R

Lines changed: 0 additions & 14 deletions
This file was deleted.

R/app_server.R

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
#' Shiny main app server function for shinysse
1+
#' Main Shiny server function for shinysse
22
#'
3-
#' Calls modules defined in app_ui, and passes on relevant data.
3+
#' Calls modules listed in app_ui, and passes on relevant data.
44
#'
55
#' @seealso \code{\link{app_ui}}, \code{\link{run_shiny}}
66
#' @export
77
#'
88
app_server <- function(input, output) {
9-
# data
9+
# fetch calc data from extdata
1010
calc_t_twosample <- readRDS(system.file("extdata", "calc_t_test_twosample.rds", package = "shinysse"))
11-
calc_t_paired <- readRDS(system.file("extdata/calc_t_test_paired.rds", package = "shinysse"))
12-
calc_prop <- readRDS(system.file("extdata/calc_prop_test.rds", package = "shinysse"))
13-
# modules
14-
mod <- fun_get_mods()
15-
callModule(dash, mod$dash)
16-
callModule(sseBasic, mod$sse_basic, calc_t_twosample, calc_t_paired)
17-
callModule(ssePlus, mod$sse_plus, calc_t_twosample)
18-
callModule(ssePlus, mod$sse_plus_paired, calc_t_paired)
19-
callModule(sseProp, mod$sse_prop, calc_prop)
11+
calc_t_paired <- readRDS(system.file("extdata", "calc_t_test_paired.rds", package = "shinysse"))
12+
calc_prop <- readRDS(system.file("extdata", "calc_prop_test.rds", package = "shinysse"))
13+
# get module labels and then call
14+
mod <- get_module_registry()
15+
callModule(sse_intro, mod$sse_intro)
16+
callModule(sse_ttest, mod$sse_ttest, calc_t_twosample, calc_t_paired)
17+
callModule(sse_ttest_plus, mod$sse_ttest_plus, calc_t_twosample, calc_t_paired)
18+
callModule(sse_prop, mod$sse_prop, calc_prop)
2019
}

R/app_ui.R

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
#' Shiny main app UI function for shinysse
1+
#' Main shiny UI function for shinysse
22
#'
3-
#' Controls the general appearance, header, sidebar, body, modules, etc.
3+
#' Controls the general appearance, header, sidebar, body, tabs with modules, etc.
44
#'
5-
#' @seealso \code{\link{app}}
5+
#' @seealso \code{\link{app_server}}
66
#' @export
77
#'
88
app_ui <- function(){
9-
mod <- fun_get_mods()
9+
mod <- get_module_registry()
1010
dashboardPage(
1111
get_header_mint("SSE app"),
1212
dashboardSidebar(
1313
div(p(" ")),
1414
sidebarMenu(
15-
menuItem("Introduction", tabName = mod$dash, icon = icon("info")),
16-
menuItem("Two sample t-Test SSE", tabName = mod$sse_basic, icon = icon("text-size", lib = "glyphicon")),
17-
menuItem("Two sample t-Test SSE + input", tabName = mod$sse_plus, icon = icon("text-size", lib = "glyphicon")),
18-
menuItem("Paired t-Test SSE + input", tabName = mod$sse_plus_paired, icon = icon("text-size", lib = "glyphicon")),
15+
menuItem("Introduction", tabName = mod$sse_intro, icon = icon("info")),
16+
menuItem("Two sample t-Test SSE", tabName = mod$sse_ttest, icon = icon("text-size", lib = "glyphicon")),
17+
menuItem("Two sample t-Test SSE + input", tabName = mod$sse_ttest_plus, icon = icon("text-size", lib = "glyphicon")),
1918
menuItem("Proportion Test SSE", tabName = mod$sse_prop, icon = icon("percentage"))
2019
)
2120
),
@@ -24,11 +23,10 @@ app_ui <- function(){
2423
tags$link(rel = "stylesheet", type = "text/css", href = "www/custom.css")
2524
),
2625
tabItems(
27-
dashUI(mod$dash, label = mod$dash),
28-
sseBasicUI(mod$sse_basic, label = mod$sse_basic),
29-
ssePlusUI(mod$sse_plus, label = mod$sse_plus),
30-
ssePlusUI(mod$sse_plus_paired, label = mod$sse_plus_paired),
31-
ssePropUI(mod$sse_prop, label = mod$sse_prop)
26+
sse_intro_ui(mod$sse_intro, label = mod$sse_intro),
27+
sse_ttest_ui(mod$sse_ttest, label = mod$sse_ttest),
28+
sse_ttest_plus_ui(mod$sse_ttest_plus, label = mod$sse_ttest_plus),
29+
sse_prop_ui(mod$sse_prop, label = mod$sse_prop)
3230
)
3331
)
3432
)

R/fun_calc_prop.R

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
#' Evaluates power -function for power.prop.test
1+
#' Evaluates power functions for power.prop.test
22
#'
3-
#' For all pre-set combinations of n, theta, xi and alpha, this function calculates a list of powPar (package: sse)
4-
#' objects and saves them at a pre-set location in inst/extdata. The function uses the stats::power.prop.test for
5-
#' this purpose. Make sure to re-build the package after running the function, in order to make it available
6-
#' with package installation.
3+
#' For all pre-set combinations of n, theta, xi and alpha, this function calculates a list of powCalc
4+
#' objects and saves the list.
75
#'
8-
#' @return list of calculated powPar objects
6+
#' Input parameters are optional. Defaults:
7+
#' * n <- seq(from = 3, to = 10000, by = 5)
8+
#' * theta <- c(seq(from = 0, to = 0.15, by = 0.0125), seq(from = 0.2, to = 0.5, by = 0.05))
9+
#' * xi <- seq(from = 0.05, to = 1, by = 0.05)
10+
#' * alpha <- c(0.01, 0.025, 0.05)
11+
#' * save_path <- file.path("inst", "extdata", "calc_prop_test.rds")
12+
#' @md
13+
#' @param n numeric vector containing possible range of sample sizes
14+
#' @param theta numeric vector containing possible range of effect sizes
15+
#' @param xi numeric vector containing possible range of proportions
16+
#' @param alpha numeric vector containing possible range of significance levels
17+
#' @param save_path string inidicating where the function output should be saved
18+
#' @return list of calculated powCalc objects
19+
#' @seealso \code{\link{calc_t}}
920
#' @export
1021
#'
11-
fun_calc_prop <- function(){
12-
## define range of significance levels
13-
alpha <- c(0.01, 0.025, 0.05)
14-
## defining the range of n and theta to be evaluated
15-
psi_prop <- powPar(n = seq(from = 3, to = 10000, by = 5), # possible range of sample sizes
16-
theta = c(seq(from = 0, to = 0.15, by = 0.0125),
17-
seq(from = 0.2, to = 0.5, by = 0.05)), # possible range of effect sizes
18-
xi = seq(from = 0.05, to = 1, by = 0.05)) # possible range of proportions
22+
calc_prop <- function(n, theta, xi, alpha, save_path){
23+
if (missing(n)) n <- seq(from = 3, to = 10000, by = 5)
24+
if (missing(theta)) theta <- c(seq(from = 0, to = 0.15, by = 0.0125), seq(from = 0.2, to = 0.5, by = 0.05))
25+
if (missing(xi)) xi <- seq(from = 0.05, to = 1, by = 0.05)
26+
if (missing(alpha)) alpha <- c(0.01, 0.025, 0.05)
27+
if (missing(save_path)) save_path <- file.path("inst", "extdata", "calc_prop_test.rds")
1928

20-
## defining a power-function based on a power.prop.test
29+
## defining power parameters needed for power fun evluation later on
30+
psi_prop <- powPar(n = n, theta = theta, xi = xi)
31+
## defining a power fun based on a power.prop.test and parameters set in powPar
2132
powFuns_prop <- list()
22-
for(i in 1:length(alpha)){
23-
eval(parse(text = paste(paste0("powFuns_prop[[", paste0("'alpha", sub(".", "p", alpha[i], fixed = TRUE)), "']]", " <- function(psi)"),
33+
for (i in 1:length(alpha)){
34+
eval(parse(text = paste(paste0("powFuns_prop[[", paste0("'alpha", sub(".", "p", alpha[i], fixed = TRUE)), "']]",
35+
" <- function(psi)"),
2436
"{",
2537
"p1 = xi(psi)",
2638
"power.prop.test(n = sse::n(psi)/2,",
@@ -31,15 +43,8 @@ fun_calc_prop <- function(){
3143
")$power",
3244
"}", sep = "\n")))
3345
}
34-
35-
## evaulate power-function for all combinations of n, theta, xi and alpha
46+
## evaulate power function for all combinations of n, theta, xi and alpha
3647
calc_prop <- map(powFuns_prop, ~powCalc(psi_prop, .))
37-
38-
## readRDS
39-
calc_prop <- readRDS("calc_prop_test.rds")
40-
calc_prop <- readRDS(system.file("extdata", "calc_prop_test.rds", package = "shinyssepkg"))
41-
## save calcs to be used in the shiny app
42-
save_path <- file.path("inst", "extdata", "calc_prop_test.rds")
4348
saveRDS(calc_prop, save_path)
4449
return(calc_prop)
4550
}

R/fun_calc_t.R

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,53 @@
1-
#' Evaluates power -function for power.t.test
1+
#' Evaluates power functions for power.t.test
22
#'
3-
#' For all pre-set combinations of n, theta, xi and alpha, this function calculates a list of powPar (package: sse)
4-
#' objects and saves them at a pre-set location in inst/extdata. The function uses the stats::power.t.test for
5-
#' this purpose. Make sure to re-build the package after running the function, in order to make it available
3+
#' For all pre-set combinations of n, theta, xi and alpha, this function calculates a list of powCalc
4+
#' objects and saves the list.
65
#'
7-
#' @param type character string specifying the type of t test. options: "two.sample", "one.sample", "paired"
8-
#' @return list of calculated powPar objects
6+
#' Input parameters are optional. Defaults:
7+
#' * n <- seq(from = 5, to = 2000, by = 5)
8+
#' * theta <- seq(from = 1, to = 50, by = 1)
9+
#' * xi <- seq(from = 1, to = 80, by = 1)
10+
#' * alpha <- c(0.01, 0.025, 0.05)
11+
#' * save_path <- file.path("inst", "extdata", "calc_t_test.rds")
12+
#' @md
13+
#' @param n numeric vector containing possible range of sample sizes
14+
#' @param theta numeric vector containing possible range of effect sizes
15+
#' @param xi numeric vector containing possible range of standard deviations
16+
#' @param alpha numeric vector containing possible range of significance levels
17+
#' @param type string of value "two.sample", "one.sample", or "paired"
18+
#' @param save_path string inidicating where the function output should be saved
19+
#' @return list of calculated powCalc objects
20+
#' @seealso \code{\link{calc_prop}}
921
#' @export
1022
#'
11-
fun_calc_t <- function(type = "two.sided"){
12-
print(type)
13-
## define range of significance levels
14-
alpha <- c(0.01, 0.025, 0.05)
23+
calc_t <- function(n, theta, xi, alpha, type, save_path){
24+
if (missing(n)) n <- seq(from = 5, to = 2000, by = 5)
25+
if (missing(theta)) theta <- seq(from = 1, to = 50, by = 1)
26+
if (missing(xi)) xi <- seq(from = 1, to = 80, by = 1)
27+
if (missing(alpha)) alpha <- c(0.01, 0.025, 0.05)
28+
if (missing(type)) type <- "two.sample"
29+
if (missing(save_path)) save_path <- file.path("inst", "extdata", "calc_t_test.rds")
30+
1531
## defining the range of n and theta to be evaluated
16-
psi_t <- powPar(n = seq(from = 5, to = 2000, by = 5), # possible range of sample sizes
17-
theta = seq(from = 1, to = 50, by = 1), # possible range of effect sizes
18-
xi = seq(from = 1, to = 80, by = 1)) # possible range of standard deviations
19-
## defining a power-function based on a power.t.test
32+
psi_t <- powPar(n = n, theta = theta, xi = xi)
33+
## defining a power fun based on a power.t.test
2034
powFuns_t <- list()
21-
for(i in 1:length(alpha)){
22-
eval(parse(text = paste(paste0("powFuns_t[[", paste0("'alpha", sub(".", "p", alpha[i], fixed = TRUE)), "']]", " <- function(psi)"),
23-
"{",
24-
"power.t.test(n = sse::n(psi)/2,",
25-
"delta = theta(psi),",
26-
"sd = xi(psi),",
27-
paste0("sig.level = ", alpha[i], ","),
28-
"power = NULL,",
29-
paste0("type = '", type, "'"),
30-
")$power",
31-
"}", sep = "\n")))
35+
for (i in 1:length(alpha)){
36+
eval(parse(text =
37+
paste(paste0("powFuns_t[[", paste0("'alpha", sub(".", "p", alpha[i], fixed = TRUE)), "']]", " <- function(psi)"),
38+
"{",
39+
"power.t.test(n = sse::n(psi)/2,",
40+
"delta = theta(psi),",
41+
"sd = xi(psi),",
42+
paste0("sig.level = ", alpha[i], ","),
43+
"power = NULL,",
44+
paste0("type = '", type, "'"),
45+
")$power",
46+
"}", sep = "\n")))
3247
}
33-
## evaulate power-function for all combinations of n, theta, xi and alpha
48+
## evaulates power function for all combinations of n, theta, xi and alpha
3449
calc_t <- map(powFuns_t, ~powCalc(psi_t, .))
3550
## save calcs to be used in the shiny app
36-
save_path <- file.path("inst", "extdata", paste0("calc_t_test_", type, ".rds"))
3751
saveRDS(calc_t, save_path)
3852
return(calc_t)
3953
}

R/fun_get_header_mint.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Shinydashboard two-color custom mint header
22
#'
33
#' @param header_title string containing a header title
4-
#' @return shiny.tag list with a custom header with title as defined in header_title
4+
#' @return shiny.tag list with a custom header
55
#' @export
66
#'
77
get_header_mint <- function(header_title){

R/fun_get_mods.R

Lines changed: 0 additions & 9 deletions
This file was deleted.

R/fun_get_module_registry.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#' Retrieves a list of module aliases
2+
#'
3+
#' Module aliases retrieved via get_module_registry() are used to define module IDs in shiny::callModule(),
4+
#' and are also used as tabnames for the sidebar, and shiny UI function, label and id.
5+
#'
6+
#' @return list of strings containing module aliases
7+
#' @seealso \code{\link{app_ui}}, \code{\link{app_server}}
8+
#' @export
9+
#'
10+
get_module_registry <- function(){
11+
mod <- list()
12+
mod$sse_intro <- "sse_intro"
13+
mod$sse_ttest <- "sse_ttest"
14+
mod$sse_ttest_plus <- "sse_ttest_plus"
15+
mod$sse_prop <- "sse_prop"
16+
return(mod)
17+
}

0 commit comments

Comments
 (0)