Skip to content

01. Introduction to RStudio⚓︎

Module items⚓︎

R Script file⚓︎

Copy the code below ➜ Paste into [[RStudio console]] ➜ Hit Enter

source(url("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/regression/docs/assets/r-scripts/0_packages_data.R")); 
download.file("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/regression/docs/assets/r-scripts/01-intro-rstudio.R", "01-intro-rstudio.R"); 
file.edit("01-intro-rstudio.R")

Lab assignment⚓︎

Generating a table

Sample lab assignment⚓︎

Sample: Generating a table

Learning outcomes⚓︎

  1. Differentiate R and RStudio
  2. Use RStudio Cloud (Posit)
  3. Explain what R packages are
  4. Understand the General Social Survey (GSS) structure
  5. Learn the steps of using RStudio

Suggested reading⚓︎

  • 📖
    Verzani, John. 2012. “Overview, Installation.” Pp. 1–11 in Getting started with RStudio. Sebastopol: O’Reilly.

Why do we use software for statistical analysis?⚓︎

  • Software helps us to collect, organize, analyze, interpret and design data.

    • Many statistical methods are too complex to be calculated by hand or with calculators.
    • Software can process repetitive computational tasks rapidly.
    • We can save our exact analytical steps as source code, which is a fundamental requirement for making scientific research transparent and reproducible.
  • For example, imagine we collected data from 1,000 students, and asked their happiness level, stress level, and class status.

    • happiness (What's your happiness level?) [1=unhappy, 2=neutral, 3=happy]
    • stress (What's your stress level?) [1=stressed, 2=neutral, 3=not stressed]
    • class (What's your class status?) [1=senior, 2=non-senior]
  • Then, the data looks like this:

    id happiness stress class
    1 3 2 1
    2 2 1 2
    3 2 3 2
    4 1 2 1
    ... ... ... ...
    1,000 2 1 2
  • When dealing with a dataset of 1,000 students, manual calculation, i.e. how many students are not stressed, becomes practically impossible.

Software⚓︎

What is R?⚓︎

  • R is a free, open-source programming language and software environment specifically designed for statistical computing, data analysis, and graphics.

    r logo

What is RStudio?⚓︎

  • RStudio is an Integrated Development Environment (IDE), which is a software application designed to make writing, editing, and executing R code much easier and more organized.

    • RStudio helps keep R more organized, and provides an interface by adding many convenient features and tools.
      • Free and open source: We can run RStudio from home and use it for the rest of our life.
      • Advanced models: Currently, the CRAN package repository features more than 23,000 available packages.
      • Growing popularity: Learning RStudio should stand us in good stead for future social research.

    rstudio logo

  • R is like a car's engine; works without a wheel, but impossible to steer.

    • RStudio is like a wheel; gives you control and direction. The engine still does all the work. R always works at the background.

    The image suggests that R is the engine that powers the work, while RStudio is the steering wheel/dashboard interface that helps you control and use R more easily.

What is RStudio Cloud?⚓︎

  • RStudio Cloud (currently known as Posit Cloud) is a browser-based version of the RStudio environment where you can write code, run analyses, install packages, and manage projects without installing R or RStudio on your own computer.

    • Although the instructions are for RStudio Cloud, all steps are same in other options.
    • Creating a “free account” will allow you 25 hours per month of connect time.

    posit logo

Options to use RStudio⚓︎

  • There are three options to use RStudio
    • (1) Create a free RStudio Cloud account
    • (2) Install R and RStudio to your personal computer
    • (3) Using RStudio on a university lab computer

Data: General Social Survey (GSS)⚓︎

  • We'll use General Social Survey ([[GSS]]) for the modules.
    • The GSS gathers data on contemporary American society in order to monitor and explain trends and constants in attitudes, behaviors, and attributes.
    • Hundreds of trends have been tracked since 1972.
    • In addition, since the GSS adopted questions from earlier surveys, trends can be followed for up to 80 years.

RStudio interface⚓︎

Main look⚓︎

RStudio interface with four panes

  • (1) Script file: This pane is where you write, edit, and save your R code or scripts.
  • (2) Environment: This pane shows the objects currently loaded in memory, such as datasets, variables, and functions.
  • (3) Console: This pane shows the codes that were run, immediate output, or error messages.
    • You will paste the script file codes here and hit "Enter" when you open RStudio.
  • (4) Files/plots/packages/help: This pane lets you browse files, view tables and figures, manage packages, and access help documentation.

More detailed look⚓︎

RStudio interface with more detailed view

  • (1) Menu bar: The top strip with File, Edit, Code, View, Plots, and other menus. This is where you access all global settings and commands.
  • (2) Script editor: The upper-left panel where you write and save your R code. Code here does not run until you tell it to.
  • (3) Save button: Saves your current script to disk.
  • (4) Run button: Executes the currently selected line(s). Keyboard shortcut: Cmd/Ctrl + Enter.
  • (5) Source button: Runs the entire script from top to bottom, as if you submitted it all at once. Useful for running a complete analysis.
  • (6) Environment tab: The upper-right panel showing every object currently loaded in memory.
  • (7) History tab: This contains the history of all the commands we have recently run. We can go to this tab and re-run any commands we have run previously.
  • (8) Data: This is where all your currently open data will appear. GSS data is there.
  • (9) Files tab: This shows the files and folders within the project folder.
  • (10) Plots tab: This displays any graphs you produce.
  • (11) Packages tab: This will show all the installed and loaded packages.
  • (12) Viewer tab: This displays any tables you produce.
  • (13) Table view: The frequency distribution of marital status variable is here. You highlight the code in the script file, click "Run", and the table appears here.
  • (14) Console tab: This shows the codes that were run, immediate output, or error messages.
    • You will paste the script file codes here and hit "Enter" when you open RStudio.

Packages⚓︎

  • [[R packages]] are written by community contributors, and they are available for anyone to use.

    • They plug into RStudio to expand the software's built-in capabilities.
    • There is a massive ecosystem of thousands of free, open-source packages created by the community.
    • Most are hosted on CRAN (the Comprehensive R Archive Network), while development versions are often shared on platforms like GitHub.
    • Packages provide pre-written tools for highly specific tasks, such as data visualization and complex statistical modeling.
      • R is like a base car. It's functional, ready to drive. It has engine, steering, brakes.
      • R packages are like add-ons; GPS navigation, sunroof, heated seats, backup camera.

    A side-by-side analogy compares base R to a basic car and R packages to optional add-ons. The left car is labeled “R — base model” with the subtitle “Functional, ready to drive,” while the right car is labeled “R packages — add-ons” with the subtitle “Install what you need,” showing extra features added to the car: GPS navigation, sunroof, heated seats, and backup camera.

    • We will use several packages. Tidyverse is one of the packages we’ll use:

      A figure illustrating R packages for data science using hexagon logos associated with the tidyverse, including ggplot2, dplyr, readr, stringr, purrr, forcats, and tibble. To the right, explanatory text describes the tidyverse as a collection of R packages with a shared design philosophy and shows the installation command install.packages("tidyverse").

Steps of using RStudio⚓︎

[[Copy the code|ref]]: Copy the specific R script file code⚓︎

RStudio is open with the Console on the left. The pasted code is highlighted there, with callouts indicating: 1) open the RStudio lab, 2) paste the code into the Console, and 3) press Enter.

  1. You will see a box of code at the top of each module, which comes with a different script file.
  2. Hover over the code and click the "Copy" icon.

Open RStudio Cloud website: Use [[RStudio console|ref]]⚓︎

RStudio is open with the Console on the left. The pasted code is highlighted there, with callouts indicating: 1) open the RStudio lab, 2) paste the code into the Console, and 3) press Enter.

  1. Open RStudio Cloud website and go to "RStudio labs".
  2. Paste the code into RStudio console.
  3. Hit Enter.
  • The code you paste will:
    • Install and load all required packages
    • Download the "gss" data,
    • Silently relabel the variables, and
    • Open the script file.

Wait⚓︎

RStudio shows the downloaded script file open in the upper-left pane. The Environment pane confirms that the dataset gss is loaded, and the Console shows the file was downloaded successfully.

  1. You will see a STOP sign.
  2. And, codes are running in the console. You should wait until the 🛑 STOP 🛑 sign in the console disappears and no more code is running in the console.
  3. When you see the script file opens and, "gss" appears under the "Environment - Data section," everything is all set.

Initial setup

The initial setup takes approximately 5–10 minutes; thereafter, it will take only 5 seconds.

[[Highlighting and running|ref]]⚓︎

In the script pane, a line of code is selected. The numbered callouts show the sequence: 1) select the code, 2) click Run, and 3) view the output table in the lower-right pane.

  1. We highlight the codes
  2. And, click “Run”
  3. Clicking “Run” generates the analysis (a frequency table for this example)

Highlighting and running

  • As R script files are simply text files, we need to highlight the codes and run. Without highlighting and running, the codes will not work.