7.5.3.Understand code chunks and exports - quanganh2001/Google-Data-Analytics-Professional-Certificate-Coursera GitHub Wiki

Hands-On Activity: Adding code chunks to R Markdown notebooks

Question 1

Overview

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

Earlier in this course, you created a visualization using the ggplot() function. You also learned how to create an R Markdown notebook. In this activity, you will combine and apply your knowledge by adding the code you used to create a visualization to a RMarkdown notebook.

By the time you complete this activity, you will be able to make and format an .rmd file containing the visualizations you created using ggplot2. This will allow you to track and share analysis and also share the R code you use to create visualizations. This will prove useful if you want to give other data analysts an interactive way to test out your code and better communicate findings to your stakeholders and colleagues.

Add code chunks to your RMarkdown notebook

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

  1. To start, log in to your RStudio Cloud account.
  2. Next, open a new R Markdown notebook and create a code block section. Notebook chunks can be inserted quickly using the keyboard shortcut Ctrl + Alt + I (Windows) or Cmd + Option + I (Mac). Code chunks can also be added using the Insert menu in the editor toolbar.

HvFR6i52TKixUeoudsyoww_d8a5c11acd854498ad061285ec8e30b8_image3

Code chunks are designated in R Markdown with delimiters. A delimiter is a character that indicates the beginning or end of a data item. In this case, the code chunk is marked using three ticks followed by a curly bracket, descriptive text, and a closed curly bracket. You then have an empty space to add the appropriate code. Here is the general syntax:

```{r}

```

When creating code chunks, it is useful to keep in mind that the output of the code chunk will appear immediately after the chunk when it is executed. Because of that, it is good practice to split chunks that produce multiple outputs into two or more chunks. That way, each code chunk only produces one output, which can be easier for users to execute and explore.

  1. Using the code from your ggplot() visualization, create two new chunks. Type the following in the first code chunk to call the required libraries, load the penguins data, and return a view of the penguins data:
```{r ggplot for penguin data}
library(ggplot2)
library(palmerpenguins)
data(penguins)
View(penguins)

Note that the only output from the code chunk is a tabular view of your data as result of the View function.

  1. Then, type the following in the second code chunk to create the visualization:
```{r ggplot for penguin data visualization}
ggplot(data = penguins) +
  geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
  1. Finally, run each code chunk to examine the results. You might recognize this visualization from a previous activity.

Congratulations! You can now add your visualizations to an R Markdown notebook. Code chunks are a great way to explain your data analysis process and allow other users to explore your work. Try adding as many code chunks to your R Markdown notebook as possible to make your notebook an interactive experience.

Confirmation and reflection

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

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?

A. It would appear as regular non-code text

B. There would be no change

C. It would appear as a code chunk but not be executable

D. An error message would replace the chunk

The correct answer is A. It would appear as regular non-code text. Explain: 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.

Question 2

In this activity, you created code chunks in an R Markdown file that will create visualizations with ggplot2. In the text box below, write 2-3 sentences (40-60 words) in response to each of the following questions:

  • How does adding code chunks improve the usability of your R Markdown file?
  • How does visualizing data in an R Markdown file differ from visualizing data in a program like Tableau?

Explain: Congratulations on completing this hands-on activity! A good response would include that visualizing with code chunks allows you to create data visualizations in the same document as text. This makes formatting reports much easier and simpler.

Adding code chunks to your R Markdown notebook can give other users an interactive way to understand your data analysis process and test out your code in their own RStudio console space. These can be useful for documenting your code and giving stakeholders a chance to explore the data.

Output formats in R Markdown

You can save this reading for future reference. Feel free to download a PDF version of this reading below:

Output formats available in RMarkdown.pdf

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.

1anMb9A5THqpzG_QORx6yw_149323e0e3ff44cf9ac91c53000167ff_Screen-Shot-2021-02-02-at-9 53 11-AM

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.

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.

Hands-On Activity: Exporting your R Markdown notebook

Question 1

Activity overview

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

In the last activity, you created a draft of your R Markdown notebook. Now, you will export and share it.

By the time you complete this activity, you will be able to export an R notebook as two different outputs: html and pdf. This will enable you to easily share your work with others to get feedback and demonstrate your R skills.

Export your R Markdown notebook

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

For this activity, you will export a notebook you created in previous activities as both an html document and a pdf.

You will use the Knit option to export your work. This will allow you to convert the Rmd file to another file type that is more readable and useful to other users.

To begin, follow these steps:

  1. Open a document in RStudio. Then, find the Knit button on the toolbar at the top of your document window. Click it to open a dropdown menu with a few export options.
  2. Click on the option that matches the file type you want to export your notebook as. Begin with html. Once you click Knit to HTML, your console might take a moment to process. When it is finished, it will automatically open your new file.

xWf5XcLKSHmn-V3Cymh5Ww_3272194bab8e4d12852b3c53bdf7adfd_Screenshot-2021-02-19-11 02 28-AM

  1. Now, repeat Step 2 but select Knit to PDF. Explore how these file formats are different from your original Rmd file.
  2. To download the files you exported, find them in the Files explorer in the bottom-right of the screen.
  3. Check the boxes of the file(s) you want to download and click the More dropdown.

RE38u1I4RPeN_LtSOJT3_Q_1ac0121d16f54dbf82723e7a8cdb42f1_Export

  1. Click Export… and name the file by a title that will help you find it later. Click Download. Now your exported file will be downloaded to your computer.

hDOWB9JlR7-zlgfSZQe_pA_ea59a87058384a16a09c55f0bd0505f1_Export2

After you have successfully exported and downloaded your R Markdown notebook, you can share it with a friend, colleague, or the discussion forums to get feedback. After you have gotten their feedback, you can make revisions and continue to share your work to refine your notebook even more.

Sharing your R Markdown notebook is a great way to give other users insights into your data analysis. R Markdown comes with a lot of different options for exporting notebooks, so you can choose the best file type for your needs.

Understanding how to export your R Markdown notebooks will make documenting and sharing your data analysis process easier. Now that you are familiar with this process, you can export your own notebooks for future projects.

Confirmation and reflection

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

When exporting an R Markdown file, which of the following file types can you Knit to? Select all that apply.

  • pdf
  • Word file
  • jpg
  • html

Explain: To export an Rmd file from RStudio, you can Knit the file to html, PDF, and Word file formats. This allows you to save, export, and download your R Markdown notebook in more accessible and readable formats that you can share in many professional settings.

Question 2

In this activity, you exported an Rmd file as an html file and a pdf. In the text box below, write 2-3 sentences (40-60 words) in response to each of the following questions:

  • What is an advantage of exporting as an html file or a pdf? Is there a format you think you’ll use more often?
  • How do you plan to use or store the Rmd files you export?

Explain: Congratulations on completing this hands-on activity! A good response would include that R Markdown is a useful tool for documenting and sharing your analysis process.

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, which you will learn more about in a future course.

Hands-On Activity: Using R Markdown templates

Question 1

Activity overview

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

In previous activities, you used Rmd files to complete analyses and even created and exported your own. In this activity, you’ll learn how to access and use customized R Markdown templates included in R packages.

Templates allow you to use customized documents and there are lots of R packages that include custom templates for a variety of purposes. For example, there are templates for academic journal articles, for interactive presentations, or for vignettes summarizing the contents of R packages.

By the end of this activity, you will be able to adapt pre-existing templates to the needs of your projects. This will enable you to save time and energy while you set up an Rmd file and allow you to focus on your analysis.

Example template in RStudio

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

You’re already familiar with a template in R Markdown from earlier videos and activities. When you create a new R Markdown document from the RStudio menu (File -> New File -> R Markdown), a default example document appears in the RStudio source editor:

u3xIeIOuSQy8SHiDrlkMbQ_0292252afd4a461d90a0a8b084cac34a_Screenshot-2021-03-10-8 12 47-PM---Display-2

Many customized templates in R packages have a similar structure that includes a YAML header, code chunks, and text headers.

R packages with templates

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

Some popular packages with templates for R Markdown include the following:

  • The vitae package contains templates for creating and maintaining a résumé or curriculum vitae (CV)
  • The rticles package provides templates for various journals and publishers
  • The learnr package makes it easy to turn any R Markdown document into an interactive tutorial
  • The bookdown package facilitates writing books and long-form articles
  • The flexdashboard package lets you publish a group of related data visualizations as a dashboard

Access the CV template in RStudio

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

To examine the CV template included in the “vitae” package, follow these steps:

  1. First, log in to RStudio.
  2. In the console, type install.packages("vitae") to install the vitae package.
  3. Type library(vitae) to load the package.
  4. You can access available templates in the R Markdown dialog box that appears when you create a new file. To create a new file in R Markdown, click File > New File > R Markdown.

D8lpzstjQb6Jac7LYxG-6g_2998381eb64c482dbf1d3e53604b9f29_Screen-Shot-2021-02-01-at-11 25 11-AM

  1. From the R Markdown dialog box, click From Template to access a list of R Markdown templates provided by all installed packages.
7qqFg9rsSISqhYPa7KiEkQ_26d76941a012421a9a07cb6e3104a96c_Screen-Shot-2021-03-12-at-5 01 15-AM

In the viewer, you may notice some template options from the vitae package: Curriculum Vitae (Awesome-CV format), Curriculum Vitae (Hyndman format), Curriculum Vitae (ModernCV format), etc. These are different types of CV templates.

  1. Scroll down and click Curriculum Vitae (Twenty seconds format).
  2. Add a name for the new file directory that will contain the files bundled in the template, such as “CV-Example.”
  3. Finally, click OK.

Convert the template to PDF format

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

The template will appear in the source editor pane. It contains a YAML header, code chunks, and text headers, just like the default example document that appears when you create a new R Markdown document. The example CV uses the scientist Marie Curie, the first woman to win a Nobel Prize (and the first person to win two Nobel Prizes). The YAML header contains entries for general information, such as name, address, phone number, and more.

vmoinwgETHuqIp8IBNx7SA_b8b8cff4d501436b98da5ea2d345576b_Screenshot-2021-03-10-8 16 00-PM---Display-2

If you scroll down, you’ll find header text that introduces separate sections for topics like personal information:

Nc0D0sguSW2NA9LILiltOA_e03010c63e634b8286fd9a25c95162ef_Screenshot-2021-03-10-8 17 24-PM---Display-2

To display the output format of the template, click Knit to render the file. You don't need to open the dropdown menu to select a format, as this template defaults to a pdf.

Note: If your browser blocks pop-ups and returns an error, make sure to click Try Again.

This will result in a pdf that displays the custom template for the CV:

NBzbmhtIRJqc25obSJSazw_5ce1583aefbc47d0b1569a5db92dc46c_Screenshot-2021-03-10-8 18 59-PM---Display-2

This pdf can be found and downloaded from the folder in the Files tab of the lower-right console.

The information in the YAML Header appears on the left side, and the information in the various sections appears on the right side.

You can replace these details with your own information to adapt the template for your own needs.

Confirmation and reflection

TOqxzuNFR2eqsc7jRVdnKg_a3c6611d874f403a923e10406b4f38a9_image4

Which of the following kinds of information can you include in this CV template? Select all that apply.

  • Instagram username
  • Profile picture
  • Email address
  • Twitter username

Explain: The template you used in this activity allows you to input an email address, profile picture, and Twitter username. You can replace the information in the template with your own to create a CV. Going forward, you can use R Markdown templates to start your documents with a pre-existing structure--leaving you with more time to focus on your content and analysis.

Question 2

In this activity, you used an R package to open a Markdown template and edited it with your own information. In the text box below, write 2-3 sentences (40-60 words) in response to the following question:

  • How might you use R Markdown templates in your future analysis?

Explain: Congratulations on completing this hands-on activity! A good response would include that R Markdown templates are a great resource for customizing your documentation.

You can use R Markdown templates for lots of different purposes. Exploring a variety of packages with unique templates will help you figure out which ones you might use to document your own analysis and conclusions. You can use R templates as starting points for reports, portfolio pieces, and other documents you will create in your career as a data analyst.

Test your knowledge on code chunks

Question 1

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

A. an .rmd file

B. an HTML report

C. a data item

D. a command line

Explain: 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?

A. HTML report

B. R notebook

C. Template

D. .rmd file

The correct answer is C. Template. Explain: 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?

A. ```{r }

B. +++{r }

C. ***{r }

D. ==={r }

The correct answer is A. ```{r }.

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

⚠️ **GitHub.com Fallback** ⚠️