---
title: "Rticles: Scientific Documents with Bookdown R Package"
subtitle: "www.lozanoisla.com"
author:
  - "Jimmy Gomez $^{1}$, Flavio Lozano-Isla $^{2*}$"
  - "$^{1}$ Agronomist, Co-founder ww.quipo.org"
  - "$^{2}$ Agronomist MSc, email: flavjack@gmail.com"
date: "`r if (knitr:::is_html_output()) format(Sys.time(), '%Y-%m-%d  %H:%M')`"
description: document template for scientific report
favicon: files/logo.png
github-repo: flavjack/rticles
site: bookdown::bookdown_site
documentclass: book
bookdown::gitbook:
output: 
  bookdown::word_document2:
    reference_docx: files/style_rticles.docx
    number_sections: false
  bookdown::html_document2:
    toc: true
    number_sections: true
    toc_float: true
always_allow_html: true
link-citations: true
colorlinks: true
zotero: true
bibliography: [files/book.bib, files/pkgs.bib]
csl: https://www.zotero.org/styles/apa
---

```{r setup, include=FALSE}
source("https://lozanoisla.com/setup.r")
```

```{r}
url <- paste0("https://docs.google.com/spreadsheets/d/"
              , "1QziIXGOwb8cl3GaARJq6Ez6aU7vND_UHKJnFcAKx0VI")
gs <- as_sheets_id(url)
# browseURL(url)

xl <- gs %>% 
  drive_download("files/fieldbook.xlsx", overwrite = T) %>%
  pluck(2)

fb <- xl %>% 
  readxl::read_excel("prosopis")  %>% 
  rename_with(tolower) %>% 
  mutate(across(c("rep", "nacl", "temp"), ~ as.factor(.) )) %>% 
  mutate(across(where(is.factor), ~ gsub("[[:space:]]", "", .)) ) %>% 
  as.data.frame()
# str(fb)
```

# Abstract {.unnumbered}

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. *Nulla facilisi* Ut fringilla. **Suspendisse potenti** Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam.

**KeyWords:** Lorem, ipsum, dolor, sit amet, consectetur.

\newpage

# Introduction

Lorem ipsum [@abbai2019] dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc [@bergström2020; @bhatt2019].

Curabitur tortor [@donald1976] Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrice. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna.

# Materials and Methods

The data was analyzed in the statistical software R [@R-base]. The germination analysis and graphics was carried out with the package GerminaR [@R-GerminaR]. Each variable was submitted at analysis of variance (ANOVA) and the mean comparison test used was Student-Newman Keuls (P\<0.05) [@R-agricolae]. For the multivariate analysis, the principal components analysis (PCA) and cluster hierarchical classification analysis (HCPC) will be used .

The vertical bars represent the means (±SE). The mean differences between the groups are represented by different capital letters and into the group different lowercase letters (SNK, p = 0.05) [@R-FactoMineR].

More information about multivarite analysis in <http://factominer.free.fr/index.html>. Explanation for intepretation and analysis:

**PCA:** <https://www.youtube.com/watch?v=pks8m2ka7Pk&feature=youtu.be>

**HCPC:** <https://www.youtube.com/watch?v=4XrgWmN9erg>

## Data set

The data set used for this analysis is available in the following link: [DATA SET](https://docs.google.com/spreadsheets/d/1QziIXGOwb8cl3GaARJq6Ez6aU7vND_UHKJnFcAKx0VI/edit#gid=137089581)

## Nulla metus metus

Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante (Table \@ref(tab:germin)).

# Results

## Sed convallis tristique sem

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos $g=\left(\frac{\sum_{i=1}^kn_1}{N}\right)100$.

Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet Mauris ipsum .

```{r code, echo=TRUE, eval=FALSE}
library(GerminaR) 

# load data
fb <- prosopis %>% 
  dplyr::mutate(across(c(rep, nacl, temp), ~ as.character(.) ))

# germination analysis
gsm <- ger_summary(SeedN = "seeds", evalName = "D", data = fb)
str(gsm)

# analisys of variance
av <- aov(formula = grp ~ nacl*temp + rep, data = gsm)
summary(av)

# mean comparision test
mc <- ger_testcomp(aov = av, comp = c("temp", "nacl"), type = "snk")
```

## Class aptent taciti

Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices (Figure \@ref(fig:jc)).

# Discussion

Curabitur tortor [@GerminaR2019], Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante (Figure \@ref(fig:germplot)).

Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. Suspendisse in justo eu magna luctus suscipit. Sed lectus. Integer euismod lacus luctus magna. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor .

# Conclusions

Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet.

# Acknowledgments

Donec lacus nunc, viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. Nulla facilisi. Integer lacinia sollicitudin massa. Cras metus. Sed aliquet risus a tortor. Integer id quam. Morbi mi.

# References

::: {#refs}
:::

```{r references}
if(!file.exists("files/pkgs.bib")){write_bib(c(.packages()),'files/pkgs.bib')}
```

<!-- Tables -->

\newpage

`r if (knitr:::is_html_output()) '# Tables {-}'`

```{r germin}
gs %>% 
  range_read("prosopis") %>% 
  select(rep:D5) %>% 
  head(10) %>% 
  inti::include_table(
    caption = "Germination dataset import from a googlesheet"
    , notes = "@schafleitner2007"
    , label = "__Source:__"
    )
```

\newpage

```{r webtab}
fb %>% 
  web_table("Germination dataset to download in html")
```

\newpage

<!-- Figures -->

`r if (knitr:::is_html_output()) '# Figures {-}'`

```{r germplot, fig.cap= fig$caption}

## Punctual analysis

# germination analysis
gsm <- ger_summary(SeedN = "seeds", evalName = "d", data = fb)

# analysis of variance
av <- aov(formula = grp ~ nacl*temp + rep, data = gsm)

# mean comparison test
mc <- ger_testcomp(aov = av, comp = c("temp", "nacl"), type = "snk")

# line or bar graphics
gbp <- mc$table %>% 
  fplot(type = "bar"
        , x = "temp"
        , y = "grp"
        , group = "nacl"
        , xlab = "Temperature (ºC)"
        , ylab = "Germination ('%')"
        , glab = "NaCl (Mpa)"
        , ylimits = c(0, 150, 10)
        , sig = "sig"
        , error = "std"
        , color = F)

## Intime analysis

# data frame with percentual or relative germination in time
git <- ger_intime(Factor = "nacl"
                  , SeedN = "seeds"
                  , evalName = "d"
                  , method = "percentage"
                  , data = fb)

# graphic germination in time
ggt <- git %>% 
  fplot(type = "line"
        , x = "evaluation"
        , xlab = "Day"
        , y = "mean"
        , ylab = "Germination ('%')"
        , group = "nacl"
        , glab = "NaCl (Mpa)"
        , ylimits = c(0, 120, 10)
        , color = T)

## Plot figures
gerplot <- cowplot::plot_grid(gbp, ggt
                              , nrow = 1
                              , labels = "AUTO")

save_plot("files/fig_gerplot.png", 
          plot = gerplot,
          dpi= 300, 
          base_height = 100, 
          base_aspect_ratio = 2.5, 
          units = "mm")

fig <- include_figure(
  figure = "files/fig_gerplot.png"
  , caption = "Germination  experiment with *Prosopis juliflor* under different osmotic potentials and temperatures."
  , notes = "
  A) Bar graph with germination percentage in a factorial analisys. 
  B) Line graph from cumulative germination under different osmotic potentials."
  , label = "__Where:__"
  )
fig$figure
```

\newpage

```{r jc, fig.cap= fig$caption}

if (!file.exists("files/fig_jc.jpg")){
  download.file(
  url = "https://raw.githubusercontent.com/Flavjack/rticles/master/files/fig_jc.jpg", destfile = "files/fig_jc.jpg", mode = "wb")
}

fg <- magick::image_read("files/fig_jc.jpg") %>% 
  grid::rasterGrob()

plot <- cowplot::plot_grid(fg) +
  draw_plot_label(label = c("A", "B", "C")
                  , x = c(0.02, 0.51, 0.02)
                  , y = c(0.98, 0.98, 0.30))
  
save_plot("files/fig_jcurcas.png"
          , plot = plot
          , dpi= 300
          , base_height = 100
          , base_aspect_ratio = 1.2
          , units = "mm")

fig <- include_figure(
  figure = "files/fig_jcurcas.png"
  , caption = "Plant of *Jatropha curcas*. 
  A) Foliage.
  B) Leaf. 
  C) Fruit.")

fig$figure
```

\newpage

```{r mv, fig.cap= fig$caption}

# Principal component Analysis

pca <- gsm %>% 
  drop_na() %>% 
  select(nacl, temp, grp, mgt, unc, syn) %>% 
  
  PCA(X = ., 
      scale.unit = T,
      graph = F,
      quali.sup = c(1,2))

# sink("files/pca.txt")
# # Results
# summary(pca, nbelements = Inf, nb.dec = 2)
# # Correlation de dimensions
# dimdesc(pca)
# sink()

ppi <- 300
png("files/fig_pca_var.png", width=7*ppi, height=7*ppi, res=ppi)

plot.PCA(pca,
         choix="var",
         title="",
         autoLab = "y", 
         cex = 0.8,
         shadowtext = T)

graphics.off()

ppi <- 300
png("files/fig_pca_ind.png", width=7*ppi, height=7*ppi, res=ppi)

plot.PCA(pca,
         choix="ind",
         habillage = 1,
         title="",
         autoLab = "y", 
         cex = 0.8,
         shadowtext = T,
         label = "ind",
         legend = list(bty = "y", x = "topright"))

graphics.off()

# Hierarchical Clustering Analysis

clt <- pca %>% 
  FactoMineR::HCPC(., nb.clust=-1, graph = F)

# sink("files/clu.txt")
# clus$call$t$tree
# clus$desc.ind
# clus$desc.var
# sink()

ppi <- 300
png("files/fig_cluster_tree.png", width=7*ppi, height=7*ppi, res=ppi)

plot.HCPC(x = clt, 
          choice = "tree")

graphics.off()

ppi <- 300
png("files/fig_cluster_map.png", width=7*ppi, height=7*ppi, res=ppi)

plot.HCPC(x = clt, choice = "map")

graphics.off()

fg_01 <- magick::image_read("files/fig_pca_var.png") %>% 
  grid::rasterGrob()
fg_02 <- magick::image_read("files/fig_pca_ind.png")  %>% 
  grid::rasterGrob()
fg_03 <- magick::image_read("files/fig_cluster_tree.png") %>% 
  grid::rasterGrob()
fg_04 <- magick::image_read("files/fig_cluster_map.png") %>% 
  grid::rasterGrob()

plot <- cowplot::ggdraw(xlim = c(0, 1), ylim = c(0, 1)) +
  draw_plot(fg_01, width = 0.5, height = 0.5, x = 0.0, y = 0.5) +
  draw_plot(fg_02, width = 0.5, height = 0.5, x = 0.5, y = 0.5) +
  draw_plot(fg_03, width = 0.5, height = 0.5, x = 0.0, y = 0.0) +
  draw_plot(fg_04, width = 0.5, height = 0.5, x = 0.5, y = 0.0) +
  draw_plot_label(label = c("A", "B", "C", "D"),
                  x = c(0.0, 0.5, 0.0, 0.5 ), 
                  y = c(0.98, 0.98, 0.48, 0.48))

save_plot("files/fig_mv.png"
          , plot = plot
          , dpi = 300
          , base_height = 20
          , base_aspect_ratio = 1.2
          , units = "cm")

fig <- include_figure(
  caption = "Multivariate Analysis: 
  Principal component Analysis and Hierarchical Clustering Analysis."
  , figure = "files/fig_mv.png"
  )
  
fig$figure
```

<!-- Anexos -->

`r if (knitr:::is_html_output()) '# Anexos {-}'`

```{r anx}
if (!file.exists("files/anexo.pdf")){
download.file(
  url = "https://journals.plos.org/ploscompbiol/article/file?id=10.1371/journal.pcbi.1005619&type=printable", destfile = "files/anexo.pdf", mode = "wb")
  }
```

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

```{r appendices, child = 'anexo.Rmd', eval = FALSE}
```
