ages <- c(23, 35, 41, 29, 50)Lesson 2b: Inline Code for Reproducible Writing
Learning goals
By the end of this lesson, you will be able to:
- Explain what inline R code is
- Use inline code to report values in sentences
- Format numbers for professional writing
- Use inline code with summaries and model results
- Avoid common reproducibility mistakes
What is inline code?
Inline code is R code that runs inside a sentence. Instead of typing numbers by hand, you let R calculate and insert them automatically when the document is rendered.
This keeps your writing and your analysis synchronized.
Why inline code matters in public health
Public health decisions depend on accurate numbers. Inline code:
- prevents copy and paste errors
- ensures reported values match the analysis
- allows results to update automatically
- supports transparency and trust
In this course, all reported statistics should come from inline code whenever possible.
A simple example
First create a vector of values.
Now write a sentence using inline code.
This includes a backtick, the letter r, a space, your R code, and a closing backtick.
The sample included 5 participants, and the mean age was 35.6 years.
When you render the document, those numbers are inserted automatically.
What happens when data change?
Change one value.
ages[1] <- 28Now re-render the document.
The mean age is now 36.6 years.
Because the number comes from inline code, it updates automatically.
Formatting inline values
You will often need to format numbers.
Rounding
Use the round() function.
The average age was 36.6 years.
Percentages
prop_smoker <- 0.237Approximately 23.7% of participants were smokers.
Sample sizes
The analytic sample included 5 participants.
Inline code with small datasets
df <- data.frame(
bmi = c(21.7, 27.4, 31.2, 29.8, 26.3, 24.9),
smoker = c("No", "Yes", "No", "No", "No", "Yes")
)mean_bmi <- mean(df$bmi)
sd_bmi <- sd(df$bmi)
prop_smoker <- mean(df$smoker == "Yes")The mean BMI was 26.9 kg/m² (SD = 3.4), and 33.3% of participants reported smoking.
Inline code with regression models
m1 <- lm(bmi ~ smoker, data = df)
coef(m1)(Intercept) smokerYes
27.25 -1.10
b_smoker <- coef(m1)[2]Compared with non-smokers, smokers had an estimated BMI difference of -1.1 units in this sample.
Common mistakes to avoid
Avoid these errors:
- Typing numbers into your paper by hand
- Copying results from the Console
- Forgetting to re-render after changing data
- Using inline code before the object exists
If a number appears in your report, it should almost always come from inline code.
Course expectations
In Projects 1–4:
- summary statistics should be written using inline code
- regression coefficients should be reported using inline code
- tables and figures should be generated in R
This is how real scientific manuscripts are written.
Practice section
Create a section in your own homework file and complete the following:
- Create a numeric vector with at least 6 values
- Use inline code to report:
- sample size
- mean
- standard deviation
- sample size
- Change one value
- Re-render and confirm the sentence updates
Starter:
x <- c(4, 6, 9, 12, 15, 18)Write a sentence like:
The sample size was 6, the mean was 10.67, and the standard deviation was 5.35.
Key takeaways
Inline code connects your writing to your analysis. It keeps numbers accurate, makes your work reproducible, and is a core skill for data-driven public health.
Looking ahead
You will use inline code in every project, especially when writing your final manuscript in Quarto.