R Tidyverse Extended: Reports and Apps ๐โจ
Imagine youโre a chef. Youโve cooked an amazing meal (your data analysis). But how do you serve it beautifully? Thatโs what R Markdown and Shiny doโtheyโre your fancy plates and live cooking shows!
๐ฏ What Youโll Learn
Think of this journey like building a restaurant:
- R Markdown = Your beautiful menu (documents that show your work)
- Shiny Apps = Your live cooking station (interactive apps people can use)
๐ R Markdown Basics
What is R Markdown?
Imagine writing a letter to a friend. But this letter is magicalโit can run code AND show the results!
R Markdown is like a magic notebook where you can:
- Write words (like a story)
- Add code (that actually runs!)
- Show pictures, charts, and results
Real Life Example:
- A scientist writes their experiment + results in one place
- A teacher creates worksheets that show step-by-step math
- You create a report that updates automatically!
The Three Parts of R Markdown
Every R Markdown file has 3 ingredients:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. YAML Header (the recipe) โ
โ Title, author, output type โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 2. Text (the story) โ
โ Your explanations โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ 3. Code Chunks (the magic) โ
โ R code that runs! โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Your First R Markdown File
---
title: "My First Report"
author: "Your Name"
date: "2024-01-15"
output: html_document
---
This is the YAML headerโitโs like the cover page of your book!
After the header, you write normal text:
# Introduction
This is my amazing report about cats!
Simple, right? Just like writing in a notebook!
๐งฉ R Markdown Code Chunks
What Are Code Chunks?
Think of code chunks like little windows into a computer lab inside your document.
When you open a window (start a chunk), R runs your code. When you close it, the results appear in your document!
Creating a Code Chunk
A code chunk looks like this:
```{r}
# This code will run!
2 + 2
```
The result shows up automatically:
[1] 4
Naming Your Chunks
Give chunks names like you name your pets:
```{r my-first-plot}
plot(1:10)
```
Why name them?
- Easy to find later
- Better error messages
- Organized reports
Chunk OptionsโThe Remote Control
Chunk options control what you see:
| Option | What It Does | Example |
|---|---|---|
echo |
Show code? | echo=FALSE hides code |
eval |
Run code? | eval=FALSE skips running |
include |
Show anything? | include=FALSE hides all |
message |
Show messages? | message=FALSE hides notes |
warning |
Show warnings? | warning=FALSE hides warnings |
ExampleโHide the Code, Show Results:
```{r, echo=FALSE}
# User sees only the plot, not this code!
library(ggplot2)
ggplot(mtcars, aes(mpg, hp)) +
geom_point()
```
Global OptionsโRules for Everyone
Set rules for ALL chunks at the top:
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE
)
```
This is like posting classroom rules that everyone follows!
๐ค R Markdown Output Formats
Your Report Can Wear Different Outfits!
The same R Markdown file can become:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ YOUR .Rmd FILE โ
โ โ โ
โ โโโโโโโโโโผโโโโโโโโโ โ
โ โผ โผ โผ โ
โ HTML PDF Word โ
โ (web) (print) (edit) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
HTML Documents
---
output: html_document
---
Perfect for:
- Sharing online
- Interactive charts
- Anyone with a browser
Fancy HTML Options:
---
output:
html_document:
toc: true # Table of contents
toc_float: true # Floating sidebar
theme: flatly # Pretty colors
code_folding: hide # Hide/show code
---
PDF Documents
---
output: pdf_document
---
Perfect for:
- Printing
- Academic papers
- Official reports
Note: You need LaTeX installed!
Word Documents
---
output: word_document
---
Perfect for:
- Colleagues who love Word
- Documents that need editing
- Business reports
Multiple Outputs at Once!
---
output:
html_document:
toc: true
pdf_document: default
word_document: default
---
Now you can create all three!
๐๏ธ Shiny App Structure
What is Shiny?
If R Markdown is a book, Shiny is a video game!
Shiny creates live, interactive apps where users can:
- Click buttons
- Move sliders
- See results update instantly
The Two Actors in Every Shiny Play
Every Shiny app has two parts:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ SHINY APP โ
โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโ โ
โ โ UI โโโโโบโ SERVER โ โ
โ โ (Stage) โ โ (Backstage)โ โ
โ โโโโโโโโโโโ โโโโโโโโโโโโโ โ
โ โ
โ What users What happens โ
โ see & click behind scenes โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Basic Shiny App Template
library(shiny)
# UI - What users see
ui <- fluidPage(
titlePanel("My App"),
# Your buttons, inputs, outputs go here
)
# Server - The brain
server <- function(input, output) {
# Your calculations and logic go here
}
# Run the app
shinyApp(ui = ui, server = server)
Thatโs it! Three parts:
uiโ The stageserverโ The backstage crewshinyApp()โ โLights, camera, action!โ
File Structure Options
Option 1: Single File (app.R)
myapp/
โโโ app.R
Option 2: Two Files (for bigger apps)
myapp/
โโโ ui.R
โโโ server.R
๐จ Shiny UI Components
Building Blocks of Your Appโs Stage
Think of UI components like LEGO blocks!
Layout: The Stage Design
fluidPage โ Responsive, stretchy layout:
ui <- fluidPage(
titlePanel("Welcome!"),
sidebarLayout(
sidebarPanel(
# Controls go here
),
mainPanel(
# Results go here
)
)
)
This creates:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Welcome! โ
โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ โ
โ Controls โ Results โ
โ โ โ
โ โ โ
โโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโ
Input ComponentsโGetting User Choices
Inputs are like asking questions!
| Input Type | What It Does | Code |
|---|---|---|
| Text box | Type words | textInput("id", "Label") |
| Number | Type numbers | numericInput("id", "Label", 5) |
| Slider | Slide to pick | sliderInput("id", "Label", 1, 100, 50) |
| Dropdown | Pick from list | selectInput("id", "Label", choices) |
| Checkbox | Yes or no | checkboxInput("id", "Label") |
| Button | Click me! | actionButton("id", "Click!") |
ExampleโA Slider:
sliderInput(
inputId = "num", # ID (for server)
label = "Pick a number:",
min = 1, # Lowest
max = 100, # Highest
value = 50 # Starting point
)
Output PlaceholdersโWhere Results Appear
Outputs are like empty picture frames waiting for art:
| Output Type | Shows | Code |
|---|---|---|
| Text | Words | textOutput("id") |
| Verbatim | Code-style text | verbatimTextOutput("id") |
| Plot | Charts | plotOutput("id") |
| Table | Data tables | tableOutput("id") |
| UI | Dynamic UI | uiOutput("id") |
ExampleโA Plot Placeholder:
mainPanel(
plotOutput("myPlot") # Empty frame for a plot
)
โ๏ธ Shiny Server Function
The Backstage Magic
The server function is where the real work happens:
server <- function(input, output) {
# input = what user chose
# output = what user sees
}
Reading Inputs
Access user choices with input$id:
server <- function(input, output) {
# If user moved slider with id "num"
user_choice <- input$num
}
Creating Outputs
Use render* functions to create content:
| What You Want | Render Function |
|---|---|
| Text | renderText() |
| Plot | renderPlot() |
| Table | renderTable() |
| Print output | renderPrint() |
ExampleโShow What User Picked:
server <- function(input, output) {
output$result <- renderText({
paste("You picked:", input$num)
})
}
Complete Mini App Example
library(shiny)
ui <- fluidPage(
titlePanel("Number Doubler"),
sidebarLayout(
sidebarPanel(
sliderInput("num",
"Pick a number:",
min = 1, max = 50, value = 5)
),
mainPanel(
textOutput("doubled"),
plotOutput("barPlot")
)
)
)
server <- function(input, output) {
output$doubled <- renderText({
paste("Double is:", input$num * 2)
})
output$barPlot <- renderPlot({
barplot(input$num, col = "steelblue")
})
}
shinyApp(ui = ui, server = server)
Reactive ExpressionsโSmart Calculations
When calculations are expensive, use reactive():
server <- function(input, output) {
# Calculate once, use many times
doubled <- reactive({
input$num * 2
})
output$text1 <- renderText({
paste("Result:", doubled()) # Note the ()
})
output$text2 <- renderText({
paste("Plus 10:", doubled() + 10)
})
}
Reactives are like recipesโyou write them once and use them whenever needed!
๐ Putting It All Together
The Journey Map
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ ๐ R Markdown โ
โ โ โ
โ โโโ Basics (YAML + Text + Code) โ
โ โ โ
โ โโโ Code Chunks (```{r} ... ```) โ
โ โ โโโ Options: echo, eval, etc. โ
โ โ โ
โ โโโ Outputs (HTML, PDF, Word) โ
โ โ
โ ๐ฎ Shiny Apps โ
โ โ โ
โ โโโ Structure (ui + server + run) โ
โ โ โ
โ โโโ UI Components โ
โ โ โโโ Inputs (slider, text, etc.) โ
โ โ โโโ Outputs (plot, text, etc.) โ
โ โ โ
โ โโโ Server Function โ
โ โโโ input$ (read user choices) โ
โ โโโ output$ (send results) โ
โ โโโ reactive() (smart calcs) โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Key Takeaways
- R Markdown = Documents that run code and look beautiful
- Code Chunks = Magic windows where R code runs inside your document
- Output Formats = Same file, different outfits (HTML, PDF, Word)
- Shiny Structure = UI (stage) + Server (backstage) + Run
- UI Components = Inputs (ask user) + Outputs (show results)
- Server Function = The brain that connects inputs to outputs
๐ You Did It!
You now know how to:
- โ Create beautiful reports with R Markdown
- โ Control code chunks like a pro
- โ Export to any format you need
- โ Build interactive Shiny apps
- โ Design user interfaces
- โ Write server logic that responds to users
Your data analysis can now reach the worldโthrough documents AND live apps! ๐
