library(tidyverse)

1. Formatting HTML files

HTML files do not use templates, but can have the styles configured within the YAML. HTMLs are interactive documents, and are particularly flexible. We cover some basic options here.

  • Table of contents: We can add a table of contents with toc: true below, and also specify that it remains viewable (“floats”) as you scroll, with toc_float: true.

  • Themes: We can refer to some pre-made themes, which come from a Bootswatch theme library. In the below example we use cerulean. Other options include: journal, flatly, darkly, readable, spacelab, united, cosmo, lumen, paper, sandstone, simplex, and yeti, Bootswatch Themes

  • Highlight: Configuring this changes the look of highlighted text (e.g. code within chunks that are shown). Supported styles include default, tango, pygments, kate, monochrome, espresso, zenburn, haddock, breezedark, and textmate.

package # remotes::install_github(“gadenbuie/cleanrmd”) cleanrmd::html_document_clean: theme : NULL

2. Titles and headings

A hash symbol in a text portion of a R Markdown script creates a heading. This is different than in a chunk of R code in the script, in which a hash symbol is a mechanism to comment/annotate/de-activate, as in a normal R script.

2.1 Bullets and numbering

Use asterisks (*) to created a bullets list. Finish the previous sentence, enter two spaces, Enter/Return twice, and then start your bullets. Include a space between the asterisk and your bullet text. After each bullet enter two spaces and then Enter/Return. Sub-bullets work the same way but are indented. Numbers work the same way but instead of an asterisk, write 1), 2), etc. Below is how your R Markdown script text might look.

Here are my bullets (there are two spaces after this colon):

  • Bullet 1 (followed by two spaces and Enter/Return)
  • Bullet 2 (followed by two spaces and Enter/Return)
    • Sub-bullet 1 (followed by two spaces and Enter/Return)
    • Sub-bullet 2 (followed by two spaces and Enter/Return)

2.2 Comment out text

You can “comment out” R Markdown text just as you can use the “#” to comment out a line of R code in an R chunk.

2.3 New lines

Uniquely in R Markdown, to initiate a new line, enter *two spaces** at the end of the previous line and then Enter/Return.

2.4 Case

Surround your normal text with these character to change how it appears in the output.

  • Underscores _ (text) or single asterisk * (text) to italicise
  • Double asterisks ** (text) for bold text
  • Back-ticks (text) to display text as code The actual appearance of the font can be set by using specific templates (specified in the YAML metadata; see example tabs).

2.5 Color

There is no simple mechanism to change the color of text in R Markdown. One work-around, IF your output is an HTML file, is to add an HTML line into the markdown text. The below HTML code will print a line of text in bold red.

DANGER: This is a warning.

3. Images

-You can include images in your R Markdown one of two ways: Cim boom

-If the above does not work, try using knitr::include_graphics() knitr::include_graphics(“path/to/image.png”)

-(remember, your file path could be written using the here package) knitr::include_graphics(here::here(“path”, “to”, “image.png”))

4. Code chunks

Some notes about the contents of the curly brackets { }:

  • They start with ‘r’ to indicate that the language name within the chunk is R
  • After the r you can optionally write a chunk “name” – these are not necessary but can help you organise your work. Note that if you name your chunks, you should ALWAYS use unique names or else R will complain when you try to render.
  • The curly brackets can include other options too, written as tag=value, such as:
  • eval = FALSE to not run the R code
  • echo = FALSE to not print the chunk’s R source code in the output document
  • warning = FALSE to not print warnings produced by the R code
  • message = FALSE to not print any messages produced by the R code
  • include = either TRUE/FALSE whether to include chunk outputs (e.g. plots) in the document
  • out.width = and out.height = - provide in style out.width = “75%”
  • fig.align = “center” adjust how a figure is aligned across the page
  • fig.show=‘hold’ if your chunk prints multiple figures and you want them printed next to each other (pair with out.width = c(“33%”, “67%”). Can also set as fig.show=‘asis’ to show them below the code that generates them, ‘hide’ to hide, or ‘animate’ to concatenate multiple into an animation.
  • A chunk header must be written in one line
  • Try to avoid periods, underscores, and spaces. Use hyphens ( - ) instead if you need a separator.

5.Case Study

5.1 Datasets

5.2 Investment Annual Summary

The investment_annual_summary dataset provides a summary of the dollars in millions provided to each region for each fiscal year, from 2012 to 2018.

  1. East Asia and the Pacific
  2. Europe and Central Asia
  3. Latin America and the Caribbean
  4. Middle East and North Africa
  5. South Asia
  6. Sub-Saharan Africa
## # A tibble: 42 x 3
##    fiscal_year region                          dollars_in_millions
##          <dbl> <chr>                                         <dbl>
##  1        2012 East Asia and the Pacific                      2548
##  2        2012 Europe and Central Asia                        2915
##  3        2012 Latin America and the Caribbean                3680
##  4        2012 Middle East and North Africa                   2210
##  5        2012 South Asia                                     1312
##  6        2012 Sub-Saharan Africa                             2733
##  7        2013 East Asia and the Pacific                      2873
##  8        2013 Europe and Central Asia                        3261
##  9        2013 Latin America and the Caribbean                4822
## 10        2013 Middle East and North Africa                   2038
## # ... with 32 more rows
Figure 1.1 The Investment Annual Summary for each region for 2012 to 2018

Figure 1.1 The Investment Annual Summary for each region for 2012 to 2018

5.3 Investment Projects in Brazil

The investment_services_projects dataset provides information about each investment project from 2012 to 2018. Information listed includes the project name, company name, sector, project status, and investment amounts.

## # A tibble: 90 x 13
##    date_disclosed      country ifc_country_code sector              project_name
##    <dttm>              <chr>   <chr>            <chr>               <chr>       
##  1 2018-12-14 00:00:00 Brazil  BRA              Funds               Vinci Capit~
##  2 2018-11-13 00:00:00 Brazil  BRA              Telecommunications~ Cabo Teleco~
##  3 2018-10-24 00:00:00 Brazil  BRA              Financial Institut~ Frida       
##  4 2018-10-18 00:00:00 Brazil  BRA              Financial Institut~ Golden Grass
##  5 2018-07-18 00:00:00 Brazil  BRA              Health and Educati~ Cruzeiro Gr~
##  6 2018-07-05 00:00:00 Brazil  BRA              Financial Institut~ Contabilizei
##  7 2018-04-27 00:00:00 Brazil  BRA              Agribusiness and F~ Bauducco Gr~
##  8 2018-04-11 00:00:00 Brazil  BRA              Agribusiness and F~ Sao Manoel  
##  9 2018-03-14 00:00:00 Brazil  BRA              Infrastructure      Gas Natural~
## 10 2018-02-07 00:00:00 Brazil  BRA              Financial Institut~ WCS Bansicr~
## # ... with 80 more rows, and 8 more variables: project_number <dbl>,
## #   company_name <chr>, status <chr>,
## #   ifc_investment_for_risk_management_million_usd <dbl>,
## #   ifc_investment_for_guarantee_million_usd <dbl>,
## #   ifc_investment_for_loan_million_usd <dbl>,
## #   ifc_investment_for_equity_million_usd <dbl>,
## #   total_ifc_investment_as_approved_by_board_million_usd <dbl>
## Warning: Removed 8 rows containing missing values (geom_point).
Figure 1.2 The Investment Services Projects in Brazil from 2012 to 2018

Figure 1.2 The Investment Services Projects in Brazil from 2012 to 2018

5.4 Investment Projects in Brazil in 2018

## Warning: Removed 4 rows containing missing values (geom_point).
Figure 1.2 The Investment Services Projects in Brazil from 2012 to 2018

Figure 1.2 The Investment Services Projects in Brazil from 2012 to 2018

The total investment amount for all projects in Brazil in the 2018 fiscal year was 951.18 million dollars.