Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style coe #8

Open
wants to merge 1 commit into
base: task6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions R/f_assertions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

154 changes: 77 additions & 77 deletions R/f_simulate_continuous_data.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#'
#' @title
#' Get Simluated Two Arm Means
Expand All @@ -12,63 +11,64 @@
#'
#' @details
#' TODO describe
#'
#'
#' @return a list with all arguments and results; the ouput is defined as a class with name 'SimulationResult'.
#'
#' @examples
#' getSimulatedTwoArmMeans(n1 = 50, n2 = 50, mean1 = 5, mean2 = 7, sd1 = 3, sd2 = 4, seed = 123)
#'
#' @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)
}

#'
Expand All @@ -80,23 +80,24 @@ 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)
#'
#' @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)
), ...)
}
Expand All @@ -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, ...)
}

#'
Expand All @@ -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
)
}


Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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, ...)
}

Expand Down
2 changes: 1 addition & 1 deletion R/f_utilities.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
##
## Insert all helper/utility functions here
##
##
16 changes: 7 additions & 9 deletions R/pkgname.R
Original file line number Diff line number Diff line change
@@ -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"


10 changes: 5 additions & 5 deletions tests/testthat/test_f_assertions.R
Original file line number Diff line number Diff line change
@@ -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"
)
})
23 changes: 9 additions & 14 deletions tests/testthat/test_f_simluate_continuous_data.R
Original file line number Diff line number Diff line change
@@ -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
})




2 changes: 0 additions & 2 deletions tests/testthat/test_f_utilities.R
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@