---
title: "Augmented Randomized Complete Block Design"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Augmented Randomized Complete Block Design}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = NA,
  warning = FALSE, 
  message = FALSE
)
```

This vignette shows how to generate an **augmented randomized complete block design ** using both the FielDHub Shiny App and the scripting function `RCBD_augmented()` from the `FielDHub` package.

## Overview

The augmented randomized complete block design is another option to overcome the problem of limited facilities or lack of seed when researchers want to test many treatments. In this kind of design, the approach is to build augmented blocks and allocate the same amount of controls in every block along with the treatments. 

FielDHub includes a function to run such experimental designs, features include options to set the number of entries and the number of checks and augmented blocks for the experiment. Users can also choose to run the same experiment over multiple locations. 

## Use case

For example, we say a project needs to test 120 genotypes of cassava over two locations. In addition, the research includes four checks and six augmented blocks to carry out this experiment. This design setup comes out with 6 blocks of size 24 plots for a total of 144 plots that will be distributed in a field of 12 rows and 12 columns.

## Running the Shiny App

To launch the app you need to run either

```{r, eval=FALSE}
FielDHub::run_app()
```

or

```{r, eval=FALSE}
library(FielDHub)
run_app()
```

## 1. Using the FielDHub Shiny App

Once the app is running, go to  **Unreplicated Designs** > **RCBD Augmented**

Then, follow the following steps where we will show how to generate an RCBD Augmented.

## Inputs

1. **Import entries' list?** Choose whether to import a list with entry numbers and names for genotypes or treatments.
    * If the selection is `No`, that means the app is going to generate synthetic data for entries and names of the treatment/genotypes based on the user inputs.

    * If the selection is `Yes`, the entries list must fulfill a specific format and must be a `.csv` file. The file must have the columns `ENTRY` and  `NAME`. The `ENTRY` column must have a unique entry integer number for each treatment/genotype. The column `NAME` must have a unique name that identifies each treatment/genotype. Both `ENTRY` and `NAME` must be unique, duplicates are not allowed. In the following table, we show an example of the entries list format. This example has an entry list with four checks and 8 treatments/genotypes. It is crucial to allocate the checks in the top part of the file.

```{r, include=FALSE}
library(FielDHub)
ENTRY <- 1:12
NAME <- c(c("CHECK1","CHECK2","CHECK3"), paste0("G-", 4:12))
df <- data.frame(ENTRY,NAME)
```

```{r, echo = FALSE, results='asis'}
library(knitr)
kable(df, format = "html")
```

1. Enter the number of stacked experiments in the **Input # of Stacked Expts** box. This means the number of times the experiment will be replicated. In our case we will perform just `1` experiment.

2. On the augmented RCBD we have the option to choose whether we randomize the entries or not, with the **Randomize Entries** toggle button. It is recommended always randomized the treatments/entries but some researchers choose not to randomize treatments, this is often due to logistical issues.

3. Enter the number of entries/treatments in the **Input # of Entries** box, which is `120` in our example experiment. 

4. Set the number of checks per block with the **Checks per Block** box. In our case, it is `5` checks.

5. Set the number of blocks with **Input # of Blocks** box, which is `6` in our example.

The total number of plots in our field will be **Input # of Stacked Expts**(**Input # of Entries** + **Input # of Blocks** * **Checks per Block**), per location. 

6. Enter the number of locations in **Input # of Locations**. Set it as `2`. 

7. Select `serpentine` or `cartesian` in the **Plot Order Layout**. For this example we will set the `serpentine` layout.

8. To ensure that randomizations are consistent across sessions, we can set a random seed in the box labeled **random seed**. In this example, we will set it to `1987`. 

9. Enter the starting plot number in the **Starting Plot Number** box. If the experiment has multiple locations, you must enter a comma separated list of numbers the length of the number of locations for the input to be valid. 

10. Once we have entered the information for our experiment on the left side panel, click the **Run!** button to run the design. 

11. You will then be prompted to select the dimensions of the field from the list of options in the drop down in the middle of the screen with the box labeled **Select dimensions of field**. In our case, we will select `12 x 12`. 

12. Click the **Randomize!** button to randomize the experiment with the set field dimensions and to see the output plots. If you change the dimensions again, you must re-randomize.

If you change any of the inputs on the left side panel after running an experiment initially, you have to click the Run and Randomize buttons again, to re-run with the new inputs.

## Outputs

After you run the augmented RCBD in FielDHub and set the dimensions of the field, there are several ways to display the information contained in the field book. The first tab, **Get Random**, shows the option to change the dimensions of the field and re-randomize, as well as a reference guide for experiment design. 

### Input Data

On the second tab, **Input Data**, you can see all the entries in the randomization in a list, as well as a table of the checks with the number of times they appear in the field. In the list of entries, the reps for each check is included as well.


### Randomized Field

The **Randomized Field** tab displays a graphical representation of the randomization of the entries in a field of the specified dimensions. The checks are all colored uniquely, showing the number of times they are distributed throughout the field. The display includes numbered labels for the rows and columns. You can copy the field as a table or save it directly as an Excel file with the _Copy_ and _Excel_ buttons at the top. 

### Plot Number Field

On the **Plot Number Field** tab, there is a table display of the field with the plots numbered according to the Plot Order Layout specified, either _serpentine_ or _cartesian_. You can see the corresponding entries for each plot number in the field book. Like the Randomized Field tab, you can copy the table or save it as an Excel file with the _Copy_ and _Excel_ buttons. 

### Field Book

The **Field Book** displays all the information on the experimental design in a table format. It contains the specific plot number and the row and column address of each entry, as well as the corresponding treatment on that plot. This table is searchable, and we can filter the data in relevant columns. 

<br>

## 2. Using the `FielDHub` function: `RCBD_augmented()`

You can run the same design with the function `RCBD_augmented()` in the `FielDHub` package.

First, you need to load the `FielDHub` package typing,

```{r, echo = TRUE}
library(FielDHub)
```

Then, you can enter the information describing the above design like this:

```{r, echo = TRUE}
aug_RCBD <- RCBD_augmented(
  lines = 120,
  checks = 4,
  b = 6, 
  repsExpt = 1, 
  l = 2,
  random = TRUE,
  exptName = "Cassava_2022", 
  plotNumber = c(1001, 2001),
  locationNames = c("FARGO", "CASSELTON"),
  nrows = 12,
  ncols = 12,
  seed = 1987
)
```


#### Details on the inputs entered in `RCBD_augmented()` above

The description for the inputs that we used to generate the design,

*   `lines = 120` is the number of entries
*   `checks = 4` is the number of checks in each augmented block.
*   `b = 6` is the number of augmented blocks.
*   `repsExpt = 1` is the number of reps for the experiment.
*   `l = 2` is the number of locations.
*   `random = TRUE` it means both treatment/entries and checks will be randomized.
*   `exptName = "Cassava_2022"` is an optional name for the experiment.
*   `plotNumber = c(1001,2001)` are the starting plot number for each location respectively, 
     or a single number for 1 location.
*   `locationNames = c("FARGO", "CASSELTON")` are the values for representing 
    respective name for each location.
*   `nrows = 12` is the number of rows in the field. It is optional
*   `ncols = 12` is the number of columns in the field. It is optional.
*   `seed = 1987` is the random seed to replicate identical randomizations.

### Print `aug_RCBD` output

To print a summary of the information that is in the object `aug_RCBD`, we can use the generic function `print()`. 


```{r, echo=TRUE, eval=FALSE}
print(aug_RCBD)
```

```{r, echo=FALSE, eval=TRUE}
print(aug_RCBD)
```

### Access to `aug_RCBD` output

The function `RCBD_augmented()` returns a list consisting of all the information displayed in the output tabs in the FielDHub app: design information, plot layout, plot numbering, entries list, and field book. These are accessible by the `$` operator, i.e. `aug_RCBD$layoutRandom` or `aug_RCBD$fieldBook`. 

`aug_RCBD$fieldBook` is a list containing information about every plot in the field, with information about the location of the plot and the treatment in each plot. As seen in the output below, the field book has columns for `ID`, `EXPT`, `LOCATION`, `YEAR`, `PLOT`, `ROW`, `COLUMN`, `CHECKS`, `ENTRY`, and `TREATMENT`.

Let us see the first 10 rows of the field book for this experiment.

```{r, echo=TRUE, eval=FALSE}
field_book <- aug_RCBD$fieldBook
head(field_book, 10)
```

```{r, echo=FALSE, eval=TRUE}
field_book <- aug_RCBD$fieldBook
head(field_book, 10)
```


### Plot field layout

For plotting the layout in function of the coordinates `ROW` and `COLUMN` in the field book object we can use the generic function `plot()` as follow,

#### Plot layout for location 1

```{r, fig.align='center', fig.width=7.2, fig.height=5.5}
plot(aug_RCBD)
```

It is possible to pass more arguments to `plot()` such as the specific location. For example, you can plot the layout for location 2. 

#### Plot layout for location 2

```{r, fig.align='center', fig.width=7.2, fig.height=5.5}
plot(aug_RCBD, l = 2)
```

<br>
<br>

