Skip to content

01. Introduction to RStudio

Module items

R Script file code

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

    • source(url("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/r-scripts/0-packages-data.R")); 
      (function(f="01-intro-rstudio.R"){if(!file.exists(f)){download.file("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/r-scripts/01-intro-rstudio.R",f,mode="wb");file.edit(f)}else{download.file("https://raw.githubusercontent.com/ttezcann/ssric-reg/refs/heads/main/docs/assets/r-scripts/01-intro-rstudio.R",gsub(".R","-original.R",f),mode="wb");file.edit(gsub(".R","-original.R",f))}})()
      

Lab assignment

Generating a table

Sample lab assignment

Sample: Generating a table

Suggested reading


  • Verzani, John. 2012. "Overview, Installation." Pp. 1–11 in Getting started with RStudio. Sebastopol: O'Reilly.

Learning outcomes

  1. Differentiate between the R programming language, RStudio, and RStudio Cloud (Posit)
  2. Define R packages and explain their purpose in extending R's built-in capabilities
  3. Describe the structure and purpose of the General Social Survey (GSS) dataset
  4. Identify the four main panes of the RStudio interface and explain the function of each
  5. Learn the steps required to run an R script file

Why do we use software for statistical analysis?

  • Software helps us 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 R is a free, open-source programming language and software environment specifically designed for statistical computing, data analysis, and graphics.
    • Free and open source: We can run R 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 R should stand us in good stead for future social research.

What is [[RStudio]]?

  • RStudio IDE 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.
  • R R is like a car's engine: it works without a wheel but impossible to steer.
    • RStudio is like a wheel: it 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]]?

  • Posit 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 R or RStudio IDE RStudio on your own computer.
    • Although the instructions are for RStudio Cloud, all steps are the same in other options.
      • Creating a “free account” will allow you 25 hours per month of connect time.

Options to use RStudio

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.

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 R is like a base car. It's functional, ready to drive. It has an engine, steering, and 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 Copy icon callout shows how to copy module code from the code box at the top of each module page.
    1. You will see a code box at the top of each module page.
    2. Hover over the code and click the "Copy" icon.
    3. Each module comes with a different R script file. This process should be done for each module, and should be done only once for each module.
      1. If the codes are accidentally changed, run the specific R script file code again, which will upload the original R script file with the extension _original.

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: (1) Install and load all required packages, (2) download the "gss" data, (3) relabel the variables, (4) add helpers, and (5) 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. Codes are running in the console. You should wait until the ⛔ STOP ⛔ sign disappears and no more code is running.
    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.
    • 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.
      1. We highlight the code.
      2. Click “Run”.
      3. Clicking “Run” generates the analysis (a frequency table, for this example)

[[RStudio interface]]

Main look

  • RStudio interface with four panes
    1. Script file: This pane is where we write, edit, and save our R code or scripts.
    2. Environment: This pane shows the objects currently loaded in memory, such as datasets, variables, and functions.
    3. [[RStudio console|ref]]: This pane shows the codes that were run, immediate output, or error messages.
      1. We will paste the script file codes here and hit "Enter" when we open RStudio.
    4. Files/plots/packages/help: This pane lets us browse files, view tables and figures, manage packages, and access help documentation.

More detailed look

  • RStudio interface with a 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).
    1. Keyboard shortcut: Ctrl+Enter  /  Cmd+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 shows 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|ref]]: This shows the files and folders within the project folder.
    • RStudio Files tab showing several .R script files. It is the first tab. An arrow and note indicate that clicking the files will reopen them.
  10. [[Plots tab|ref]]: This displays any graph you produce.
    • RStudio interface with the "Plots" tab highlighted. It is the second tab in the lower-right panel, next to the "Files" tab.
  11. Packages tab: This will show all the installed and loaded packages.
    • RStudio interface with the "Packages" tab highlighted. It is the third tab in the lower-right panel, next to the "Plots" tab.
  12. [[Viewer tab|ref]]: This displays any table you produce.
    • RStudio interface with the "Viewer" tab highlighted. It is located in the lower-right panel, next to the "Help" tab, and displays rendered tables, HTML output, and other interactive content.
  13. Table view: The frequency distribution of the marital status variable is here. You highlight the code in the script file, click "Run", and the table appears here.
  14. [[RStudio console]] tab: This shows the codes that were run, immediate output, or error messages.
    1. We will paste the R script file codes here and hit "Enter" when we open RStudio.