---
title: "Row-Column Design"
output: rmarkdown::html_vignette
bibliography: references.bib
vignette: >
  %\VignetteIndexEntry{Row-Column 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 a **row-column Design** using both the FielDHub Shiny App and the scripting function `row_column()` from the `FielDHub` package.

## 1. Using the FielDHub Shiny App

To generate a Row-Column Design using the FielDHub app:

First, go to **Lattice Designs** > **Resolvable Row-Column Design (RCD)**

Then, follow the following steps where we will show how to generate a Row-Column Design with 45 treatments, 5 rows and 3 reps.

## 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 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. The column `NAME` must have a unique name that identifies each treatment. 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 12 treatments. 
    

```{r, include=FALSE}
ENTRY <- 1:12
NAME <- c(paste0("Genotype", LETTERS[1:12]))
df <- data.frame(ENTRY,NAME)
```

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

2. Input the number of treatments in the **Input # of Treatments** box. We will enter `45` for our sample experiment. 
3. Set the number of plots in each incomplete block with the **Input # of Plots per IBlock** box. In this examples, set it to `5`.

4. Select the number of replications of these treatments with the **Input # of Full Reps** box. In this examples, set it to `3`.

5. Enter the number of locations in **Input # of Locations**. We will run this experiment over a single location, so set it to `1`.

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

7. 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. Set it to `101`.

8. Enter a name for the location of the experiment in the **Input Location** box. If there are multiple locations, each name must be in a comma separated list. For this example, set it to `"FARGO"`.

9. 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 `1244`. 

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

## Outputs

After you run a row-column design in FielDHub, there are several ways to display the information contained in the field book. 

### Field Layout

When you first click the run button on a row-column design, FielDHub displays the Field Layout tab, which shows the entries and their arrangement in the field. In the box below the display, you can change the layout of the field. 
You can also display a heatmap over the field by changing **Type of Plot** to `Heatmap`. To view a heatmap, you must first simulate an experiment over the described field with the **Simulate!** button. A pop-up window will appear where you can enter what variable you want to simulate along with minimum and maximum values. 

### 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. If we have simulated data for a heatmap, an additional column for that variable appears in the field book. 

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

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

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

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

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


```{r, echo = TRUE}
rcd <- row_column(
  t = 45,
  nrows = 5,
  r = 3,
  l = 1, 
  plotNumber = 101, 
  locationNames = "FARGO",
  seed = 1244
)
```

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

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

*   `t = 45` is the number of treatments.
*   `nrows = 5` is the number of rows.
*   `r=3` is the number of reps
*   `l = 1` is the number of locations.
*   `plotNumber = 101` is the starting plot number.
*   `locationNames = "FARGO"` is an optional name for each location.
*   `seed = 1244` is the random seed to replicate identical randomizations.

### Print `rcd` object

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

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

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

### Access to `rcd` object

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

`rcd$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`, `LOCATION`, `PLOT`, `REP`, `ROW`, `COLUMN`, `ENTRY`, and `TREATMENT`.


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

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

### Plot the field layout

For plotting the layout in function of the coordinates `ROW` and `COLUMN`, you can use the the generic function `plot()` as follows,


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


<br>
<br>
<br>