Mixing writing and r

Author

Denis Schluppeck

Session date:

2023-01-04

Background

Here is an example of a document that produces a plot from some data that’s stored separately.

The data in Figure 1 shows the daily interactions with the moodle page for my second year lab classes. Can you spot the two dominant patterns in the data?

an actual computed figure

Figure 1: A line plot of some mystery data

or a table

A badly formatted table… rstats with various packages handles tabular data much more nicely!

Code
head(df)
# A tibble: 6 × 2
  theTime_day     n
  <date>      <dbl>
1 2016-06-13      2
2 2016-07-21      1
3 2016-09-01      2
4 2016-09-09      2
5 2016-09-13      1
6 2016-09-14      4

or some further analysis

If you want to compute things for including in your text, so-called inline code, then you can make your code spit out markdown that’s been patched up.

E.g. figure out the total # of interactions:

`r sum(df$n)`

Or in text: the total number of interactions were 8761, but mean and median were 108.16, and 36 respecively.

For code cells, if you turn #| echo: true to false, then the code is hidden!

We can also elaborate on previous plots, by adding additional analysis. Figure 2 shows the days, where n>100.

Figure 2: Events with > 100 interactions labelled

Notes

  • dplyr
  • Check out how conveniently the output format can be swapped out with quarto render 01-doc-with-julia.qmd --to pdf etc