7) Iterations

๐Ÿ“– Lecture

In addition to writing functions to reduce duplication in your code, you also need to learn about iteration, which helps you when you need to do the same operation several times. Namely, we review control flow structures such as for loops, while loops, repeat loops, and the apply family functions.

๐Ÿ“š Reading

Read chapters 13 and 14 of โ€œR Coding Basicsโ€:

๐Ÿ”ฌ Lab

Youโ€™ll keep working writing a variety of loops in R (e.g. for, while, repeat).

๐ŸŽฏ Objectives

At the end of this week you will be able to:

  • Write for loops to repeat the same operation a given number of times
  • Write while loops to repeat the same operation an unknown number of times

๐Ÿ”† Shiny Friday

The shiny app for this week is the same one from last week: it uses mtcars data set to visualize the top-n cars given a selected variable.

An important component of this app is the use of the reactive() function to create so-called reactive conductors (i.e. an R expression that uses widget input(s) and returns a value).

  • mtcars-topn-barchart: produces a simple barchartโ€”via "ggplot2"โ€”to visualize the top-n cars given a selected variable. In addition to the plot, it also displays a table with the top-n cars. More important, this app uses a so-called reactive conductor element.
    https://github.com/data133/shiny/tree/main/mtcars-topn-barchart

๐Ÿ”” Assignments

  • HW5 due this 03/01
  • HW6 released on 03/02, due 03/08