Skip to content

Commit

Permalink
fct_CRD-print_summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Prof-ThiagoOliveira committed Mar 19, 2021
1 parent a14562c commit 5e35eb5
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 40 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated by roxygen2: do not edit by hand

S3method(print,CRD)
S3method(summary,CRD)
export("%>%")
export(CRD)
export(RCBD)
Expand All @@ -21,6 +23,7 @@ export(square_lattice)
export(strip_plot)
import(shiny)
importFrom(config,get)
importFrom(dplyr,glimpse)
importFrom(golem,activate_js)
importFrom(golem,add_resource_path)
importFrom(golem,bundle_resources)
Expand All @@ -43,5 +46,7 @@ importFrom(stats,rnorm)
importFrom(stats,runif)
importFrom(stats,sd)
importFrom(utils,capture.output)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(utils,str)
importFrom(utils,write.csv)
169 changes: 141 additions & 28 deletions R/fct_CRD.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Generates a Completely Randomized Design (CRD)
#'
#'
#' @description It randomly generates a completely randomized design.
#'
#' @param t An integer number with total number of treatments or a vector of dimension t with labels.
Expand All @@ -8,55 +8,55 @@
#' @param locationName (optional) Name of the location.
#' @param seed (optional) Real number that specifies the starting seed to obtain reproducible designs.
#' @param data (optional) Data frame with the 2 columns with labels of each treatments and its number of replicates.
#'
#'
#' @importFrom stats runif na.omit
#'
#'
#' @return A list with information on the design parameters.
#' @return Data frame with the CRD field book.
#'
#'
#'
#' @references
#' Federer, W. T. (1955). Experimental Design. Theory and Application. New York, USA. The
#' Macmillan Company.
#'
#'
#' @examples
#' # Example 1: Generates a CRD design with 10 treatments and 5 reps each.
#' crd1 <- CRD(t = 10,
#' reps = 5,
#' plotNumber = 101,
#' seed = 1987,
#' crd1 <- CRD(t = 10,
#' reps = 5,
#' plotNumber = 101,
#' seed = 1987,
#' locationName = "Fargo")
#' crd1$infoDesign
#' head(crd1$fieldBook,10)
#'
#'
#' # Example 2: Generates a CRD design with 15 treatments and 6 reps each.
#' Gens <- paste("Wheat", 1:15, sep = "")
#' crd2 <- CRD(t = Gens,
#' reps = 6,
#' plotNumber = 1001,
#' seed = 1654,
#' crd2 <- CRD(t = Gens,
#' reps = 6,
#' plotNumber = 1001,
#' seed = 1654,
#' locationName = "Fargo")
#' crd2$infoDesign
#' head(crd2$fieldBook,10)
#'
#'
#' # Example 3: Generates a CRD design with 12 treatments and 4 reps each.
#' # In this case, we show how to use the option data.
#' treatments <- paste("ND-", 1:12, sep = "")
#' treatment_list <- data.frame(list(TREATMENT = treatments, REP = 4))
#' head(treatment_list)
#' crd3 <- CRD(t = NULL,
#' reps = NULL,
#' plotNumber = 2001,
#' seed = 1655,
#' crd3 <- CRD(t = NULL,
#' reps = NULL,
#' plotNumber = 2001,
#' seed = 1655,
#' locationName = "Cali",
#' data = treatment_list)
#' crd3$infoDesign
#' head(crd3$fieldBook,10)
#'
#' @export
CRD <- function(t = NULL, reps = NULL, plotNumber = 101, locationName = NULL,
CRD <- function(t = NULL, reps = NULL, plotNumber = 101, locationName = NULL,
seed = NULL, data = NULL) {

if (is.null(seed) || !is.numeric(seed)) seed <- runif(1, min = -50000, max = 50000)
if (!is.null(plotNumber)) {
if (plotNumber < 1 || plotNumber %% 1 != 0) shiny::validate('plotNumber must be an integer greater than 0.')
Expand Down Expand Up @@ -106,9 +106,10 @@ CRD <- function(t = NULL, reps = NULL, plotNumber = 101, locationName = NULL,
nt <- length(data$Treatment)
reps <- as.vector(data$Reps)
}

design <- data.frame(list(LOCATION = rep(locationName, N)), PLOT = sample(plotNumber:(plotNumber + N - 1)),
REP = REP, TREATMENT = TRT)

design <- data.frame(list(LOCATION = rep(locationName, N)),
PLOT = sample(plotNumber:(plotNumber + N - 1)),
REP = as.factor(REP), TREATMENT = TRT)
design <- design[order(design$PLOT),]
id <- 1:nrow(design)
design <- cbind(id, design)
Expand All @@ -118,7 +119,119 @@ CRD <- function(t = NULL, reps = NULL, plotNumber = 101, locationName = NULL,
TRT <- levels(factor(TRT, as.character(unique(TRT))))
parameters <- list(numberofTreatments = nt, treatments = TRT, Reps = reps, locationName = locationName,
seed = seed)

return(list(infoDesign = parameters, fieldBook = design))

}
CRD <- list(infoDesign = parameters, fieldBook = design)
class(CRD) <- "CRD"
return(invisible(CRD))
}

#-----------------------------------------------------------------------
# Print
#-----------------------------------------------------------------------
#' @rdname print.CRD
#' @method print CRD
#' @title Print an \code{CRD} object
#' @usage \method{print}{CRD}(x, n, ...)
#' @aliases print.CRD
#' @description Prints information about randomization of Completely
#' Randomized Design (CDR)
#' @return an object inheriting from class \code{CRD}
#' @param x an object inheriting from class
#' @param n a single integer...
#' \code{\link[FielDHub]{CRD}}
#'
#' @param ... further arguments passed to \code{\link{head}}.
#' @author Thiago de Paula Oliveira,
#' \email{thiago.paula.oliveira@@alumni.usp.br}
#' @importFrom utils head str
#' @examples
#' # Example 1: Generates a CRD design with 5 treatments and 5 reps each.
#' crd1 <- CRD(t = 5, reps = 5, plotNumber = 101,
#' seed = 1985, locationName = "Fargo")
#' crd1$infoDesign
#' print(crd1)
#'
#' @export
print.CRD <- function(x, n=10, ...){
#---------------------------------------------------------------------
# Parameters
#---------------------------------------------------------------------
cat("Completely Randomized Design (CRD)", "\n\n")
cat("Information on the design parameters:", "\n")
str(x$infoDesign)
#---------------------------------------------------------------------
# Head
#---------------------------------------------------------------------
nr <- nrow(x$fieldBook)
nhead <- min(n, nr)
if (n < 0) {
nhead_print <- nr + n
}else {
nhead_print <- nhead
}
cat("\n", nhead_print,
"First observations of the data frame with the CRD field book:",
"\n")
print(head(x$fieldBook, n=nhead, ...))
}

#-----------------------------------------------------------------------
# Summary
#-----------------------------------------------------------------------
#' @rdname summary.CRD
#' @method summary CRD
#' @title Summary an \code{CRD} object
#' @usage \method{summary}{CRD}(object, ...)
#' @aliases summary.CRD
#' @description Summarise information on the design parameters, and data
#' frame structure
#' @return an object inheriting from class \code{summary.CRD}
#' @param object an object inheriting from class
#' \code{\link[FielDHub]{CRD}}
#'
#' @param ... Unused, for extensibility
#' @author Thiago de Paula Oliveira,
#' \email{thiago.paula.oliveira@@alumni.usp.br}
#'
#' @examples
#' # Example 1: Generates a CRD design with 5 treatments and 5 reps each.
#' crd1 <- CRD(t = 5, reps = 5, plotNumber = 101,
#' seed = 1985, locationName = "Fargo")
#' crd1$infoDesign
#' summary(crd1)
#'
#' @export
summary.CRD <- function(object, ...) {
structure(object, oClass=class(object),
class = "summary.CRD")
}

#-----------------------------------------------------------------------
# Print summary
#-----------------------------------------------------------------------
#' @rdname print.summary.CRD
#' @method summary CRD
#' @title Print the summary of an \code{CRD} object
#' @usage \method{print}{summary.CRD}(x, ...)
#' @aliases print.summary.CRD
#' @description Print summary information on the design parameters, and
#' data frame structure
#' @return an object inheriting from class \code{CRD}
#' @param x an object inheriting from class \code{\link[FielDHub]{CRD}}
#'
#' @param ... Unused, for extensibility
#' @author Thiago de Paula Oliveira,
#' \email{thiago.paula.oliveira@@alumni.usp.br}
#' @importFrom utils str
#' @importFrom dplyr glimpse
#' @export
print.summary.CRD <- function(x, ...){
cat("Completely Randomized Design (CRD):", "\n\n")
#---------------------------------------------------------------------
cat("1. Information on the design parameters:", "\n")
str(x$infoDesign)
cat("\n")
#---------------------------------------------------------------------
cat("2. Strcuture of the data frame with the CRD field book:", "\n\n")
dplyr::glimpse(x$fieldBook)
#---------------------------------------------------------------------
}
24 changes: 12 additions & 12 deletions man/CRD.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions man/print.CRD.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/print.summary.CRD.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions man/summary.CRD.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e35eb5

Please sign in to comment.