Professional Output & Reporting
Communicating your analysis like a professional
📖 Concept Recap
R gives you powerful tools for formatting output that looks like a real professional report:
- cat() — print text with
\nnewlines and\ttabs; concatenates multiple values - sprintf() — C-style string formatting:
%s(string),%d(integer),%.2f(float 2 decimal),%-20s(left-aligned 20 wide) - paste() / paste0() — combine strings;
paste0()with no separator - format() — format numbers with
big.mark=",",nsmall=2 - strrep() — repeat a string:
strrep("=", 50)makes a 50-char divider
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:
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.
%-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”.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.
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.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.
\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.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.
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 →