You RRR a Beginner: R, RStudio and Posit Cloud ๐Ÿ‘ฉโ€๐Ÿ’ป

MATH 4720/MSSC 5720 Introduction to Statistics

Dr. Cheng-Han Yu
Department of Mathematical and Statistical Sciences
Marquette University

Letโ€™s get equipped with our tools!

R and RStudio

  • R: free open-source programming language
  • R is mainly for doing data science with strength in statistical modeling, computing and data visualization

  • RStudio 1: interface for R, Python, etc called an IDE (integrated development environment), e.g. โ€œI write R code in the RStudio IDEโ€.
  • RStudio is not a requirement for programming with R, but itโ€™s commonly used by R developers, statisticians and data scientists.

โ˜๏ธ Posit Cloud - Statistics w/o hardware hassles

  • ๐Ÿ˜Ž We can implement R programs without installing R and RStudio in your laptop!
  • ๐Ÿ˜Ž Posit Cloud lets you do, share and learn data science online for free!

๐Ÿ˜ž R/RStudio: Lots of friction

  • Download and install R
  • Download and install RStudio
  • Install wanted R packages:
    • rmarkdown
    • tidyverse
    • โ€ฆ
  • Load these packages
  • Download and install tools like Git

๐Ÿค“ Posit Cloud: Much less friction

>hello R!

Sign Up Posit Cloud

  • Step 1: In the Posit website https://posit.co/, choose Products > Posit Cloud as shown below.

Sign Up Posit Cloud

  • Step 2: Click GET STARTED.

  • Step 3: Free > Sign Up. Please sign up using your Marquette email address or the one you prefer.

New Projects

In Posit Cloud, click New Project > New RStudio Project, then you are all set!

New RStudio Project

First R Code in Posit Cloud!

  • Give your project a nice name (click Untitled Project), math-4720 for example.

  • First R code: "Hello WoRld!" or 2 + 4 after > in the Console pane.

  • Change the editor theme: Tools > Global Options > Appearance

Working in RStudio

RStudio Panes

R Script

  • A R script is a .R file that contains R code.

  • To create a R script, go to File > New > R Script, or click the green-plus icon on the topleft corner, and select R Script.

Run Code

  • Run : run the current line or selection of code.

  • Icon right to the Run : re-run the previous selected code.

  • Source : run all the code in the R script.

Environment Tab

  • The (global) environment is where we are currently working.
  • Anything created or imported into the current R session is stored in our environment and shown in the Environment tab.
  • After we run the R script, objects stored in the environment are
    • Data set mtcars
    • Object x storing integer values 1 to 10.
    • Object y storing three numeric values 3, 5, 9.

Help

  • Donโ€™t know how a function works or what a data set is about โ“

  • ๐Ÿ‘‰ Simply type ? followed by the data name or function name like

  • A document will show up in the Help tab, teaching you how to use the function or explaining the data set.

What does the function mean() do?


  • What is the size of mtcars data?

  • Type mtcars and hit Enter in the Console to see the data set.

  • Discuss data type of each variable.

  • Type mtcars[, 1] and hit Enter in the Console. What do you see?

05:00

Install R and RStudio Locally to Your Computer (Optional)

Install R โ€“ Step 1

Install R โ€“ Step 2

  • If you are a Mac user, you should see the page as below. You are recommended to download and install the latest version of R (now R-4.5.1 (Great Square Root)), if your OS version allows to do so. Otherwise, choose a previous version, R-3.6.3.

  • If you are a Windows user, after clicking Download R for Windows, please choose base version, then click Download R-4.5.1 for Windows.

Install R โ€“ Step 3

  • Once you install R successfully, when you open R, you should be able to see the following R terminal or console:

Windows

Mac

Welcome to the R World!

  • Now you are ready to use R to do statistical computation.

  • You can use R like a calculator. After typing your formula, simply hit Enter, you get the answer! For example,

1 + 2
[1] 3
30 * 42 / 3
[1] 420
log(5) - exp(3) * sqrt(7)
[1] -51.5319

Install RStudio โ€“ Step 1

  • In the Posit website, on top go to OPEN SOURCE > DOWNLOAD RSTUDIO ->

Install RStudio โ€“ Step 2

  • Click DOWNLOAD RSTUDIO.

Install RStudio โ€“ Step 3

The page will automatically detect your operating system and recommend a version of RStudio that works the best for you that is usually the latest version.

  • Click DOWNLOAD RSTUDIO DESKTOP FOR [Your OS version].

  • Follow the standard installation steps and you should get the software.

  • Make sure that R is installed successfully on your computer before you download and install RStudio.

RStudio Screen

  • When you open RStudio, you should see something similar to the figure below.

  • If you do, congratulations! You are able to do every statistical computation in R using RStudio locally in your computer.