diff --git a/R/f_assertions.R b/R/f_assertions.R index 6ff9829..977c215 100644 --- a/R/f_assertions.R +++ b/R/f_assertions.R @@ -3,8 +3,7 @@ ## assertPackageIsInstalled <- function(packageName) { - if (!requireNamespace(packageName, quietly = TRUE)) { - stop("Package ", sQuote(packageName), " is needed for this function to work") - } + if (!requireNamespace(packageName, quietly = TRUE)) { + stop("Package ", sQuote(packageName), " is needed for this function to work") + } } - diff --git a/R/f_simulate_continuous_data.R b/R/f_simulate_continuous_data.R index f8a9954..b74a61f 100644 --- a/R/f_simulate_continuous_data.R +++ b/R/f_simulate_continuous_data.R @@ -1,4 +1,3 @@ - #' #' @title #' Get Simluated Two Arm Means @@ -12,7 +11,7 @@ #' #' @details #' TODO describe -#' +#' #' @return a list with all arguments and results; the ouput is defined as a class with name 'SimulationResult'. #' #' @examples @@ -20,55 +19,56 @@ #' #' @export #' -getSimulatedTwoArmMeans <- function( - n1, - n2, - mean1, - mean2, - sd1, - sd2, - alternative = c("two.sided", "less", "greater"), - ..., - seed = NA_integer_) { - - # TODO use assertions to check all input arguments - checkmate::assertInt(n1, lower = 1) - checkmate::assertInt(n2, lower = 1) - - if (is.na(seed)) { - # TODO create a new seed if it is NA, i.e., it is undefined - seed = 1 - } - - # specify seed - if (!is.na(seed)) { - set.seed(seed) - } - - # TODO create normal distributed random data for the two groups - - # TODO save the fake data to a data frame in long format - - # TODO put all arguments and results to a list - result <- list(n1 = n1, n2 = n2, - mean1 = mean1, mean2 = mean2, sd1 = sd1, sd2 = sd2) - result$data <- data.frame( - group = c(rep(1, n1), rep(2, n2)), - values = c( - rnorm(n = n1, mean = mean1, sd = sd1), - rnorm(n = n2, mean = mean2, sd = sd2) - ) +getSimulatedTwoArmMeans <- function(n1, + n2, + mean1, + mean2, + sd1, + sd2, + alternative = c("two.sided", "less", "greater"), + ..., + seed = NA_integer_) { + # TODO use assertions to check all input arguments + checkmate::assertInt(n1, lower = 1) + checkmate::assertInt(n2, lower = 1) + + if (is.na(seed)) { + # TODO create a new seed if it is NA, i.e., it is undefined + seed <- 1 + } + + # specify seed + if (!is.na(seed)) { + set.seed(seed) + } + + # TODO create normal distributed random data for the two groups + + # TODO save the fake data to a data frame in long format + + # TODO put all arguments and results to a list + result <- list( + n1 = n1, n2 = n2, + mean1 = mean1, mean2 = mean2, sd1 = sd1, sd2 = sd2 + ) + result$data <- data.frame( + group = c(rep(1, n1), rep(2, n2)), + values = c( + rnorm(n = n1, mean = mean1, sd = sd1), + rnorm(n = n2, mean = mean2, sd = sd2) ) - - result$n_total = result$n1 + result$n2 - result$allocation_ratio = result$n1 / result$n2 - result$creation_time = Sys.time() - result$t.test = t.test(result$data$values[result$data$group == 1], - result$data$values[result$data$group == 2], - alternative = alternative) - # set the class attribute - result <- structure(result, class = "SimulationResult") - return(result) + ) + + result$n_total <- result$n1 + result$n2 + result$allocation_ratio <- result$n1 / result$n2 + result$creation_time <- Sys.time() + result$t.test <- t.test(result$data$values[result$data$group == 1], + result$data$values[result$data$group == 2], + alternative = alternative + ) + # set the class attribute + result <- structure(result, class = "SimulationResult") + return(result) } #' @@ -80,7 +80,7 @@ getSimulatedTwoArmMeans <- function( #' #' @param x a \code{SimulationResult} object to print. #' @param ... further arguments passed to or from other methods. -#' +#' #' @examples #' x <- getSimulatedTwoArmMeans(n1 = 50, n2 = 50, mean1 = 5, mean2 = 7, sd1 = 3, sd2 = 4, seed = 123) #' print(x) @@ -88,15 +88,16 @@ getSimulatedTwoArmMeans <- function( #' @export #' print.SimulationResult <- function(x, ...) { - args <- list(n1 = x$n1, n2 = x$n2, - mean1 = x$mean1, mean2 = x$mean2, - sd1 = x$sd1, sd2 = x$sd2 - ) - + args <- list( + n1 = x$n1, n2 = x$n2, + mean1 = x$mean1, mean2 = x$mean2, + sd1 = x$sd1, sd2 = x$sd2 + ) + print(list( - args = format(args), + args = format(args), n_total = x$n_total, - create_time = format(Sys.time(), '%B %d, %Y'), + create_time = format(Sys.time(), "%B %d, %Y"), data = dplyr::tibble(x$data) ), ...) } @@ -113,12 +114,12 @@ print.SimulationResult <- function(x, ...) { #' #' @examples #' getSimulatedTwoArmMeans(n1 = 50, n2 = 50, mean1 = 5, mean2 = 7, sd1 = 3, sd2 = 4, seed = 123) -#' +#' #' @export #' showDefault.SimulationResult <- function(x, ...) { - # TODO optionally change the default output function - print(x = x, ...) + # TODO optionally change the default output function + print(x = x, ...) } #' @@ -145,21 +146,20 @@ showDefault.SimulationResult <- function(x, ...) { #' if (require(ggplot2)) plot(x) #' #' @importFrom rlang .data -#' +#' #' @export #' -plot.SimulationResult <- function( - x, - ..., - main = "Continuous Fake Data", - xlab = "Group", - ylab = "Simulated Values") { - +plot.SimulationResult <- function(x, + ..., + main = "Continuous Fake Data", + xlab = "Group", + ylab = "Simulated Values") { # TODO implement the plot function - boxplot(x$data$values ~ x$data$group, - main = main, - xlab = xlab, - ylab = ylab) + boxplot(x$data$values ~ x$data$group, + main = main, + xlab = xlab, + ylab = ylab + ) } @@ -172,7 +172,7 @@ plot.SimulationResult <- function( #' #' @param x a \code{SimulationResult} object to summarise #' @param ... further arguments passed to or from other methods. -#' +#' #' @examples #' x <- getSimulatedTwoArmMeans(n1 = 50, n2 = 50, mean1 = 5, mean2 = 7, sd1 = 3, sd2 = 4, seed = 123) #' summary(x) @@ -181,7 +181,7 @@ plot.SimulationResult <- function( #' summary.SimulationResult <- function(x, ...) { simplelm <- lm(x$data$values ~ x$data$group) - + lapply(list( data = x$data, lm = simplelm @@ -198,14 +198,14 @@ summary.SimulationResult <- function(x, ...) { #' #' @param x a \code{SimulationResult} object to summarise #' @param ... further arguments passed to or from other methods. -#' +#' #' @examples #' x <- getSimulatedTwoArmMeans(n1 = 50, n2 = 50, mean1 = 5, mean2 = 7, sd1 = 3, sd2 = 4, seed = 123) #' kable(x) #' #' @export #' -kable.SimulationResult <- function(x, ...){ +kable.SimulationResult <- function(x, ...) { knitr::kable(x$data, ...) } diff --git a/R/f_utilities.R b/R/f_utilities.R index ea59136..b82ae81 100644 --- a/R/f_utilities.R +++ b/R/f_utilities.R @@ -1,3 +1,3 @@ ## ## Insert all helper/utility functions here -## \ No newline at end of file +## diff --git a/R/pkgname.R b/R/pkgname.R index d2459b4..51f6416 100644 --- a/R/pkgname.R +++ b/R/pkgname.R @@ -1,25 +1,23 @@ -#' +#' #' @title -#' simulatr - Generator for Reproducible Fake Data -#' +#' simulatr - Generator for Reproducible Fake Data +#' #' @description #' An example package for learning about software development best-practices for R packages. -#' +#' #' @details #' For more information please visit \href{https://kkmann.github.io/workshop-r-swe}{kkmann.github.io/workshop-r-swe}. -#' +#' #' @docType package #' @author Friedrich Pahlke, Daniel Sabanes Bove, Kevin Kunzmann #' @name simulatr -#' +#' #' @import methods #' @import stats #' @import utils #' @import graphics #' @import tools #' @import checkmate -#' +#' "_PACKAGE" #> [1] "_PACKAGE" - - diff --git a/tests/testthat/test_f_assertions.R b/tests/testthat/test_f_assertions.R index 5b1d851..ecf5c4b 100644 --- a/tests/testthat/test_f_assertions.R +++ b/tests/testthat/test_f_assertions.R @@ -1,7 +1,7 @@ - - test_that("'assertPackageIsInstalled' works as expected", { - expect_no_error(assertPackageIsInstalled("stats")) - expect_error(assertPackageIsInstalled("notExistingPackage"), - "Package 'notExistingPackage' is needed for this function to work") + expect_no_error(assertPackageIsInstalled("stats")) + expect_error( + assertPackageIsInstalled("notExistingPackage"), + "Package 'notExistingPackage' is needed for this function to work" + ) }) diff --git a/tests/testthat/test_f_simluate_continuous_data.R b/tests/testthat/test_f_simluate_continuous_data.R index 7a75631..5c6982a 100644 --- a/tests/testthat/test_f_simluate_continuous_data.R +++ b/tests/testthat/test_f_simluate_continuous_data.R @@ -1,25 +1,20 @@ - test_that("For invalid input arguments 'getSimulatedTwoArmMeans' throws meaningful exceptions", { - expect_error( - getSimulatedTwoArmMeans(), - 'argument "n1" is missing, with no default' - ) - - # TODO implement tests for all arguments + expect_error( + getSimulatedTwoArmMeans(), + 'argument "n1" is missing, with no default' + ) + + # TODO implement tests for all arguments }) test_that("With defined seed 'getSimulatedTwoArmMeans' returns reproducible results", { - # TODO implement tests + # TODO implement tests }) test_that("With undefined seed 'getSimulatedTwoArmMeans' returns valid results", { - # TODO implement tests + # TODO implement tests }) test_that("'getSimulatedTwoArmMeans' result can be printed", { - # TODO implement tests + # TODO implement tests }) - - - - diff --git a/tests/testthat/test_f_utilities.R b/tests/testthat/test_f_utilities.R index 139597f..e69de29 100644 --- a/tests/testthat/test_f_utilities.R +++ b/tests/testthat/test_f_utilities.R @@ -1,2 +0,0 @@ - -