---
title: "Decorations on heatmaps"
author: "Zuguang Gu ( z.gu@dkfz.de )"
date: "`r Sys.Date()`"
output: 
    rmarkdown::html_vignette:
        width: 8
        fig_width: 5
vignette: >
  %\VignetteIndexEntry{4. Decorations on heatmaps}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---


```{r, echo = FALSE}
library(knitr)
knitr::opts_chunk$set(
    error = FALSE,
    tidy  = FALSE,
    message = FALSE,
    warning = FALSE,
    fig.align = "center"
)
suppressPackageStartupMessages(library(InteractiveComplexHeatmap))
```

<style>
img {
    background-color: #FFFFFF;
    padding: 2px;
    border: 1px solid #DDDDDD;
    border-radius: 3px;
    border: 1px solid #CCCCCC;
    margin: 0 5px;
}
</style>

In **ComplexHeatmap**, functions `decorate_*()` (_e.g._ `decorate_annotation()`) are used to add graphics after
the heatmaps are drawn. To also put the decoration graphics in heatmaps in
the Shiny app, user needs to wrap all the decoration code into a function and
send it with `post_fun` argument to `draw()` function. For example, the following
code generates a decorated heatmap:

```{r, eval = FALSE}
library(ComplexHeatmap)
m = matrix(rnorm(100), 10)
ht = Heatmap(m, name = "foo")
ht = draw(ht)
decorate_heatmap_body("foo", {
    grid.text("some text", gp = gpar(fontsize = 30))
})
```

To export it as a Shiny app also with decorations, the code needs to be slightly 
adjusted to:

```{r, eval = FALSE}
library(InteractiveComplexHeatmap)
post_fun = function(ht_list) {
    decorate_heatmap_body("foo", {
        grid.text("some text", gp = gpar(fontsize = 30))
    })
}
ht = draw(ht, post_fun = post_fun)
htShiny(ht)
```

<script>
document.write('<img src="https://jokergoo.github.io/images/decoration.png" width="100%" />');
</script>

`post_fun` is a self-defined function and always needs one argument `ht_list`.
Since `post_fun` is applied after the heatmaps are drawn, `ht_list` used in
`post_fun` actually contains the clustering results (if they are applied). Thus,
you can do something like follows in `post_fun`:

```{r, eval = FALSE}
post_fun = function(ht_list) {
    decorate_heatmap_body("foo", {
        row_dend(ht_list)
        row_order(ht_list)
        ...
    })
}
```

Note the decorations will not be applied on the sub-heatmap, so you will not see the decoration graphics there.

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

