Reports and Apps

Back

Loading concept...

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:

  1. R Markdown = Your beautiful menu (documents that show your work)
  2. 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:

  1. ui โ€” The stage
  2. server โ€” The backstage crew
  3. shinyApp() โ€” โ€œ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

  1. R Markdown = Documents that run code and look beautiful
  2. Code Chunks = Magic windows where R code runs inside your document
  3. Output Formats = Same file, different outfits (HTML, PDF, Word)
  4. Shiny Structure = UI (stage) + Server (backstage) + Run
  5. UI Components = Inputs (ask user) + Outputs (show results)
  6. 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! ๐ŸŽ‰

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.