Welcome to R and RStudio!

You’ve already downloaded R and RStudio - great job! Now let’s get oriented with your new workspace. Think of R as the engine of a car, and RStudio as the dashboard and steering wheel that makes it easier to control.

What is RStudio?

RStudio is an Integrated Development Environment (IDE) for R. This means it’s a program that makes working with R much easier by providing:

  • A place to write and save your code
  • A way to see your data and results
  • Tools to help you organize your work
  • A console to interact with R directly

The Four Panes of RStudio

When you open RStudio, you’ll see four main areas (panes). Let’s explore each one:

1. Source/Script Editor (Top Left)

This is where you’ll write and save your R scripts. Think of a script as your playbook - it’s where you write down all the plays (code) you want to run.

Try this:

  1. Click File > New File > R Script
  2. A blank file should appear in the top-left pane
  3. Type: print("Hello Volleyball World!")
  4. Save this file: File > Save As - name it my_first_script.R

Why save scripts?

  • You can run the same analysis on different matches
  • You can share your work with others
  • You can come back later and remember what you did
  • You can fix mistakes without starting over

2. Console (Bottom Left)

This is where R actually runs your code and shows you results. You can type code directly here, but it won’t be saved.

Try this:

  1. Click in the Console pane
  2. Type: 2 + 2 and press Enter
  3. R should show you: [1] 4

The [1] just means “this is the first result.” Don’t worry about it for now.

When to use the Console:

  • Quick calculations
  • Testing small pieces of code
  • Checking if something works before adding it to your script

When to use the Script Editor:

  • Anything you want to save and reuse
  • Complete analyses
  • Code you might need to debug or modify later

3. Environment/History (Top Right)

This pane has several tabs:

Environment tab: Shows all the data and variables you’ve created. Think of this like your roster - it shows what “players” (data objects) are currently available.

History tab: Shows every command you’ve run. Like a game log.

Try this:

  1. In your script, type: kills <- 15
  2. Run this line by clicking it and pressing Ctrl+Enter (or Cmd+Enter on Mac)
  3. Look at the Environment tab - you should see kills with the value 15

4. Files/Plots/Packages/Help (Bottom Right)

This pane has multiple useful tabs:

Files: Navigate your computer’s folders (like Windows Explorer or Mac Finder)

Plots: Where your charts and graphs will appear

Packages: Shows installed add-ons (we’ll cover this soon)

Help: Documentation for R functions

Viewer: For viewing web content and interactive graphics

Running Code: Three Ways

There are three main ways to run your R code:

Method 2: Type in Console

  1. Type code directly in the Console
  2. Press Enter
  3. Results appear immediately below

Use this for: Quick tests, simple calculations, exploring

Method 3: Source the Entire Script

  1. Click the Source button at the top of the Script pane
  2. This runs ALL the code in your script at once

Use this for: Running complete analyses after you’ve tested individual pieces

Let’s practice all three methods:

# Create a new script and type these lines:

# Line 1: Store a player's kills
player_kills <- 12

# Line 2: Store a player's errors  
player_errors <- 3

# Line 3: Store attempts
player_attempts <- 28

# Line 4: Calculate hitting efficiency
hitting_eff <- (player_kills - player_errors) / player_attempts

# Line 5: Show the result
print(hitting_eff)

# Now practice:
# 1. Run each line individually with Ctrl+Enter
# 2. Check the Environment pane after each line
# 3. Try typing "hitting_eff" in the Console and press Enter
# 4. Click Source to run everything at once

Comments: Leaving Notes for Yourself

The # symbol creates a comment. R ignores everything after # on that line.

Why use comments?

  • Explain what your code does
  • Leave reminders for yourself
  • Help others understand your work
  • Temporarily disable code without deleting it
# This is a comment - R ignores this line

kills <- 15  # You can also put comments after code

# Calculate hitting efficiency
# Formula: (Kills - Errors) / Attempts
hitting_eff <- (15 - 3) / 30  # Player had 30 attempts

# To temporarily disable code, add # at the start:
# print("This won't run")
print("This will run")

Good commenting practices:

  • Explain WHY you’re doing something, not just WHAT
  • Use comments to break your code into sections
  • Comment anything that might be confusing later

Customizing Your Workspace

Let’s make RStudio comfortable for you.

Changing Appearance

  1. Go to Tools > Global Options

  2. Click Appearance in the left sidebar

  3. Try different options:

    • Editor theme: Dark themes (like “Tomorrow Night”) are easier on eyes for long sessions
    • Zoom: Make text bigger or smaller
    • Editor font: Choose what’s comfortable to read
    • Font size: Increase if you’re straining to read

My recommendations:

  • Font size: 12-14 (comfortable to read without squinting)
  • Theme: Personal preference! Try a few
  • Default: RStudio’s defaults are actually quite good

Pane Layout

You can resize panes by dragging the dividers between them. You can also:

  1. Tools > Global Options > Pane Layout
  2. Rearrange which panes appear where
  3. Most people keep the default, but customize if you prefer

Setting Your Working Directory

Your working directory is the folder where R looks for files and saves outputs by default.

Why This Matters

When you tell R to load a file called "match_data.dvw", R looks in your working directory. If the file isn’t there, you’ll get an error.

Checking Your Current Working Directory

# This shows your current working directory
getwd()

Setting Your Working Directory (Basic Method)

Option 1: Using the menu

  1. Go to Session > Set Working Directory > Choose Directory
  2. Navigate to the folder where you’ll keep your volleyball analysis files
  3. Click Select Folder

Option 2: Using code

# Replace this path with your actual folder path
# Windows example:
setwd("C:/Users/YourName/Documents/Volleyball Analysis")

# Mac example:
setwd("/Users/YourName/Documents/Volleyball Analysis")

Important notes about file paths:

  • Always use forward slashes / in R (even on Windows!)
  • Windows typically uses backslashes \, but R needs forward slashes /
  • Or you can use double backslashes: "C:\\Users\\..."

A Better Way: R Projects (Coming Soon!)

setwd() works, but it has a problem: if you move your files or share your code with someone else, the file path breaks.

We’ll learn a better method using R Projects and the here package in Tutorial 02. For now, just know that setwd() is a temporary solution.

Installing Packages

Base R is powerful, but packages extend R’s capabilities. Think of packages like apps on your phone - they add specific features.

What Packages Will We Use?

For volleyball analysis, you’ll need:

  • datavolley: Load and work with DVW files
  • dplyr: Manipulate data easily
  • ggplot2: Create visualizations
  • gt: Make professional tables
  • glue: Create dynamic text (for titles, file names)
  • here: Better file path management (we’ll learn this with R Projects)
  • teamcolors: Get official team colors for charts

Installing Packages

You only need to install a package once on your computer (like downloading an app).

# Install packages (only run this once!)
install.packages("dplyr")
install.packages("ggplot2")
install.packages("gt")
install.packages("glue")
install.packages("here")
install.packages("teamcolors")

# datavolley requires a special installation from GitHub
install.packages("remotes")  # This helps us install from GitHub
remotes::install_github("openvolley/datavolley")

What’s happening here?

  • install.packages("name") downloads a package from CRAN (R’s app store)
  • remotes::install_github() downloads packages directly from developers on GitHub
  • You might see lots of text appear - that’s normal!
  • If asked “Do you want to install from sources?” type no and press Enter

Loading Packages

After installation, you need to load packages every time you start RStudio (like opening an app).

# Load packages (do this at the start of every script!)
library(dplyr)
library(ggplot2)
library(datavolley)
library(gt)
library(glue)
library(here)
library(teamcolors)

Think of it this way:

  • install.packages() = Downloading an app to your phone (once)
  • library() = Opening the app to use it (every time)

Package Loading Errors

If you see an error like:

Error in library(dplyr) : there is no package called 'dplyr'

This means you forgot to install it! Go back and run install.packages("dplyr").

Getting Help

R has built-in help documentation for every function.

# Three ways to get help:

# Method 1: Help pane
?mean  # Opens help for the mean() function

# Method 2: Alternative syntax
help(mean)

# Method 3: Search for a topic
??average  # Searches all help files for "average"

Reading help files:

  • Description: What the function does
  • Usage: How to use it
  • Arguments: What inputs it needs
  • Examples: Code you can run to see it in action

Tip: Always scroll to the bottom and try running the examples!

Common Beginner Issues

Issue 1: “Object not found”

# This will cause an error:
print(player_name)
# Error: object 'player_name' not found

# Why? We never created player_name!
# Fix it:
player_name <- "Sarah"
print(player_name)

Solution: Make sure you’ve run the code that creates the object first.

Issue 2: “Could not find function”

# This will cause an error:
dv_read("match.dvw")
# Error: could not find function "dv_read"

# Why? We haven't loaded the datavolley package!
# Fix it:
library(datavolley)
dv_read("match.dvw")  # Now it works (if the file exists)

Solution: Load the package with library() first.

Issue 3: Working Directory Problems

# This might cause an error:
data <- read.csv("matches.csv")
# Error: cannot open file 'matches.csv': No such file or directory

# Why? The file isn't in your working directory!
# Fix it by either:
# 1. Moving the file to your working directory, or
# 2. Using the full file path:
data <- read.csv("C:/Users/YourName/Desktop/matches.csv")

Issue 4: Typos and Capitalization

R is case-sensitive: Player and player are different!

# These are different:
Player <- "Sarah"
player <- "Emma"

print(Player)  # Shows "Sarah"
print(player)  # Shows "Emma"

Tips to avoid typos:

  • Use tab completion: Type the first few letters and press Tab
  • Copy and paste variable names instead of retyping them
  • Use consistent naming (we’ll cover this in the next tutorial)

Your First Complete Script

Let’s put it all together. Create a new script and try this:

# My First Volleyball Analysis Script
# Created: [Today's Date]
# Purpose: Practice the basics

# Load packages
library(dplyr)

# Set working directory (use your actual path!)
setwd("C:/Users/YourName/Documents/Volleyball")

# Create some volleyball stats
player_name <- "Sarah Smith"
kills <- 15
errors <- 3
attempts <- 35

# Calculate hitting efficiency
hitting_eff <- (kills - errors) / attempts

# Display results
print(paste(player_name, "had a hitting efficiency of", 
            round(hitting_eff, 3)))

# The round() function rounds to 3 decimal places
# paste() combines text and numbers

Challenge: Modify this script for a different player with different stats!

What’s Next?

In Tutorial 01: R Fundamentals, you’ll learn:

  • How to store data in variables
  • Different types of data (numbers, text, true/false)
  • How to work with lists of data (vectors)
  • How to organize data in tables (data frames)

These are the building blocks you’ll use for all your volleyball analysis!

Quick Reference: Essential Commands

# Working Directory
getwd()                    # Check current directory
setwd("path/to/folder")    # Set directory

# Getting Help
?function_name             # Help for a function
??search_term              # Search help files

# Packages
install.packages("name")   # Install once
library(name)              # Load every session

# Running Code
# Ctrl+Enter: Run current line from script
# Source: Run entire script

# Comments
# Use # for comments

Practice Exercises

Before moving to Tutorial 01, make sure you can:

  1. ✓ Open RStudio and identify all four panes
  2. ✓ Create and save a new R script
  3. ✓ Run code from your script using Ctrl+Enter
  4. ✓ Check your working directory
  5. ✓ Install and load the packages we’ll need
  6. ✓ Add comments to your code
  7. ✓ Customize your RStudio appearance (optional but recommended!)

When you’re comfortable with these tasks, you’re ready for Tutorial 01!


Troubleshooting Tips

If something isn’t working:

  1. Read the error message - R’s errors are usually helpful
  2. Check for typos - Especially capitalization and spelling
  3. Make sure packages are loaded - Run library() commands
  4. Verify your working directory - Run getwd() to check
  5. Restart R - Sometimes a fresh start helps: Session > Restart R
  6. Ask for help - Share the error message and the code you ran

Remember: Every expert was once a beginner who got error messages. Debugging is a normal part of coding!