7.5.2.R Markdown documents & Code chunks and exports - sj50179/Google-Data-Analytics-Professional-Certificate GitHub Wiki

Create R Markdown documents

Question

A data analyst inserts some code directly into their R Markdown file so that they can refer to it directly in their write-up. What is this called?

  • R notebook
  • Markdown
  • Inline code
  • YAML header

Correct. Inline code can be inserted directly into the text of an .rmd file.

Question

A data analyst wants to include a link in their RMarkdown document. They should use the syntax (click here)[rstudio.com].

  • True
  • False

Correct. This syntax is incorrect. The analyst should enclose the text that they want to appear in the document in brackets. The URL should be in parenthesis after that. This data analyst has switched the formatting. It should be click here.

Question

A data analyst wants to include the caption example 1 below an image (image URL: r-project.org/logo/Rlogo.png). What is the correct syntax for adding an image with this caption in an RMarkdown document?

  • ![example 1](r-project.org/logo/Rlogo.png)
  • +[example 1](r-project.org/logo/Rlogo.png)
  • =[example 1](r-project.org/logo/Rlogo.png)
  • %>%[example 1](r-project.org/logo/Rlogo.png)

Correct. The correct syntax for adding an image with the caption example 1 to an RMarkdown file is ![example 1](r-project.org/logo/Rlogo.png)*. The syntax is an exclamation mark followed by the caption in brackets and the image URL or pathway in parentheses.*

Test your knowledge about creating R Markdown documents

TOTAL POINTS 4

Question 1

What information does a data analyst usually find in the header section of an RMarkdown document? Select all that apply.

  • Conclusions
  • File type
  • Date
  • Title and author

Correct. The header section of an RMarkdown document contains the title, author, date, and file type.

Question 2

While formatting their R Markdown document, a data analyst decides to make one of the headers smaller. What do they type into the document to do this?

  • Brackets
  • Backticks
  • Hashtags
  • Parentheses

Correct. Hashtags can be used to make headers smaller. The more hashtags, the smaller the text.

Question 3

To create bullet points to their output document, a data analyst adds _____ to their RMarkdown document.

  • hashtags
  • brackets
  • asterisks
  • spaces

Correct. To create bullet points to their output document, a data analyst adds asterisks to their RMarkdown document.

Question 4

A data analyst wants to embed a link in their RMarkdown document. They write (click here!)(www.rstudio.com) but it doesn’t work. What should they write instead?

  • [click here!](www.rstudio.com)
  • "click here!"(www.rstudio.com)
  • click here!(www.rstudio.com)
  • <click here!>(www.rstudio.com)

Correct. The analyst should write [click here!](www.rstudio.com)*. The text to be linked should be bracketed. The parentheses around the URL itself are correct.*

Understand code chunks and exports

Question

Fill in the blank: Code added to a(n) _____ file is usually called a code chunk.

  • rmd
  • markdown
  • HTML
  • data

Correct. Code added to a .rmd file is usually called a code chunk.

Question

A data analyst writes in a code chunk and puts three backticks at the end of their code to mark the end of the data item. What are these backticks?

  • Delimiter
  • Inline code
  • YAML
  • Metadata

Correct. The delimiter is a character that indicates the beginning or end of a data item. In RMarkdown, {r }** *and* ** (shortcut: ctrl+alt+I) can be used as delimiters for code chunks.

Question

In R Markdown, you use delimiters--in this case, tick marks--to designate a code chunk. What would happen to your code chunk if you formatted it with two marks instead of three?

  • An error message would replace the chunk
  • There would be no change
  • It would appear as regular non-code text
  • It would appear as a code chunk but not be executable

Correct. If you format a code chunk with two ticks inside of three, it would appear as regular non-code text. Delimiters are characters that designate code, so it is important that they be in the correct syntax. Going forward, you can pay attention to delimiters to guarantee your R Markdown notebook will run your code properly.

Output formats in R Markdown

This reading will explore the different types of output formats you can produce with R Markdown.

Setting the output of an R Markdown document

When working in RStudio, you can set the output of a document in R Markdown by changing the YAML header.

For example, the following code creates an HTML document:

--
title: "Demo"
output: html_document
--

And the following code creates a PDF document:

--
title: "Demo"
output: pdf_document
--

The Knit button in the RStudio source editor renders a file to the first format listed in its output field (HTML is the default). You can render a file to additional formats by clicking the dropdown menu next to the knit button.

Available document outputs

In addition to the default HTML output (html_document), you can create other types of documents in R Markdown using the following output settings:

  • pdf_document – This creates a PDF file with LaTeX (an open source document layout system). If you don’t already have LaTeX, RStudio will automatically prompt you to install it.
  • word_document – This creates a Microsoft Word document (.docx).
  • odt_document – This creates an OpenDocument Text document (.odt).
  • rtf_document – This creates a Rich Text Format document (.rtf).
  • md_document – This creates a Markdown document (which strictly conforms to the original Markdown specification)
  • github_document – This creates a GitHub document which is a customized version of a Markdown document designed for sharing on GitHub.

For a detailed guide to creating different types of R Markdown documents, check out the Documents chapter in R Markdown: The Definitive Guide.

Notebooks

A notebook (html_notebook) is a variation on an HTML document (html_document). Overall, the output formats are similar; the main difference between them is that the rendered output of a notebook always includes an embedded copy of the source code.

Notebooks and HTML documents also have different purposes. HTML documents are good for communicating with stakeholders. Notebooks are better for collaborating with other data analysts or data scientists.

To learn more, check out the section on Notebooks in the R Markdown documentation.

Presentations

You can also use R Markdown to produce presentations. Automatically inserting the results of your R code into a presentation can save you lots of time.

R Markdown renders files to specific presentation formats when you use the following output settings:

  • beamer_presentation – for PDF presentations with beamer
  • ioslides_presentation – for HTML presentations with ioslides
  • slidy_presentation – for HTML presentations with Slidy
  • powerpoint_presentation – for PowerPoint presentations
  • revealjs : : revealjs_presentation – for HTML presentations with reveal.js (a framework for creating HTML presentations that requires the reveal.js package)

To learn more, check out the section on Slide Presentations in the R Markdown documentation.

Dashboards

Dashboards are a useful way to quickly communicate a lot of information. The ****flexdashboard package lets you publish a group of related data visualizations as a dashboard. Flexdashboard also provides tools for creating sidebars, tabsets, value boxes, and gauges.

To learn more, visit the flexdashboard for R page and the Dashboards section in the R Markdown documentation.

Shiny

Shiny is an R package that lets you build interactive web apps using R code. You can embed your apps in R Markdown documents or host them on a webpage.

To call Shiny code from an R Markdown document, add  runtime: shiny to the YAML header:

--
title: "Shiny Web App"
output: html_document
runtime: shiny
--

To learn more about Shiny and how to use R code to add interactive components to an R Markdown document, check out the Shiny tutorial from RStudio.

Other formats

Other packages provide even more output formats:

  • The bookdown package is helpful for writing books and long-form articles.
  • The prettydoc package provides a range of attractive themes for R Markdown documents.
  • The ****rticles ****package provides templates for various journals and publishers.

Visit the RStudio Formats page in the R Markdown documentation for a more comprehensive list of output formats and packages.

Additional resources

For more information, check out these additional resources:

  • The R Markdown gallery from RStudio has tons of examples of the outputs you can create with R Markdown.
  • The R Markdown Formats chapter in the R for Data Science book provides more details about the output formats introduced in this reading. This reading was compiled from information in this book.

Question

What is an advantage of exporting as an html file or a pdf?

  • Data analysts share their work in a variety of formats, such as pdfs, html files, and R Markdown notebooks. Understanding how to export or convert your work into any of these formats will help you be flexible in how you share it. As html files and pdfs, your work can be attached to emails or uploaded to a cloud-based file sharing platform like Google Drive. This also allows you to share your analysis with potential employers when you search for a job as a data analyst.

Test your knowledge on code chunks

TOTAL POINTS 3

Question 1

Fill in the blank: A delimiter is a character that marks the beginning and end of _____.

  • a command line
  • an HTML report
  • a data item
  • an .rmd file

Correct. A delimiter is a character that marks the beginning and end of a data item. It can mark a single line of code, or a whole section of code in an .rmd file.

Question 2

A data analyst has to create a monthly report for their stakeholders. What can they create to help them save time generating these reports?

  • Template
  • HTML report
  • .rmd file
  • R notebook

Correct. Creating a template for your reports allows you to run one line of code to update your data without having to recreate the report from scratch. Templates can also help you customize the appearance of your final report.

Question 3

A data analyst wants to mark the beginning of their code chunk. What delimiter should they type in their .rmd file?

  • **{r }
  • +++{r }
  • ````{r }`
  • ==={r }

Correct. Three backticks followed by the letter r in braces (````{r }`) indicates the beginning of a code chunk in an .rmd file.