Code examples

This section contains worked examples related to what we’ve been covering. Material for specific weeks can be accessed via the menu. In the examples, you will see both chunks of code and the output the code produces. For instance here are some shell commands:

# The absolute path where this project lives on my Mac
pwd
/Users/kjhealy/Documents/courses/mptc
# The directory listing for the top-level folder of this project
ls
R
README.md
README.qmd
README_files
_extensions
_freeze
_quarto.yml
_site
_targets
_targets.R
_variables.yml
about
assets
assignment
content
data
deploy.sh
example
files
html
index.qmd
mptc.Rproj
renv
renv.lock
renv.lock.orig
schedule
site_libs
slides
staging
syllabus
# Listing all and only the qmd files at the project's top-level folder
ls *.qmd
README.qmd
index.qmd

The command comes first, the output comes second.

# Get a count of the number of lines in all the qmd files in the root folder 
wc -l *.qmd
      59 README.qmd
      73 index.qmd
     132 total
# Get a count (separately) of the number of lines in every file in the slides folder 
find ./slides -name '*.qmd' | xargs wc -l 
    1516 ./slides/08-slides.qmd
     810 ./slides/05b-slides.qmd
    1832 ./slides/05-slides.qmd
     248 ./slides/00-slides.qmd
    1475 ./slides/07-slides.qmd
     970 ./slides/02-slides.qmd
     433 ./slides/01a-slides.qmd
     422 ./slides/08a-slides.qmd
     498 ./slides/01b-slides.qmd
    1701 ./slides/04-slides.qmd
     725 ./slides/03-slides.qmd
     838 ./slides/06-slides.qmd
   11468 total

You can copy the code used in these examples in one of two ways. First, you can mouse over any code chunk and click the clipboard icon, , that appears.

Second, you can scroll to the top of your screen. 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.