How to refer to R results in text

This is a primer on how to use inline R Expressions in an R Markdown document.

First we load some sample data, calculate test score means by school, save them as data frame and display the results as formatted table (see script “R Markdown Tables” for details):

setwd("//mcifs02/homes$/NiMevenkamp/MCI/Lehre/09-AdvancedStatistics/ihsm24/")

df = read.xlsx("SchoolData.xlsx") 

means_df = data.frame(
  by(df$test_score, df$school_long_name, mean)
  )
names(means_df) = c("mean_score")

kable_styling(
  kable(means_df,
        col.names = c("School","Average Test Score")
        ),
  full_width = F, font_size = 13, bootstrap_options = c("hover", "condensed")
)
School Average Test Score
Hillcrest High School 48.84623
Oakwood High School 39.32777
St Mary’s Church of England School 47.19057

In addition to showing the results as “external output”, we can refer to any R output as “inline expression”, too. To make this work, we have to wrap the R Expression into `r `. For example, the function nrow(df) returns the number of rows in the data frame named df:

# By default, all code chunks are hidden. 
# "class.source = 'fold-show'" causes this chunk to be initially shown
nrow(df)
## [1] 183

If we want to make a statement about the size of our data set in fluent text, we type

The school data set comprises of n=`r nrow(df)` rows.

In the compiled document this reads as

The school data set comprises of n=183 rows.

With this technique we can make some general statements about the structure of our data, for example that students worked for 8.899 hours on average and received an average test score of 44.769.

We can also add some technical information: This document was built with R (Version 4.4.1) using packages knitr (1.47) and kableExtra (1.4.0) .

For more advanced examples, appropriate data can be collected before use. This simplifies inline coding.

# get school  names
school = rownames(means_df)
# print school  names
school
## [1] "Hillcrest High School"              "Oakwood High School"               
## [3] "St Mary’s Church of England School"
# get test scores
mean_score = round(means_df$mean_score, 3)
# print test scores
mean_score
## [1] 48.846
## ------------------------------------------------------------ 
## [1] 39.328
## ------------------------------------------------------------ 
## [1] 47.191

By referring to this data we can now comment at school level as follows:

The data set comprises data from three schools, namely Hillcrest High School, Oakwood High School, and St Mary’s Church of England School.

The average test score of St Mary’s Church of England School is 47.191. This is by 7.863 scores greater than scores at Oakwood High School and by 1.655 scores lesser compared to Hillcrest High School.

This looks pretty weird. Why should I use inline expressions at all?

Using inline expressions …

In short: Use inline expressions in R Markdown to ensure your results are accurate, consistent, up-to-date, and fully reproducible.