---
title: "Unreplicated Diagonal Arrangement Design"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Unreplicated Diagonal Arrangement Design}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8} 
---


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


## Single Unreplicated Diagonal Arrangement Design

This vignette shows how to generate **single and multiple unreplicated diagonal arrangement designs** using both the FielDHub Shiny App and the scripting function `diagonal_arrangement()` from the `FielDHub` R package.

## Overview

In some experiments, there is insufficient seed quantity or field space to conduct trials with large numbers of genotypes, so plant breeders must use unreplicated or partially replicated experimental designs, like unreplicated designs with checks allocated in a systematic diagonal distribution. In some cases, the experiment is split into blocks of specified size. This allows breeders to design a field that contains multiple different experiments, for example, plants at various stages of maturity.

FielDHub includes a function to run such experimental designs, as well as tabs for single and multiple diagonal arrangement on the FielDHub app. 

## Use Case

Suppose a plant breeding project needs to identify superior entries of barley. In this project, a preliminary yield trial (PYT) is carried out with 300 genotypes tested in one experiment and over one location by an unreplicated design. The experiment is lying in a field containing 15 rows and 22 columns of plots. In addition, 5 checks are included in a systematic diagonal arrangement across the field to fill 30 plots representing 9.1% of the total number of experimental plots. 

## 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** > **Single Diagonal Arrangement**

Then, follow the following steps where we will show how to generate a single unreplicated diagonal arrangement design.

##  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 integer number entry 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 4 checks and 8 treatments/genotypes. It is crucial to allocate the checks in the top part of the file.

```{r, include=FALSE}
ENTRY <- 1:12
NAME <- c(c("CH1","CH2","CH3","CH4"), paste0("ND-", 5:12))
df <- data.frame(ENTRY,NAME)
```

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


2. Enter the number of entries/treatments in the **Input # of Entries** box, which is 300 in our case.

3. Select 5 from the drop-down on the **Input # of Checks** box. 

4. Since we want to run this experiment over 1 location, set **Input # of Locations** to 1.

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

6. To ensure that randomizations are consistent across sessions, we can set a seed number in the box labeled **Seed Number**. For instance, we will set it to `16`. 

7. Enter the name for the experiment in the **Input Experiment Name** box. For example, `PYT_BARLEY_2022`.

8. Enter the starting plot number in the **Starting Plot Number** box. In this experiment we want the plot start at `1001`.

9. Enter the name of the site/location in the **Input the Location** box. For this experiment we will set the site as `FARGO`. In the case of users will run the experiment in multiple locations, the name for each location must be enter separate by comma, for example: `FARGO, CASSELTON, MINOT`.

10. Once we have entered all 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 `15 x 22`. 

12. Click the **Randomize!** button to randomize the experiment with the set field dimensions and to see the output plots. 


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 a single diagonal arrangement 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, **Expt Design Info**, 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 that was generated with the inputs, as well as a table of the checks with the number of times they appear in the field.


### 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 each 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. 

In the **Choose % of Checks:** drop-down box, users can play with different options for the total amount of checks in the field. 

### 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: `diagonal_arrangement()`

You can run the same design with a function in the FielDHub package, `diagonal_arrangement()`.

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, warning=FALSE, comment=''}
single_diag <- diagonal_arrangement(
  nrows = 15,
  ncols = 22,
  lines = 300,
  checks = 5,
  l = 1,
  plotNumber = 1,
  exptName = "PYT_BARLEY_2022",
  locationNames = "FARGO",
  seed = 16,
)
```

#### Details on the inputs entered in `diagonal_arrangement()` above:

*   `nrows = 15` is the number of columns in the field.
*   `ncols = 22` is the number of rows in the field.
*   `lines = 300` is the number of genotypes.
*   `checks = 5` is the number of checks.
*   `l = 1` is the number of locations.
*   `plotNumber = 1` is the starting plot number.
*   `exptName = "PYT_BARLEY_2022"` optional name for the experiment
*   `locationNames = "FARGO"` optional name for each location.
*   `seed = 16` is the seed number to replicate identical randomizations.

### Print `single_diag` object

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


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

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

### Access to `single_diag` object

The function `diagonal_arrangement()` 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. `single_diag$layoutRandom` or `single_diag$fieldBook`. 

`single_diag$fieldBook` is a data frame 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 <- single_diag$fieldBook
head(field_book, 10)
```

```{r, echo=FALSE, eval=TRUE}
field_book <- single_diag$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,

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

The figure above shows a map of an experiment randomized as a single unreplicated diagonal arrangement design. Gray plots represent the unreplicated treatments, while distinctively colored check plots are replicated throughout the field in a systematic diagonal arrangement.

<br>

## Multiple Unreplicated Diagonal Arrangement Design

Now, we show how to generate the same kind of unreplicated design with multiple experiments in the same field. 

## Use Case 

A plant breeding project needs to test 300 genotypes divided among three different experiments in amounts of 100, 120, and 80 respectively. Each experiment represents different stages of maturity. The 3 experiments are lying in a field containing 15 rows and 22 columns of plots. In addition, 5 checks are included in a systematic diagonal arrangement across experiments to fill 30 plots representing 9.1% of the total number of experimental plots.

`FielDHub` can perform the randomization for the design in the problem explained above. This can be solved either through the app or the `diagonal_arrangement()` function.

## 1. Using the FielDHub Shiny App

To generate a multiple unreplicated diagonal arrangement design using the FielDHub app:

First, go to **Unreplicated Designs** > **Multiple Diagonal Arrangement**

Then, follow the following steps where we will show how to generate this kind of design.

##  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}
ENTRY <- 1:12
NAME <- c(c("CH1","CH2","CH3","CH4"), paste0("ND-", 5:12))
df <- data.frame(ENTRY,NAME)
```
    * **Note:** If you wish to create multiple blocks of different sizes from an imported entries list, for example, a block of size 80, 90, and 100 plots, FielDHub will read the imported entries list as checks, then the 80 entries for the first block, then the 90 entries for the second block, then the 100 entries for the last block.
    
```{r, echo = FALSE, results='asis'}
library(knitr)
kable(df)
```

2. Select the checkbox option **Use the same entries across experiments** if the purpose is to make replications instead of testing different experiments. Checking this option requires the same size for all blocks. For example, testing 100 treatments across 3 blocks require to set `300` in **Input # of Entries** and  `100, 100, 100` as input in **Input # Entries per Expt**. In our case we will keep unchecked this option.

3. Enter the total number of entries/treatments in the **Input # of Entries** box, which is `300` in our case.

4. Enter the number of entries/treatments by experiment separate by comma in the **Input # Entries per Expt** box, which are `100, 120, 80` in our case.

5. Select 5 from the drop-down on the **Input # of Checks** box. 

6. Since we want to run this experiment over 1 location, set **Input # of Locations** to `1`.

7. Select `By Row` or `By Column` in the **Blocks Layout:**. For this example we will set the `By Row` experiments/blocks layout.

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

9. Enter the starting plot number for each experiment in the **Starting Plot Number** box. In this experiment we want the plot start at `1, 1001, 2001` for each experiment. The app also allows setting only one number for all experiments. For example, the plot number could start at `10`.

10. Enter the name for each experiment in the **Input Experiment Name** box. For example, `MATURITY1, MATURITY2, MATURITY3`.

11. To ensure that randomization are consistent across sessions, we can set a seed number in the box labeled **Seed Number**. For instance, we will set it to `17`. 

12. Enter the name of the site/location in the **Input the Location** box. For this experiment we will set the site as `FARGO`. In the case of users will run the experiment in multiple locations, the name for each location must be enter separate by comma, for example: `FARGO, CASSELTON, MINOT`.

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

14. 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 `15 x 22`. 

15. Click the **Randomize!** button to randomize the experiment with the set field dimensions and to see the output plots. 


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 a single diagonal arrangement 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, **Expt Design Info**, 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 that was generated with the inputs, as well as a table of the checks with the number of times they appear in the field.


### 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. 

In this tab by the **Choose % of Checks:** box  users can play with different options for the total amount of checks in the field. 

### 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: `diagonal_arrangement()`

A variation on the single diagonal arrangement included in the `diagonal_arrangement()` function is the multiple diagonal arrangement, where the experiment is split into blocks of specified size. 


```{r, echo=TRUE}
multi_diag <- diagonal_arrangement(
  nrows = 15,
  ncols = 22,
  lines = 300,
  kindExpt = "DBUDC", 
  blocks = c(100,120,80),
  checks = 5,
  l = 1,
  plotNumber = c(1, 1001, 2001),
  exptName = c("MATURITY1", "MATURITY2", "MATURITY3"),
  locationNames = "FARGO",
  seed = 17
)
```

#### Details on the inputs entered in `diagonal_arrangement()` above:

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

*   `nrows = 15` is the number of columns in the field.
*   `ncols = 22` is the number of rows in the field.
*   `lines = 300` is the number of genotypes.
*   `kindExpt = "DBUDC"` is an option to randomize multiple experiments
*   `blocks = c(100,120,80)` are the blocks in multiple arrangement.
*   `checks = 5` is the number of checks.
*   `l = 1` is the number of locations.
*   `plotNumber = c(1, 1001, 2001)` is the starting plot number for each experiment. It could be just one number as well.
*   `exptName = c("MATURITY1", "MATURITY2", "MATURITY3")` is an optional name for each experiment.
*   `locationNames = "FARGO"` optional name for each location.
*   `seed = 17` is the seed number to replicate identical randomizations.

### Print `multi_diag` object

For printing a summary of the information that is in the object `multi_diag` we can use the generic function `print()`

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

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

### Access to `multi_diag` object

The function `diagonal_arrangement()` 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. `multi_diag$layoutRandom` or `multi_diag$fieldBook`. 

`multi_diag$fieldBook` is a data frame 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 <- multi_diag$fieldBook
head(field_book, 10)
```

```{r, echo=FALSE, eval=TRUE}
field_book <- multi_diag$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,

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

The figure above shows a map of an experiment randomized as a multiple unreplicated diagonal arrangement design. Gray, salmon, and pink shade the blocks of unreplicated experiments, while distinctively colored check plots are replicated throughout the field in a systematic diagonal arrangement.

<br>
<br>
<br>
