---
title: "Example 17: Code Insertion"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Example 17: Code Insertion}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```
The **reporter** package allows users to insert code directly with 
`report_options(allow_code = TRUE)`. This feature is currently available only
for RTF and HTML outputs. 

### Outline{#top}

* [Insert RTF Code](#rtf)
* [Insert HTML Code](#html)

## Insert RTF Code{#rtf}

The **reporter** package let's you insert RTF code in data sets, titles,
footnotes, headers, and footers.

Let's prepare a data set with RTF code:
```{r eval=FALSE, echo=TRUE}
df <- read.table(header = TRUE, text = '
      var     label        A             B
      "ampg"   "\\li360 N"          "19"          "13"
      "ampg"   "\\li360 Mean"       "18.8"  "22.0 (4.9)"
      "ampg"   "\\li360 Median"     "16.4{\\sub mm}"        "21.4{\\super 2}"
      "ampg"   "\\li360 {\\cf12 Q1 - Q3}"    "15.1 - 21.2" "19.2 - 22.8"
      "ampg"   "\\li360 Range"      "{\\cf2 10.4 - 33.9}" "14.7 - 32.4"
      "cyl"    "\\li360 8\\line Cylinder" "\\ul 10 ( 52.6%) \\ul0" "4 ( 30.8%)"
      "cyl"    "\\li360 6\\line {\\cf11 Cylinder and more perhaps more}" "4 ( 21.1%)"  "3 ( 23.1%)"
      "cyl"    "\\li360 4\\line Cylinder" "\\ul 5 ( 26.3%) \\ul0"  "6 ( 46.2%)"')

```

<img src="../man/images/example17_1.png" alt="Example image A"/>

Observe that there are several RTF formatting codes in the data set:

* Indentation with `\\li`
* Subscript and Superscript with `\\sub` and `\\super`
* Coloring with `\\cf`
* Underline with `\\ul`

Now let's do some more manipulation of the RTF in the **reporter** code:
```{r eval=FALSE, echo=TRUE}
fp <- file.path(tempdir(), "example17.rtf")

# Create table
tbl <- create_table(df, first_row_blank = TRUE, borders = c("all")) |>
  stub(c("var", "label"), width = .8) |>
  define(var, blank_after = TRUE, label_row = TRUE,
         format = c(ampg = "\\i Here is a italic label \\i0.", 
                    cyl = "\\ul Under line label \\ul0")) |>
  define(A, label = "\\shad {{\\cf6 Group A}} {common::supsc('2')}\\shad0", align = "center", n = 19) |>
  define(B, label = "\\outl Group B \\outl0", align = "center", n = 13) |>
  footnotes("\\i\\fs14 * Italic Small Table Footnote \\fs18")

# Create report and add content
rpt <- create_report(fp, orientation = "portrait", output_type = "RTF",
                     font = "Times") |>
  report_options(allow_code = TRUE) |>
  page_header(left = "\\strike Strikethrough header \\strike0", right = "Study: Cars") |>
  titles("\\b\\i {{\\highlight7 This is yellow italic highlight}} \\b0\\i0", 
         "\\fs0\\fs28 {{\\cf2 This is a blue big title}} \\fs18") |>
  add_content(tbl) |>
  footnotes("\\i\\fs14 * Italic Small Report Footnote \\fs18") |>
  page_footer(left = "Left",
              center = "Confidential",
              right = "Page [pg] of [tpg]")

```

Note that in the **reporter** package, curly braces are normally reserved
for the glue replacement feature.  Therefore, if you want to use curly braces
as part of your RTF code insertion, they must be doubled, i.e.`{{\\cf6 Group A}}`. 
The single curly braces will still be available as glue replacements.

Here is the result:

<img src="../man/images/example17_2.png" alt="Example image A"/>

As seen above, all the RTF code insertion worked as expected:

* Header is formatted with strike through.
* Titles are formatted with yellow highlight, italic, bold, blue color, and bigger font size.
* Column labels are formatted with red, shadow, and outline.
* Row labels are formatted with italic and underline.
* Subscript and superscript are displayed.
* Data values are formatted with blue, purple, underline, and indentation.
* Footnotes are formatted with italic and smaller font size.

### RTF Color Table

The above example contains several references to the RTF color table.
Here is the complete color table: 

* `\cf1`  : Black
* `\cf2`  : Blue
* `\cf3`  : Cyan
* `\cf4`  : Lime Green
* `\cf5`  : Magenta
* `\cf6`  : Red
* `\cf7`  : Yellow
* `\cf8`  : White
* `\cf9`  : Navy Blue
* `\cf10` : Teal
* `\cf11` : Green
* `\cf12` : Purple
* `\cf13` : Maroon/ Dark Red
* `\cf14` : Olive
* `\cf15` : Gray
* `\cf16` : Light Gray

The above color references are the same as those used by SAS ODS RTF.

[top](#top)

## Insert HTML Code{#html}

The **reporter** package also allows you insert HTML code in data sets, titles,
footnotes, headers, and footers.

Let's prepare a data set with HTML code:

<img src="../man/images/example17_3.png" alt="Example image A"/>

Now let's do some more manipulation of the HTML in the **reporter** code:
```{r eval=FALSE, echo=TRUE}
fp <- file.path(tempdir(), "example17b.html")

# Create table
tbl <- create_table(dat, borders = "outside") %>%
  titles('<p style="color: blue; font-size: 18px;">Blue 18 px <b>Title</b>: Page [pg] of [tpg]</p>',
         "<b>Bold table title</b>") %>%
  footnotes("<i>Italic footnote: Page [pg] of [tpg]</i>", "Emphasize <sup>Super</sup>") %>%
  define(var, label = "Name") %>%
  define(A, label = "Treatment A mg<sup>2</sup>") %>%
  define(B, label = "Treatment B mg<sup>2</sup>")

# Create Report
rpt <- create_report(fp, output_type = "html", font = fnt,
                     font_size = fsz, orientation = "landscape") %>%
  report_options(allow_code = TRUE) %>%
  set_margins(top = 1, bottom = 1) %>%
  add_content(tbl) %>%
  add_content(tbl) %>%
  titles("<mark>Mark Title: Page [pg] of [tpg]</mark>") %>%
  footnotes("<small>Small footnote</small>: Page [pg] of [tpg]", borders = "none") %>%
  page_header('<code style="color:red;">Code Style Page [pg] of [tpg]</code>',
              right = '<del>Strikethrough</del>') %>%
  page_footer("Footer: Page <sub>[pg]</sub> of [tpg]",
              right = "Footer: Page <sub>[pg]</sub> of [tpg]")

res <- write_report(rpt)
```

<img src="../man/images/example17_4.png" alt="Example image A"/>

As seen above, all the HTML code insertion worked as expected:

* Headers are formatted with red code style and strike through.
* Titles are formatted with yellow highlight, blue 18px, and bold.
* Column headers are formatted with superscript.
* Data values are formatted with underline, indentation, color, bold, and background color.
* Footnotes are formatted with italic, superscript, and smaller font size.
* Footers are formatted with subscript.

[top](#top)

Next: [Example 18: Report Options](reporter-report_options.html)
