12 Overview of R and RStudio
12.1 What Is R?
- R (R Core Team, 2024) is a powerful language and environment for statistical computing and graphics.
- R is an open-source programming language, widely used among statisticians, data analysts, and researchers for data manipulation, calculation, and graphical display — including agricultural researchers analyzing field-trial and sensor data.
- R is not just a programming language, but also an environment for interactive statistical analysis.
- It was developed by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently maintained by the R Development Core Team.
- It is a GNU project, freely available under the GNU General Public License.
- Packages: the R community actively contributes packages — more than 20,000 are available on the Comprehensive R Archive Network (CRAN) today, covering everything from agronomy-specific analysis to deep learning.
- Platform independent: R runs on Windows, macOS, and Unix-like systems.
12.2 Installation and Setup
Install R — download and install R from CRAN, choosing the installer for your operating system (Windows, macOS, or Linux).
Install RStudio — RStudio is the integrated development environment (IDE) most R users work in day to day: an editor, console, environment browser, and plotting pane in one window. Download it from Posit — Posit is the company (formerly RStudio, Inc.) that develops and maintains RStudio. Posit also publishes Positron, a newer, lighter-weight IDE that supports both R and Python in the same window; RStudio remains the more widely used choice for R-only work and is what this book assumes.
Modern package management — as a project grows, renv (per-project package versioning, so a script keeps working years later) and pak (a faster, more reliable package installer than base install.packages()) are worth knowing about early, even if you don’t need them for the exercises in this book.
12.3 Overview of RStudio Panels
RStudio’s default layout has four panels, each with a specific job:
- Source (top-left) — where you write and edit R scripts, R Markdown files, and other documents, with syntax highlighting and code completion.
- Console (bottom-left) — where R commands actually execute and results appear; typing directly here runs code immediately, without saving it to a script.
- Environment / History (top-right) — the Environment tab lists every object (dataset, variable, model) currently loaded in memory; the History tab logs every command you’ve run in the session.
-
Files / Plots / Packages / Help / Viewer (bottom-right) — browse project files, view plots as they’re created, manage installed packages, read documentation (
?function_name), and preview HTML output.
Working knowledge of these four panels is enough to start; the rest becomes familiar with use.
12.4 R Syntax
A few conventions apply throughout R code:
-
Case sensitivity —
Yieldandyieldare different objects. -
Statement separation — a newline or a semicolon (
;) ends a statement. -
Comments — anything after
#on a line is ignored by R, and is the standard way to document intent. -
Assignment —
<-(preferred by convention) or=binds a value to a name.
12.5 Writing and Running an R Script
An R script is a plain-text file with a .R extension that stores code for reuse, sharing, and version control. In RStudio: File → New File → R Script, write your code, then run a line or selection with Ctrl+Enter (Windows/Linux) or Cmd+Return (macOS). Rscript runs a saved script from outside RStudio entirely — useful for scheduled or automated jobs, such as a nightly script that pulls the day’s sensor readings and updates a dashboard.
12.5.1 Example 1: Hello World
A minimal script that prints a message to the console.
12.5.2 Example 2: Basic Arithmetic
A script that calculates the total area of two fields and prints the result.
Summary
| Concept | Description |
|---|---|
| R and RStudio Fundamentals | |
| What R Is | An open-source language and environment for statistical computing, with 20,000+ CRAN packages. |
| Installation and Setup | R from CRAN, RStudio (or Positron) as the IDE, and renv/pak for project-level package management. |
| RStudio Panels | Source (write code), Console (run code), Environment/History (inspect state), and Files/Plots/Packages/Help/Viewer. |
| R Syntax | Case-sensitive, newline or semicolon ends a statement, `#` starts a comment, `<-` for assignment. |
| Writing and Running a Script | Scripts are saved as `.R` files, run line-by-line in RStudio or in batch with `Rscript`. |