---
title: "Two-Factors Design: Split-Plot in RCBD"
description: > 
  Split-Plot in RCBD
vignette: >
  %\VignetteIndexEntry{DoE-2_SPLIT}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{quarto::html}
knitr:
  opts_chunk: 
    collapse: true
    comment: '#>'
    echo: false
    warning: false
    message: false
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
source("https://raw.githubusercontent.com/Flavjack/inti/master/pkgdown/favicon/docs.r")
```

Planning an experiment follows a reproducible routine:

1. **Load required libraries:** Load `inti`, `knitr`, and `dplyr` packages.
1. **Define factor levels:** Set up lists with genotypes, treatments, and management factors.
1. **Dispatch design generator:** Choose between CRD, RCBD, Split-plot, or Augmented designs.
1. **Plot the field sketch:** Verify spatial layouts and serpentine/zigzag sequences.
1. **Label design:** Design the experimental labels to facilitate the data collection.
5. **Export to Field Book app:** Generate field-ready sheets with trait parameters.

```{r, echo=TRUE}
# Install packages and dependencies

library(inti)
library(dplyr)
library(huito)
```

# Designs with Two Factors

When evaluating two or more factors, four designs become available: **CRD**, **RCBD**, **Split-plot RCBD**, and **Augmented**.

## Split-Plot Design in RCBD

The Split-plot Design is recommended when one factor requires larger experimental units due to management constraints (such as irrigation) assigned to main plots, while a second factor (such as commercial quinoa varieties) is assigned to sub-plots within each main plot.

```{r, echo=TRUE}
# 1. Define factors: Irrigation regimes (main plots) and commercial quinoa varieties (sub-plots)
factors_split <- list(
  Irrigation = c("Full", "Deficit"),
  Variety    = c("Var_1", "Var_2", "Var_3")
)

# 2. Generate Split-plot layout: 2 main levels x 3 sub levels x 4 blocks = 24 plots
split_exp <- design_split(
  factors = factors_split,
  type = "split_rcbd",
  rep = 4,
  zigzag = TRUE,
  seed = 2026
)

# Fieldbook preview
split_exp$fieldbook %>% 
  head(10) %>% 
  knitr::kable(caption = "Split-plot Fieldbook preview")

# Field layout visualization
tarpuy_plotdesign(
  data = split_exp,
  factor = "Irrigation",
  fill = c("plots", "Variety")
)
```

# Label 

The experimental field book generated by the design is used as the input data for label creation. Each row represents an experimental unit, allowing the automatic generation of individualized labels.

```{r, echo=TRUE}
# Experimental fieldbook
fb <- split_exp$fieldbook
```

# Customize the label layout

The label layout can be customized by combining text, images and QR codes. Each layer can use values from the experimental field book, allowing automatic generation of labels for every experimental plot.

Load package and import fonts.

```{r, echo=TRUE}
font <- c("Permanent Marker", "Tillana", "Courgette")

huito_fonts(font)
```

> You can find more fonts in <https://fonts.google.com/>

# Label design

```{r}
#| echo: true

label <- fb %>%  
  label_layout(size = c(10, 2.5)
               , border_color = "blue"
               ) %>%
  include_image(
    value = "https://flavjack.github.io/inti/img/inkaverse.png"
    , size = c(2.1, 2.4)
    , position = c(1.2, 1.25)
    # , opts = list("image_scale(200)", "image_noise()")
    ) %>%
  include_barcode(
     value = "barcode"
     , size = c(2.5, 2.5)
     , position = c(8.2, 1.25)
     ) %>%
  include_text(value = "INKAVERSE"
               , position = c(4.6, 2)
               , size = 20
               , font = font[1]
               , fontface = "bold"
               , color = "red"
               ) %>%
  include_text(value = "Irrigation"
               , position = c(2.4, 1.2)
               , size = 12
               , opts = list(hjust = 0.0, vjust = 0.0)
               , font = font[2]
               , color = "black"
               , prefix = "Irrigation: "
               , fontface = "bold"
               ) %>%
    include_text(value = "Variety"
               , position = c(2.4, 0.5)
               , opts = list(hjust = 0.0, vjust = 0.0) 
               , size = 12
               , color = "#009966"
               , font = font[2]
               , prefix = "Variety: "
               , fontface = "bold"
               ) %>% 
  include_text(value = "plots"
               , position = c(9.7, 1.25)
               , angle = 90
               , size = 12
               , color = "brown"
               , font = font[3]
               , prefix = "Plot: "
               ) 
```

## Label preview

The preview mode `label_print(mode = "preview")` generate a example of the label design from a random row of the data set.

```{r}
label %>% 
  label_print(mode = "preview")
```

## Generate the complete labels

If you want generate the complete labels list, change: `label_print(mode = "complete")`.

```{r echo = TRUE}
#| eval: false

label %>% 
  label_print(mode = "complete"
              , filename = "horizontal-split"
              , nlabels = 12)
```

<!-- <embed src="horizontal-split.pdf" height="600" width="100%" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html"> -->


