Day 05 Mastery

R Markdown, Shiny & Production R

R Markdown produces reproducible reports combining code, output, and prose. Shiny builds interactive web applications from R. Today covers both — the tools

~1 hour Hands-on Precision AI Academy

Today's Objective

R is slow for loops — vectorize everything, or use Rcpp to call C++ from R for hot loops.

01

R Markdown

An .Rmd file combines YAML header (title, author, output format), prose (Markdown), and R code chunks (```{r}...```). knit() executes all chunks and renders the result to HTML, PDF, Word, or slides. Inline code: `r round(mean(x), 2)` embeds R output in prose. Parameters make reports dynamic: users change values without editing code. R Markdown is standard for academic papers, business reports, and data science documentation.

02

Shiny: Interactive Web Apps

Shiny apps have two parts: ui (the layout and input/output elements) and server (the reactive logic). Input widgets: sliderInput, selectInput, textInput, dateRangeInput, fileInput. Output renderers: renderPlot, renderTable, renderText, renderUI. Reactive expressions (reactive(), observe(), eventReactive()) re-execute when inputs change. Deploy to shinyapps.io (free tier available) or self-host with Shiny Server.

03

Production R: Performance and Packages

R is slow for loops — vectorize everything, or use Rcpp to call C++ from R for hot loops. The future package parallelizes code across CPU cores. data.table is 10-100x faster than dplyr for large datasets (>1M rows). Package development: devtools, roxygen2 for documentation, testthat for tests. CRAN has 20,000+ packages; Bioconductor adds 2,000+ bioinformatics packages.

r
r
# Shiny app: interactive histogram
library(shiny)
library(ggplot2)

ui <- fluidPage(
  titlePanel('Distribution Explorer'),
  sidebarLayout(
    sidebarPanel(
      selectInput('dataset', 'Dataset',
                  choices = c('mtcars', 'iris', 'diamonds')),
      selectInput('variable', 'Variable', choices = NULL),
      sliderInput('bins', 'Bins', min=5, max=50, value=20)
    ),
    mainPanel(
      plotOutput('histogram'),
      verbatimTextOutput('stats')
    )
  )
)

server <- function(input, output, session) {
  dataset <- reactive({ get(input$dataset) })

  observeEvent(input$dataset, {
    nums <- names(Filter(is.numeric, dataset()))
    updateSelectInput(session, 'variable', choices = nums)
  })

  output$histogram <- renderPlot({
    req(input$variable)
    x <- dataset()[[input$variable]]
    ggplot(data.frame(x=x), aes(x)) +
      geom_histogram(bins=input$bins, fill='steelblue', color='white') +
      theme_minimal()
  })

  output$stats <- renderPrint({
    req(input$variable)
    summary(dataset()[[input$variable]])
  })
}

shinyApp(ui, server)
💡
Use req() inside render functions to prevent errors when inputs haven't been set yet. req(input$variable) stops the render silently if input$variable is NULL — you see a blank output instead of an error message.

Supporting References & Reading

Go deeper with these external resources.

Docs
R Markdown, Shiny & Production R Official documentation for r programming.
GitHub
R Markdown, Shiny & Production R Open source examples and projects for R Markdown, Shiny & Production R
MDN
MDN Web Docs Comprehensive web technology reference

Day 5 Checkpoint

Before moving on, confirm understanding of these key concepts:

Course Complete
Return to Course Overview