Coding basics

Jeff Stevens

2025-01-27

Review: Mental model of RStudio

Coding elements

Coding actions

  • To evaluate means to send code to the R interpreter to process
    • 2 + 2 is evaluated by submitting the numbers 2 and 2 to the addition function
  • To return means to output the results
    • After 2 + 2 is evaluated, the number 4 is returned
  • To print means to return the output to the console
    • Output can be returned without being printed

Comments

  • Everything on a line after a hashtag # is a comment

  • Comments are not evaluated

  • Comments can be used as notes for the reader and to prevent code from being evaluated

# we're about to add 2 and 2
2 + 2 # looky there, we added 2 and 2
[1] 4
# 2 + 2  the hashtag was before the code here, so the code was not evaluated

Functions

Perform computations and return output

[1] "2025-01-29"
[1] "/home/jstevens/github/dpavir2025/slides"

Arguments

Information needed by functions

They include argument names and values

read.csv(file = "mypath/myfile.csv")
dplyr::filter(.data = mydata, mycolumn == "Sophomore")
plot(x = x, y = y)
write.csv(x = mydata, file = "mypath/myfile.csv")

Note

If you give argument values in correct order, you don’t need to include argument names. But it’s usually a good idea anyway.

read.csv("mypath/myfile.csv")
dplyr::filter(mydata, mycolumn == "Sophomore")
plot(x, y)
write.csv(mydata, "mypath/myfile.csv")

Objects

Variables created to store information

mydata <- read.csv(file = "mypath/myfile.csv")
trimmed_data <- dplyr::filter(.data = mydata, mycolumn == "Sophomore")
myplot <- plot(x = x, y = y)

Assignment

Assign a value or set of values to an object

# the best way
x <- 9
# avoid this
y = 10
# definitely don't do this
11 -> z

Note

You can assign multiple objects at the same time:

# chain assignments of the same value to different objects
a <- b <- c <- 0

Assignment

Viewing object contents while assigning

x <- 9  # assign value 9 to object x
x  # print contents of object x to console
[1] 9
(x <- 9)  # add parentheses to print to console when assigning
[1] 9

When things go wrong

Errors

When a function hits a problem that stops it from completing

Error in mean.default() : argument "x" is missing, with no default

When things go wrong

Warnings

When a function completes but something strang happens, so it alerts you

mean(sqrt(-1))
Warning in sqrt(-1): NaNs produced
[1] NaN

When things go wrong

Messages

When a function just wants to let you know something

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Mental model of coding

Writing good code

Who are we coding for?

  1. Computer

  2. Ourselves

  3. Others

How to write good code

The Fundamental Theorem of Readability

Code should be written to minimize the time it would take for someone else to understand it.

Principles of writing good code

  • Write less code
  • Avoid repetition
  • Use clear syntax
  • Use good names
  • Use comments

Coding style: Spacing

mean1<-mean (x[1,4:10],na.rm=TRUE)+0.5
mean1 <- mean(x[1, 4:10], na.rm = TRUE) + 0.5

Coding style: Clarity

mean(x, T)
mean(x, na.rm = TRUE)

Coding style: Indenting

for(i in 1:10) {
for(j in 1:5) {
print(x[i, j])
}
}
for(i in 1:10) {
  for(j in 1:5) {
    print(x[i, j])
  }
}

Coding style: Line breaks

if (x > 5) {print("Too big!")}
if (x > 5) {
  print("Too big!")
}

Coding style

The tidyverse style guide

  • Use <- as assignment operator

  • Use space between operators (*, =, ==) and after commas

  • Do not use space between function and parentheses

  • Use indents to separate nested components (Ctrl/Cmd+I)

  • Write out argument names

  • Write out TRUE and FALSE

  • Use ", not ', for quoting text unless it already contains double quotes

Naming things

Naming things

Core principles for naming objects, data columns, files, folders

  • Be nice to machines

  • Be nice to humans

  • Make sorting and searching easy

Be nice to machines

  • avoid spaces, special characters, and accented characters
    • my_file.R not My filé$.R
  • avoid case sensitivity
    • foo.R and Foo.R
  • use consistent, searchable text chunks
    • expt1_cond2_subj114.csv
  • can’t start with a number

Be nice to humans

  • be descriptive (not x) but not too descriptive (this_is_my_object)
  • separate words (preferably using snake_case)
  • avoid capital letters (case matters: a ≠ A)
  • use human readable names that contain content (slugs)
    • prelim_analysis_expt1.R

Make sorting and searching easy

  • use ISO 8601 standard for date YYYY-MM-DD
  • no, really—always use ISO 8601 standard for date!!
  • use ISO 8601 dates before or after slugs
    • 2021-04-06_prelim_analysis_expt1.Rmd
    • prelim_analysis_expt1_2021-04-06.Rmd
  • use padded numbers as prefixes
    • 01_preface.Rmd
    • 02_introduction.Rmd

Mental model of coding

Let’s code!

Coding basics