From 5e35eb5c33926457ee39f9e3393b9b4fad029f25 Mon Sep 17 00:00:00 2001 From: Prof-ThiagoOliveira Date: Fri, 19 Mar 2021 19:47:24 +0000 Subject: [PATCH] fct_CRD-print_summary --- NAMESPACE | 5 ++ R/fct_CRD.R | 169 ++++++++++++++++++++++++++++++++------- man/CRD.Rd | 24 +++--- man/print.CRD.Rd | 35 ++++++++ man/print.summary.CRD.Rd | 24 ++++++ man/summary.CRD.Rd | 33 ++++++++ 6 files changed, 250 insertions(+), 40 deletions(-) create mode 100644 man/print.CRD.Rd create mode 100644 man/print.summary.CRD.Rd create mode 100644 man/summary.CRD.Rd diff --git a/NAMESPACE b/NAMESPACE index d43a01d..5b19235 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,7 @@ # Generated by roxygen2: do not edit by hand +S3method(print,CRD) +S3method(summary,CRD) export("%>%") export(CRD) export(RCBD) @@ -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) @@ -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) diff --git a/R/fct_CRD.R b/R/fct_CRD.R index a5899f7..0ee8bd7 100644 --- a/R/fct_CRD.R +++ b/R/fct_CRD.R @@ -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. @@ -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.') @@ -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) @@ -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)) - -} \ No newline at end of file + 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) + #--------------------------------------------------------------------- +} diff --git a/man/CRD.Rd b/man/CRD.Rd index 57c1024..cf8131d 100644 --- a/man/CRD.Rd +++ b/man/CRD.Rd @@ -36,20 +36,20 @@ It randomly generates a completely randomized design. } \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) @@ -59,10 +59,10 @@ head(crd2$fieldBook,10) 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 diff --git a/man/print.CRD.Rd b/man/print.CRD.Rd new file mode 100644 index 0000000..49293bf --- /dev/null +++ b/man/print.CRD.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fct_CRD.R +\name{print.CRD} +\alias{print.CRD} +\title{Print an \code{CRD} object} +\usage{ +\method{print}{CRD}(x, n, ...) +} +\arguments{ +\item{x}{an object inheriting from class} + +\item{n}{a single integer... +\code{\link[FielDHub]{CRD}}} + +\item{...}{further arguments passed to \code{\link{head}}.} +} +\value{ +an object inheriting from class \code{CRD} +} +\description{ +Prints information about randomization of Completely + Randomized Design (CDR) +} +\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) + +} +\author{ +Thiago de Paula Oliveira, + \email{thiago.paula.oliveira@alumni.usp.br} +} diff --git a/man/print.summary.CRD.Rd b/man/print.summary.CRD.Rd new file mode 100644 index 0000000..a38ae71 --- /dev/null +++ b/man/print.summary.CRD.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fct_CRD.R +\name{print.summary.CRD} +\alias{print.summary.CRD} +\title{Print the summary of an \code{CRD} object} +\usage{ +\method{print}{summary.CRD}(x, ...) +} +\arguments{ +\item{x}{an object inheriting from class \code{\link[FielDHub]{CRD}}} + +\item{...}{Unused, for extensibility} +} +\value{ +an object inheriting from class \code{CRD} +} +\description{ +Print summary information on the design parameters, and + data frame structure +} +\author{ +Thiago de Paula Oliveira, + \email{thiago.paula.oliveira@alumni.usp.br} +} diff --git a/man/summary.CRD.Rd b/man/summary.CRD.Rd new file mode 100644 index 0000000..ec41744 --- /dev/null +++ b/man/summary.CRD.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fct_CRD.R +\name{summary.CRD} +\alias{summary.CRD} +\title{Summary an \code{CRD} object} +\usage{ +\method{summary}{CRD}(object, ...) +} +\arguments{ +\item{object}{an object inheriting from class +\code{\link[FielDHub]{CRD}}} + +\item{...}{Unused, for extensibility} +} +\value{ +an object inheriting from class \code{summary.CRD} +} +\description{ +Summarise information on the design parameters, and data + frame structure +} +\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) + +} +\author{ +Thiago de Paula Oliveira, + \email{thiago.paula.oliveira@alumni.usp.br} +}