0% found this document useful (0 votes)
4 views84 pages

01 RMAID Introduction

The document outlines a workshop on R Markdown and interactive dashboards led by RStudio educators, focusing on equipping participants with data literacy skills. It includes setup instructions, a schedule of activities, and exercises for creating R Markdown documents and dashboards. Participants will learn to use RStudio tools, knit documents, and publish insights while emphasizing collaboration and experimentation.

Uploaded by

kulchi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views84 pages

01 RMAID Introduction

The document outlines a workshop on R Markdown and interactive dashboards led by RStudio educators, focusing on equipping participants with data literacy skills. It includes setup instructions, a schedule of activities, and exercises for creating R Markdown documents and dashboards. Participants will learn to use RStudio tools, knit documents, and publish insights while emphasizing collaboration and experimentation.

Uploaded by

kulchi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link].

com

R Markdown and Interactive Dashboards


Introduction
Carl Howe, Director of Education, RStudio
Kelly O'Brient, Solutions Engineer, RStudio

[Link]/RMAID
RStudio's Mission:

Equip everyone, regardless of means,


to participate in a global economy that
rewards data literacy

!2
RStudio Education's mission:

Train the next million R users


#NextMillionRUsers

!3
GETTING SET UP

! [Link]/RMAID
WORKSHOP MATERIALS
OPTION 1 OPTION 2

Cloud

log in and sit tight Clone or download


[Link]/2m2azap

! [Link]/RMAID
! [Link]/RMAID
IF YOU RUN LOCALLY, YOU NEED TO INSTALL PACKAGES
The list is in the top level directory as [Link]. They are also listed here.
• library(geonames)
• library(dplyr)
• library(magrittr)
• library(rmarkdown)
• library(flexdashboard)
• library(leaflet)
• library(babynames)
• library(tufte)
• library(rticles)
• library(shiny)
• library(DT)
• library(reticulate)
• library(revealjs)
• library(plotly)

[Link]/RMAID
ASKING FOR HELP

I'm stuck I'm not stuck, but I need help I need help
on my computer understanding something
(which likely means others
do too)
! [Link]/RMAID
SCHEDULE
Time Content
10:00-10:45 Introduction and your first dashboard

10:45-11:00 Break

11:00-12:30 R Markdown Basics and Publishing

12:30-13:30 Lunch

13:30-15:00 Adding Interactivity and Web-based Dashboards

15:00-15:15 Break

15:15-16:30 Shiny and Putting It All Together

16:30-17:00 Q&A, Recap, and Wrap-up


[Link]/RMAID
The [Link] space for this course is
[Link]/RMAID

The github repository for all the materials is at


[Link]/2m2azap
THINGS YOU SHOULD KNOW
• There are no stupid questions
• Because we have limited time, please write down questions as they occur to you,
and then ask them when the instructor pauses for questions
• Don't be afraid to experiment and try things out; you won't blow up anything
important if you make a mistake.

[Link]/RMAID
RSTUDIO TECHNOLOGIES WE'LL USE
• RStudio Integrated Development Environment (the RStudio IDE)
• [Link] interactive service
• The R language (naturally)
• The R Markdown document language
• The Tidyverse series of R packages
• HTML Widgets
• Shiny interactivity

[Link]/RMAID
WHAT YOU'LL LEARN TODAY
Today you'll learn how to
• Use the RStudio IDE and [Link]
• Knit an R Markdown document
• Create reports in R Markdown
• Turn static reports into dynamic content
• Apply web browser and Shiny interactivity
• Publish your insights to a larger web audience

[Link]/RMAID
WHY R MARKDOWN AND
INTERACTIVE DASHBOARDS?
Your Turn
Form groups of 2-4 people. Introduce yourself to your group members. Tell
them:
1. Who you are
2. What you do with data
3. How long you have been using R
4. What do they want to do with R Markdown and Interactive Dashboards?
[Link]
AND
YOUR FIRST DASHBOARD
IMPORTANT BUTTONS

Run Chunk
Run Allows you to run a single code chunk

Allows you to run a code


chunk or your whole
document
[Link]/RMAID
IMPORTANT BUTTONS

Run Chunk
Knit Run Allows you to run a single code chunk
Knits all your text and code
into a finished document Allows you to run a code
chunk or your whole
document
[Link]/RMAID
IMPORTANT BUTTONS Publish
Publish your document to presentation
services such as RStudio Connect or
[Link]

Run Chunk
Knit Run Allows you to run a single code chunk
Knits all your text and code
into a finished document Allows you to run a code
chunk or your whole
document
[Link]/RMAID
IMPORTANT BUTTONS Publish
Publish your document to presentation
services such as RStudio Connect or
[Link]

Run Chunk
Return to Project List Knit Run Allows you to run a single code chunk
Leaves this project Knits all your text and code
and returns into a finished document Allows you to run a code
you to the list of projects chunk or your whole
in this space document
[Link]/RMAID
IMPORTANT BUTTONS Publish
Publish your document to presentation
services such as RStudio Connect or
[Link]

Top Menu
Go somewhere
else in [Link]

Run Chunk
Return to Project List Knit Run Allows you to run a single code chunk
Leaves this project Knits all your text and code
and returns into a finished document Allows you to run a code
you to the list of projects chunk or your whole
in this space document
[Link]/RMAID
Don't Panic!
[Link] also has Primers for
helping you learn more of the details
of the language once this class is
done.

[Link] also has Cheat Sheets


for the IDE functions and for all the
packages we are using!

! [Link]/RMAID
ACCOUNT SETUP FOR [Link]
• This process allows us to use real-time weather and earthquake data
• Please set up an account for yourself at [Link]
• Only a user name, email account, and password are required; no other information is
gathered

[Link]/RMAID
EXERCISE 1
Knit Your First Document

[Link] on [Link]
in the files pane to open that file
2. Change the geonames_account parameter
to the account name you created a few moments
ago.
2. Click Knit above the source pane
3. Admire the result.

WARNING: you may need to disable your browser


pop-up blocker to see the result
YOU SHOULD SEE SOMETHING LIKE THIS

! [Link]/RMAID
EXERCISE 2
Edit Your Dashboard To Use A Different City

1. Find the lines following the word params:


2. Change the value Boston to the name of
another city.
3. Change the value KBOS to the ICAO airport
code for an airport in that city (you can look up
the ICAO code for any major airport using Google)
4. Knit your dashboard again and see the new city
temperature gauge and map location.
Congratulations! You have created your first
dashboard!

Time Check: 20 minutes


WHAT YOU'VE LEARNED
You've already learned how to
• Launch a project in [Link]
• Edit a file
• Knit a file into a result

[Link]/RMAID
THE ANATOMY OF AN R MARKDOWN DOCUMENT
You'll learn
• What R Markdown is
• How to create your own R Markdown document
• The components of an R Markdown document
• How to customize the content R Markdown documents

! [Link]/RMAID
R Markdown is a language for
creating computational
documents*
Donald Knuth's name for computational documents was Literate
Programming, which he defined in a book of the same name. I prefer
computation documents because it is more intuitively descriptive.
EXERCISE 3

Create Your Own R Markdown Document

1. Choose New File from the File Menu


2. Pick Document, select HTML as your
output type, and title it My First R
Markdown Document
3. Select Knit to render your document
THIS IS OK; CLICK TRY AGAIN

! [Link]/RMAID
YOU SHOULD SEE SOMETHING LIKE THIS

! [Link]/RMAID
YAML* Header Data About Your Document
(Metadata)
*YAML: Yet Another Markdown Language

Everything between --- and --- is read as


key value pairs

!41
Metadata for an R Markdown
document is written at the
top of the document in YAML
Things To Know About YAML
• output selects what type of file your R Markdown
will generate
• If you indent YAML lines, they must be indented 2
spaces and those lines will become sub-parameters
(we'll get to this later)
• If you omit or modify one or both --- lines, you may
get strange output and very unhelpful errors
R Markdown flags structural
elements of our text for
special treatment
Text Headers
The # signs indicate that this is a heading line

# = Level 1 heading
## = Level 2 heading
### = Level 3 heading

When we get to dashboards, you'll find that these headings are


interpreted differently according to what kind of dashboard layout
you are using:

# = New dashboard page


## = New column or row on this dashboard page
### = New tab on this column or row

This will make more sense when we discuss layouts.

!45
Ordinary Text
Any text you type will flow into the resulting report unchanged,
although lines may be wrapped.
Use *text* for italics, or **text** for bold.

!46
Code chunks delimited by
``` glyphs allow us to add
computation to our
document.
Code Chunks
R Code should start with ```{r chunk_name} and end with ```.
Any output generated by the code will be inserted into your knit
document.

!48
RECAP

Text Gets Reformatted

• Headers get formatted


• Ordinary text flows through

! [Link]/RMAID
RECAP: R MARKDOWN GENERATES THIS OUTPUT

Code Chunks Get Run

• Output gets put into the document.


• If the echo option is true, the code run will also be shown in the
document

! [Link]/RMAID
RECAP: R MARKDOWN GENERATES THIS OUTPUT

Output Includes Plots and Graphs

• Static plots show up in the output document


• Code chunk isn't shown because we included a chunk option
turning off the echo option

! [Link]/RMAID
EXERCISE 4
Modify Your R Markdown Document

1. Add the text library(ggplot2)to the setup code chunk


2. Add a new section with a header, text, and a code chunk at the bottom.
3. In the code section, add code to create a ggplot graphic of the pressure data
set. The following is code you can copy if you don't know ggplot:
ggplot(pressure, aes(temperature, pressure)) + geom_point()
4. Test your ggplot code chunk using the green arrow.
5. When you're happy with your ggplot, knit the final document.
6. Save your file as the file name [Link]
Your Turn
In your group, discuss the following questions:
1. Why are chunks scattered through the R Markdown
document? Why not put all the code in one chunk?
2. Did you notice anything special about the setup R
chunk when you ran just your ggplot chunk?
DISCUSSION TAKEAWAYS
• Allowing text and code in the same document allows us to create what Donald Knuth
called literate programming, a type of holistic programmable document
• The setup chunk is special. When you run any code chunk in your document, the
setup chunk is guaranteed to have been run previously. It's a great place to put
global parameter settings and library statements.

[Link]/RMAID
References and Resources

Time Check: 40 minutes


YOU'VE KNIT THE DOC, NOW GET THE CHEAT SHEET!

cheatsheets/[Link]

[Link]

! [Link]/RMAID
[Link]
! [Link]/RMAID
R MARKDOWN: THE DEFINITIVE GUIDE
Free!
[Link]

Also available from CRC Press, Amazon, Apple Books

! [Link]/RMAID
MORE FORMATTING TYPES
TEXT AND HEADERS
• Text can be plain text or decorated as *italic* or **bold**
• Headers use #s
# Header 1
## Header 2
### Header 3

[Link]/RMAID
NUMBERED SECTIONS

[Link]/RMAID
Show / hide
Tips
document
outline
LISTS
• Lists can be ordered Hint: you don't have to keep track of the numbers
1. Item 1 in an ordered list. You can just put 1. at the front of each
2. Item 2 item, and R Markdown will supply the proper item numbers.
+ Item 2a
+ Item 2b
• And lists can be unordered
* Item 1
* Item 2
+ Item 2a
+ Item 2b

[Link]/RMAID
LINKS
• Links can be plain http address or they can add a link to a phrase:
• [Link]
• [R Markdown website]([Link]

[Link]/RMAID
IMAGES
• Including an image is very similar to hyperlinking
• Images can be on the web:
![RStudio logo]([Link]
uploads/2014/04/[Link])
• Or they can be Iocally stored:
![RStudio logo](img/[Link])

[Link]/RMAID
TIPS
• To improve the accessibility of your document, always add alt text to you images.
• To print the alt text underneath the image as a caption, use fig_caption: true
in the YAML, and make sure there is a line break before the figure call.

[Link]/RMAID
REFERENCE STYLE LINKS AND IMAGES
• Links
• A [linked phrase][id]
• At the bottom of the document: [id]: [Link] "Title"
• Images
• ![alt text][id]
• At the bottom of the document: [id]: figures/[Link] “Title"
• Useful if you’ll be linking to the same target/image multiple times throughout the
document

[Link]/RMAID
MATH TEXT
• If you already know some LaTeX, you’re good to go
• Equations can be inline:
$\bar{x} \sim N (\mu, \frac{\sigma}{\sqrt{n}})$

• And equations can be centered in a new line:


$$ \bar{x} \sim N (\mu, \frac{\sigma}{\sqrt{n}}) $$

[Link]/RMAID
MATH TEXT (CONTINUED)
• If your LaTeX is rusty or non-existent, you can try out equations in the RStudio IDE
using an equations code chunk, and RStudio will show you how it will render.
• The equation code chunk starts and ends with $$

[Link]/RMAID
EXERCISE 5
Add An Equation to Your R Markdown Document

1. Still using [Link], add the


following text after your last chunk:

$$
E = mc^{2}
$$

You should see Einstein's equation appear below the code


chunk. Don't forget the curly braces around the 2. If you have
time, try some other simple equations.
TABLES | Column A | Column B | Column C |
• Two components let you |-----------|-----------|-----------|
define tables: | Value 1A | Value 1B | Value 1C |
| Value 2A | Value 2B | Value 2C |
• A header row that starts
and ends with a vertical
bar
• A separator row starting
and ending with a vertical
bar containing hyphens
and optionally colons
• Hyphens separate the
header row from content
cells, and vertical bars
separate the columns
[Link]/RMAID
TABLE ALIGNMENT
| Left-Aligned | Right Aligned | Centered |
|:-------------|--------------:|:----------:|
| Value 1A | Value 1B | Value 1C |
| Left | Right | Centered |

Colons in the row that


separates the header
from the values let you
left-align, right-align, and
center your columns

[Link]/RMAID
TABLE INPUT FORMATTING DOESN'T MATTER
| Left-Aligned | Right Aligned | Centered |
|:-|-:|:-:|
|Value 1A|Value 1B|Value 1C|
|Left|Right|Centered|

Note that you don't have


to format your input so
that things line up -- the
vertical bars and hyphens
are all you need

[Link]/RMAID
TABLE FONT DIRECTIVES (HTML ONLY)
| Left-Aligned | Right Aligned | Centered |
You can modify some |:-|-:|:-:|
font attributes using html |Left|Right|Centered|
directives of the form: |<font color="red">Red</font>|<font size="10pt">Big</font>|<font
face="times" color="blue">Times Blue</font>|
<font color="red"
size="10pt"
face="times">text
</font>

Only do this for small


local changes. for
document wide changes,
you would want to use a
custom CSS file.

[Link]/RMAID
MARKDOWN QUICK REFERENCE
In RStudio: Help ➡ Markdown Quick Reference

[Link]/RMAID
EXERCISE 6
Add A Table to Your R Markdown Document

1. Still using [Link], add a table to the bottom


of your document that recreates the table to the right.

Don't forget Help ➡ Markdown Quick Reference


TIPS
• Keep your text to max ~80 characters across, especially if you use a version control
system (like git)
• Starting a list? Leave an empty line before the first item on your list
• Need to test out bits of markdown code without knitting the entire document? Start
another document with bits and pieces of code to test them out

[Link]/RMAID
EXERCISE 7
Turn Plain Text Into A Report

1. Open [Link]
2. Add R Markdown notation to make it
render as shown at right
3. You can view a file showing you are
trying to produce in answers/07-Open-
[Link]
You'll have to view that in a browser.

Don't forget Help ➡ Markdown Quick


Reference
WHAT HAVE WE FORGOTTEN? CODE CHUNKS
• Up until this point, we've focused our efforts on R Markdown text.
• But the essence of a computational document is code.
• We put our code in code chunks
• Code chunks look like this:

```{r chunk_name parameter1=value, param2=value}


print("My R code goes here")
```

[Link]/RMAID
R Markdown code chunks
support many languages.
CODE CHUNKS SUPPORT MANY LANGUAGES
• R and Python are the most common in R Markdown documents
• However, R Markdown also supports
• SQL
• Bash
• Rcpp
• Stan
• JavaScript
• CSS

[Link]/RMAID
EXERCISE 8
Observe R Markdown's Multi-Language Support

1. Open [Link] by clicking on it in the File menu


2. Read and either run or knit the code
3. Where does the value py$python_string come from?
SUMMARY
• R Markdown is a language for creating computational documents.
• Metadata for an R Markdown document is written at the top of the document in ____
• R Markdown documents can include a variety of formatting elements
• Headers
• Plain text
• Code chunks in several languages
• Links
• Tables
• Images
• More
[Link]/RMAID
Break

[Link]/RMAID

You might also like