Coding the Codebook

S17 · Chapter 9

Today’s Agenda

  • Install the AURA Lab R Companion and check your setup
  • Build a project folder that holds the whole study
  • Install your codebook in that folder as a real document
  • Load the shared example data and look at it for the first time
  • Learn what to do when a line of R returns an error

Meet the R Companion

  • A free VS Code extension built for this course
    • Checks your setup and installs what is missing
    • Writes commented R code into your own script from menus
    • Explains R error messages in plain language
  • Everything it writes is ordinary R you can read and explain
  • It is not an AI tool, so using it needs no AI-use log entry

Installing the Companion

  • Open the Extensions view: Ctrl+Shift+X, or Cmd+Shift+X on a Mac
  • Search for AURA Lab R Companion, then select Install
  • Select View, then Command Palette, and run AURA: Check my setup
    • Pick MC 451 when it asks for your course
    • If a line says MISSING, run AURA: Install what is missing
  • Every Companion command starts with AURA: in the Command Palette
  • The setup walkthrough video is linked in today’s Blackboard announcement

What We Build Today

  • One project folder that holds the whole study
    • Your Chapter 8 codebook, installed as a document
    • The shared Twitch example data, loaded and looked at
  • No analysis today: we open the project, load the data, and look
  • The steps you practice today on the example data are the steps you repeat on your own source
  • Your White Paper’s method section will describe these same steps

Why the Folder Comes First

  • You designed a study and have not yet seen the data in R
  • Reproducible work lives in files in known places, not in commands typed once
  • A project is one folder that holds the data, the code, the codebook, and the report
    • Nothing in the study depends on a file only you have

Deciding where each file goes is a decision you do not need to make. The scaffold makes it for you.

Scaffolding the Project

v2v::new_portfolio("twitch-portfolio")

This one line creates the whole project folder, already named and already wired together.

The v2v:: prefix says the function comes from this book’s package, so you always know where a function came from.

The example project is named twitch-portfolio. Name your own project after your own study.

Today’s steps, with what each should print, are in the starter file: aura-lab.siue.edu/v2v-hub/starters/09-coding-the-codebook.qmd

What the Scaffold Gives You

twitch-portfolio/
├── _quarto.yml
├── index.qmd
├── 01-introduction.qmd ... 05-reflection.qmd
├── references.qmd, references.bib
├── codebook/codebook.qmd
├── R/
├── journals/week-01.qmd
├── data/raw/, data/derived/
└── figures/

The scaffold decides where every file goes and what each file is named, so you do not have to. The five numbered files are the sections of your White Paper, already in order. The R/ folder holds the scripts you write.

Confirming the Stack

v2v::setup()

AURA: Check my setup in the R Companion checks R, Quarto, Git, and every package this course uses, and AURA: Install what is missing installs anything it flags. v2v::setup() runs the course package’s own version of the same check from the console.

  • You ran it once, when you installed the stack
  • Run it every time you open the project
  • It reports a missing piece before that piece interrupts your work
The R Companion's Output panel after Check my setup. Two lines read: RESULT: everything this course needs is installed. SETUP OK: R and the course packages are working.

The Codebook Becomes a Document

  • The scaffold includes a file named codebook/codebook.qmd
    • In Chapter 8 your codebook was a plan on paper
    • From today it is the reference document every later step follows
  • Move your draft codebook into that file, then add a version line and a date
  • A codebook that renders cleanly is a codebook a coder can read

Exercise: Guess the Columns Before You Look

We are about to run glimpse(chat), which prints every column in the example chat table. Answer these three questions before we run it.

  • Name three columns you expect to find in a table of chat messages
  • The book’s example codebook needs the message text and the sender. Which columns does your own codebook need from your own source?
  • If a column your study depends on turns out to be missing from your data, what is your plan: find another source, or change the variable?

Loading the Data

library(tidyverse)

chat <- v2v::twitch_chat()
streams <- v2v::twitch_streams()

Line one loads the tidyverse. The next two lines fetch each example table and name it with <-.

The example data arrives through a function call, not a file path, which removes a whole category of beginner error. You get the working sample, not the full 2018 collection. Loading your own source from a file uses a different first line, read_csv(); every step after it is the same.

What a Function Is

Function: a named piece of code that does one job each time you call it, by writing its name followed by parentheses. Example: v2v::twitch_chat() hands back the chat table.

  • Whatever goes inside the parentheses tells the function what to work on, such as the file read_csv() should read

The Companion Writes This Block

VS Code editor showing a script named wrangle.R. The R Companion inserted a commented block: library(tidyverse), library(v2v), chat <- twitch_chat(), streams <- twitch_streams(), glimpse(chat), and glimpse(streams).
  • Write R code in the R Companion, then Load the course Twitch corpus
  • The block arrives with its comments, ready to run

Looking at the Chat Table

glimpse(chat)
Rows: 35,267
Columns: 5
$ id      <int> 89, 551, 1033, 2094, 2803, …
$ channel <chr> "sodapoppin", "xqcow", "forsen", …
$ sender  <chr> "madzee", "prometheanow", "spectre155", …
$ message <chr> "???????????????????????", "TriEasy Clap TriEasy…
$ date    <dbl> 1.542578e+12, 1.542578e+12, 1.542578e+12, …

glimpse() prints every column, its type, and its first few values.

Reading That Output

  • The table has 35,267 rows and five columns, one message per row
  • The first two messages are not errors
    • One is twenty-three question marks, and one is copypasta (a block of text pasted over and over)
    • Messages like these are normal in Twitch chat, and every source has its own version of them
  • The date column is a number (<dbl>) and is unreadable as it stands
    • It counts milliseconds since 1970; Chapter 11 converts it into a readable date and time

One Row in Your Own Data

  • In the example, one row is one chat message
  • Decide what one row is in your own study before you load anything
    • A study of influencer posts on YouTube and Instagram: one row per comment
    • A study of debut films by YouTube creators: one row per review, with its film attached
    • A study of comments on targeted Instagram ads: one row per comment, with its ad
    • A study of conflict in a reality TV franchise: one row per scene

One Row in Your Own Data (cont.)

  • Every source has its own odd rows
    • Emoji-only comments, replies to replies, deleted reviews, and scenes with no speaker
    • Your codebook says what happens to each one

Looking at the Stream Table

glimpse(streams)
Rows: 32,276
Columns: 6
$ id      <int> 4, 10, 18, 61, 105, …
$ channel <chr> "sodapoppin", "xqcow", "forsen", "giantwaffle", …
$ title   <chr> "Hello truckers\n44835158804929_2115841973", …
$ game    <chr> "Marble It Up!", "Just Chatting", "Artifact", …
$ viewers <int> 27934, 19381, 15300, 4697, 28076, …
$ date    <dbl> 1.542578e+12, 1.542578e+12, 1.542578e+12, …

Each row is one snapshot of one stream at one moment.

Counting What Channels Stream

streams %>%
  count(game, sort = TRUE)
# A tibble: 61 × 2
  game                                   n
  <chr>                              <int>
1 Art                                 5298
2 Fortnite                            3682
3 Just Chatting                       2474
4 Hearthstone                         2157
5 Pokemon: Let's Go, Pikachu!/Eevee!  1967
6 League of Legends                   1952
# ℹ 55 more rows

count() tallies how often each value appears, and sort = TRUE puts the most common value first.

Confirming the Corpus

n_distinct(chat$channel)
[1] 50
  • The example corpus has fifty channels, as the book has said since Chapter 1
  • n_distinct(streams$channel) returns the same fifty
  • The fifty channels streamed 61 different game categories, so most channels move between games

Counting is how you check that the data is what you were told it is. Run the same checks on your own source.

When a Line Breaks

  • R returns an error message instead of a result
    • The usual causes are a misspelling, a package that is not loaded, or a stray bracket
    • This happens to everyone, and it says nothing about your ability
  • An error message is information, so read it before you retype anything
  • Copy the error text and run AURA: Explain an R error from the Command Palette for a plain-English reading

What Explain an R Error Shows

An R Companion panel titled object not found. It quotes the message object chat_dta not found, then explains why this happens (R has no value stored under that name: the line that creates it never ran, the name is misspelled, or text was typed without quotation marks) and what to do (run the line that creates it first, check the spelling and capital letters against the name you assigned, or put text in quotation marks).
  • The panel quotes your error, says why it happens, and lists what to do
  • In this example, chat_dta is a misspelling of a name R never stored

Checkpoint

You should now have:

  • A project folder created by new_portfolio()
  • A clean v2v::setup() result
  • Your codebook in codebook/codebook.qmd, versioned and rendered
  • The chat and streams example tables loaded, and a glimpse() of each

Nothing changed in the data. Looking is all we did.

Before Thursday

  • Finish moving your codebook into codebook/codebook.qmd, then render it
  • Due Friday, October 30, 11:59 PM: Codebook and Qualitative Memo
    • The full codebook plus a 200 to 300 word memo
  • Bring your glimpse() output on Thursday
    • On Thursday we read the data against your codebook

Questions, Comments, or Concerns?