---
# System prepended metadata

title: Rmarkdown
tags: [r-learn]

---

---
tags: r-learn
---

this document is accessible at https://hackmd.io/@alper-yilmaz76/SysvUvk3u/edit

# Rmarkdown

* html documents
    * webpage (htmlwidgets)
    * blog
    * presentation
* pdf/word documents

## YAML header

you can have many options in header part. for full list, refer to ...

```yaml
---
title: "Example Title"
author: "alper yilmaz"
date: "`r Sys.Date()`"
output: 
  html_document:
    theme: paper
    highlight: pygments
    df_print: paged
    code_folding: hide
    number_sections: yes
    toc: yes
    toc_float:
      collapsed: no
      smooth_scroll: yes
---
```

## Chunk options

`warning, messages, cache, echo, eval` are some of chunk options. If you want consistent options for all chunks, then add this chunk on top

    ```{r setup, include=FALSE}
    library(knitr)
    knitr::opts_chunk$set(cache=TRUE, warning=FALSE,
                      message=FALSE, echo=TRUE, dpi=180,
                      fig.width=8, fig.height=6)
    ggplot2::theme_set(ggplot2::theme_light())
    ```

## Theme gallery 

* https://www.datadreaming.org/post/r-markdown-theme-gallery/
* https://bookdown.org/yihui/rmarkdown/appearance-and-style-1.html
* https://www.garrickadenbuie.com/blog/pandoc-syntax-highlighting-examples/

> You don't need Rstudio to knit documents:
> ```
> Rscript -e "rmarkdown::render('04-Rmarkdown.Rmd')"
> ```

## Custom css

you can apply custom css code to your Rmarkdown **for html documents**

```css
<style> 
body { 
  text-align: justify
  } 
</style>
```

## Tables

* For PDF, you can use $\LaTeX$ code. For PDF, you can also use `kable` package.
* For html, you can use [`gt`](https://gt.rstudio.com/) package for great *static* tables. You can use datatables [DT](https://rstudio.github.io/DT/) package for interactive tables.

## HTMLwidgets

Since HTML is very interactive thanks to JS, Rmarkdown documents can be quite interactive.

Please checkout [leaflet](http://rstudio.github.io/leaflet/) package which generates interactive maps.

## Parameterized reports

You can generate reports using a parameter. You can just change the parameter and then knit **OR** you can write a script generating many reports.

    ---
    title: "`r params$car` Report"
    author: "alper yilmaz"
    date: "7/6/2021"
    output: pdf_document
    params:
      car: "Merc"
    ---
  
    ```{r setup, include=FALSE}
    library(tidyverse)
    library(knitr)
    knitr::opts_chunk$set(cache=FALSE, warning=FALSE,
                        message=FALSE, echo=TRUE, dpi=180,
                        fig.width=8, fig.height=6)
    ggplot2::theme_set(ggplot2::theme_light())
    ```
  
    # A report about `r params$car` 
  
    ## The table
  
    Here's data about the car of interest. 
  
    ```{r echo=F}
    selected_car <- mtcars %>% 
      as_tibble(rownames = "model") %>%   
      filter(str_detect(model,params$car))
  
    selected_car %>% kable()
    ```
  
    There are `r nrow(selected_car)` cars for `r params$car`.
  
    ## The plot
  
    And here's the weight and qsec plot
  
    ```{r echo=F}
    mtcars %>% 
      as_tibble(rownames = "model") %>% 
      filter(str_detect(model,params$car)) %>% 
      ggplot(aes(wt, qsec)) + 
      geom_point()
    ```

You can generate many reports;
* using terminal:
   ```bash
   for i in Merc Mazda Toyota Datsun Hornet;  do \
     Rscript -e "rmarkdown::render('05-rmarkdown-parameter.Rmd',\
                 params=list(car='"$i"'),\
                 output_file=paste0('"$i"','.pdf'))"; \
  done
   ```
* in R;
   ```R
   cars <- c("Merc", "Mazda", "Toyota", "Datsun", "Hornet")
   for (the_car in cars){ 
     rmarkdown::render('05-rmarkdown-parameter.Rmd', 
                        params=list(car=the_car), 
                        output_file=paste0(the_car,'.pdf'))
   }
   ```


