Code examples

This section contains worked examples in R together with supplementary information that will be useful as you work on your problem sets and project. You will see both chunks of code and the output the code produces as well.

Static examples

letters
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"

The code comes first, the output comes second.

length(letters)
[1] 26
letters |> 
  length() |> 
  sqrt()
[1] 5.09902

Try your code live in the browser

In addition to static examples like the ones above, through the magic of WASM and web-r we can run code live in the browser. Press the Play button to run the code live.

You can also edit and re-run this code. Try plotting bill_length_mm against flipper_length_mm, for example.

You can copy code in both the static and the dynamic code cells by clicking the clipboard icon, . (It will appear in the static chunks when you mouse over them.)

If you scroll to the top of the page you will see “</> Code” written in gray in the right corner. If you click that, you will get a little menu that allows you to hide or show all the code chunks on an example page, and also copy the code for the entire page. For example pages with live code cells, The right-side menu will have a “View R History” button where you can see and copy the code you’ve run.