Learn Without Walls
← Back to R Practice Labs
Lab 9 of 10

Professional Output & Reporting

Communicating your analysis like a professional

← Lab 8: Tidyr Lab 9 of 10 Lab 10: Capstone →
⏳ Loading R... (first load takes ~15 seconds)

📖 Concept Recap

R gives you powerful tools for formatting output that looks like a real professional report:

The key is building report generator functions that take data and produce formatted, readable output automatically.

👀 Worked Example

A professional report generator using aligned columns and dividers:

generate_report <- function(data, title = "Analysis Report") { cat(strrep("=", 50), "\n", title, "\n", strrep("=", 50), "\n\n", sep="") cat("SUMMARY STATISTICS\n", strrep("-", 30), "\n", sep="") cat(sprintf("%-20s %10s\n", "Metric", "Value")) cat(strrep("-", 30), "\n", sep="") cat(sprintf("%-20s %10d\n", "Observations:", nrow(data))) cat(sprintf("%-20s %10.2f\n", "Mean Revenue:", mean(data$revenue))) cat(sprintf("%-20s %10.2f\n", "Median Revenue:", median(data$revenue))) cat(sprintf("%-20s %10.2f\n", "Std Deviation:", sd(data$revenue))) cat(strrep("-", 30), "\n", sep="") } sales <- data.frame(rep=c("Alice","Bob","Carol","David"), revenue=c(38300,29200,47500,28800)) generate_report(sales, "Q1 Sales Report")
✏️ Guided

Exercise 1 — Sales Leaderboard with Medals

Run this leaderboard printer and study the sprintf formatting. Then try adding a 4th and 5th place row without medals.

Output will appear here...
💡 Hint: %-12s means left-aligned, 12 characters wide — the - flag left-justifies text. %10s right-aligns to 10 chars. format(n, big.mark=",") adds thousands separators like “47,500”.
💪 Independent

Exercise 2 — Monthly Report Function

Write a monthly_report(sales_df) function that prints a complete professional report with: a header, summary stats table, top 3 performers section, monthly totals section, and a footer with a simulated timestamp.

Output will appear here...
💡 Hint: tapply(revenue, rep, sum) gives total revenue per rep. Wrap in sort(..., decreasing=TRUE) for the leaderboard order. Use names() to get the rep names from the sorted result.
🔥 Challenge

Exercise 3 — Side-by-Side Comparison Report

Write comparison_report(group1, group2, label1, label2) that prints: side-by-side stats, marks which group is higher with ▲, a plain-English t-test interpretation, and a text bar chart of the two means.

Output will appear here...
💡 Hint: \u25B2 is the Unicode up-arrow ▲. strrep("|", n) creates a text bar of length n. Scale bars to the maximum mean so the wider bar always fills to 20 characters.
🏆 Mini Project — Executive Summary Generator

Auto-Generated Executive Report

Build a complete exec_summary(sales) function that automatically generates a multi-section executive report from any sales data frame, including auto-written insight sentences.

Output will appear here...
💡 Hint: Store computed values in named variables at the top of the function so the insight sentences can reference them naturally: cat(sprintf("... %s ...", top_rep)). This makes the report auto-update when the data changes.

✅ Lab 9 Complete!

You can now build professional, auto-generated reports in R using sprintf, cat, and structured formatting. This skill transforms raw analysis into polished deliverables that communicate clearly to any audience.

Continue to Lab 10: Capstone Analysis →

← Lab 8: Tidyr Lab 9 of 10 Lab 10: Capstone →