2 Bookdown Code Samples

This article describes some code examples to handling formating of special objects like images and tables as part of this Bookdown project:

2.1 Code Blocks

Code blocks should be displayed as the following:

~~~{.lineAnchors .numberLines startFrom="350"}
Code can go here
The .lineAnchors option makes code carriage return if the text is too long on one line for .pdfs.
The .numberLines option enables line numbers
The startFrom="350" options makes line numbers start with 350
~~~

The following is the above block as fully rendered:

Code can go here
The .lineAnchors option makes code carriage return if the text is too long on one line for .pdfs.
The .numberLines option enables line numbers
The startFrom="350" options makes line numbers start with 350

2.2 Images

Images needs to list the full path from the root project folder. Additional details such as positioning can be controlled via the R code block.

```{r echo=FALSE, fig.cap="Graphic Caption", out.width = '75%', fig.align='center', fig.pos = 'H'}
knitr::include_graphics("articles/how-to-install-xyz/Example Graphic.png")
```

2.3 Tables

While Markdown has simpler syntax for tables, the following should be used for table data to ensure full compatibility with both web and pdf versions of this Bookdown project:

```{r echo=FALSE, message=FALSE, warning=FALSE, results='asis'}
# header row is first
tabledata = "Version Number,Date,Author,Description
1.0,08/02/2022,YOURNAME,Initial version
"
# read the above text as a csv file
thistable <- read.csv(text = tabledata, header=TRUE, check.names=FALSE)
# turn it into a kable table
kbl(thistable, align = "c", caption = "Version History") %>% # name of table
  kable_styling(latex_options = c("striped", "responsive")) %>%
  column_spec(1, border_left = T) %>% # column 1 for border left line
  column_spec(4, border_right = T) %>% # last column for border right line
  row_spec(0, bold = T, background = "#241556", color = "white")
```

This method already includes Delinea’s color branding.

This code block uses the kable package to design and build the table. Essentially, the tabledata shown above is treated as a String object, which is then read as a .csv. The kbl function then turns that csv into the table.

Check out (https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html) for all kinds of options.

The following is the example code block from above:

TABLE 2.1: Version History
Version Number Date Author Description
1 08/02/2022 YOURNAME Initial version