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.
RStudio is an Integrated Development Environment (IDE) for R. This means it’s a program that makes working with R much easier by providing:
When you open RStudio, you’ll see four main areas (panes). Let’s explore each one:
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:
File > New File > R Scriptprint("Hello Volleyball World!")File > Save As - name it
my_first_script.RWhy save scripts?
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:
2 + 2 and press Enter[1] 4The [1] just means “this is the first result.” Don’t
worry about it for now.
When to use the Console:
When to use the Script Editor:
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:
kills <- 15Ctrl+Enter
(or Cmd+Enter on Mac)kills with
the value 15This 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
There are three main ways to run your R code:
Ctrl+Enter (Windows) or Cmd+Enter
(Mac)Why this is best: Your code is saved, and you can run it again anytime.
Use this for: Quick tests, simple calculations, exploring
Source button at the top of the Script
paneUse 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 onceLet’s make RStudio comfortable for you.
Go to Tools > Global Options
Click Appearance in the left sidebar
Try different options:
My recommendations:
You can resize panes by dragging the dividers between them. You can also:
Tools > Global Options > Pane LayoutYour working directory is the folder where R looks for files and saves outputs by default.
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.
Option 1: Using the menu
Session > Set Working Directory > Choose DirectorySelect FolderOption 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:
/ in R (even on
Windows!)\, but R needs
forward slashes /"C:\\Users\\..."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.
Base R is powerful, but packages extend R’s capabilities. Think of packages like apps on your phone - they add specific features.
For volleyball analysis, you’ll need:
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 GitHubno
and press EnterAfter 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)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").
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:
Tip: Always scroll to the bottom and try running the examples!
# 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.
# 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.
# 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")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:
TabLet’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 numbersChallenge: Modify this script for a different player with different stats!
In Tutorial 01: R Fundamentals, you’ll learn:
These are the building blocks you’ll use for all your volleyball analysis!
# 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 commentsBefore moving to Tutorial 01, make sure you can:
When you’re comfortable with these tasks, you’re ready for Tutorial 01!
If something isn’t working:
library() commandsgetwd() to checkSession > Restart RRemember: Every expert was once a beginner who got error messages. Debugging is a normal part of coding!
Comments: Leaving Notes for Yourself
The
#symbol creates a comment. R ignores everything after#on that line.Why use comments?
Good commenting practices: