---
title: "Two-Factors Design: RCBD"
description: > 
  Factorial Complete Randomize Block Design
vignette: >
  %\VignetteIndexEntry{DoE-2_RCBD}
  %\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 factors, four designs become available: **CRD**, **RCBD**, **Split-plot RCBD**, and **Augmented**.

#  Factorial Randomized Complete Block Design (RCBD)

Recommended for multi-factor trials where field spatial variability or environmental gradients require blocking to control experimental error.

```{r, echo=TRUE}
# 1. Define factors: Bean genotypes and fertilization levels
factors_rcbd <- list(
  Genotype = c("Bean_01", "Bean_02", "Bean_03"),
  Fertilization = c("0", "50", "100")
)

# 2. Generate factorial RCBD layout
rcbd_exp <- design_repblock(
  nfactors = 2,
  factors = factors_rcbd,
  type = "rcbd",
  rep = 4,
  zigzag = TRUE,
  seed = 2026
)

# Fieldbook preview
rcbd_exp$fieldbook %>%
  head(10) %>%
  knitr::kable(caption = "Factorial RCBD Fieldbook preview")

# Spatial layout visualization
tarpuy_plotdesign(
  data = rcbd_exp,
  factor = "Genotype",
  fill = c("plots", "Fertilization")
)
```

# 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 <- rcbd_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(5.2, 10)
    ,
    border_color = "#5C0000"
    ,
    border_width = 1.5
  ) %>%
  include_image(
    value = "https://inkaverse.com/img/inkaverse.png"
    ,
    size = c(1.3, 1.5)
    ,
    position = c(0.8, 9.1)
  )  %>%
  include_text(
    value = "plots"
    ,
    position = c(4.2, 9.1)
    ,
    size = 20
    ,
    color = "black"
    ,
    fontface = "bold"
    ,
    font = font[1]
  )  %>%
  include_image(value = "https://huito.inkaverse.com/img/scale.pdf"
                ,
                size = c(5, 1)
                ,
                position = c(2.6, 7.7)) %>%
  include_barcode(value = "qrcode"
                  ,
                  size = c(5, 5)
                  ,
                  position = c(2.6, 4.7)) %>%
  include_text(
    value = "Genotype"
    ,
    position = c(2.6, 2)
    ,
    size = 12
    ,
    prefix = "Genotype: "
    ,
    color = "blue"
    ,
    font = font[2]
    , 
    fontface = "bold"
  )  %>%
    include_text(
    value = "Fertilization"
    ,
    position = c(2.6, 1.5)
    ,
    size = 12
    ,
    prefix = "Fertilization: "
    ,
    color = "red"
    ,
    font = font[2]
    , 
    fontface = "bold"
  ) |> 
  include_image(value = "https://huito.inkaverse.com/img/scale.pdf"
                ,
                size = c(5, 1)
                ,
                position = c(2.6, 0.6)) 
```

## 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 = "vertical-DBCA-2"
              , nlabels = 12)
```

<!-- <embed src="vertical-DBCA-2.pdf" height="600" width="100%" alt="pdf" pluginspage="http://www.adobe.com/products/acrobat/readstep2.html"> -->

