Posit Cloud ☁️

MATH/COSC 3570 Introduction to Data Science

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

Let’s get equipped with data science tools!

Integrated Development Environment


which are software for efficiently writing computer programs.

☁️ Posit Cloud - Data Science w/o hardware hassles

  • 😎 Implement R/Python programs without needing to install R/Python and the IDE in your laptop!

  • 😎 Posit Cloud lets you do, share and learn data science online for free!

😞 Get everything ready locally: Lots of friction

  • Download and install R/Python
  • Download and install IDE
  • Install wanted R/Python packages:
    • tidymodels
    • tidyverse
    • NumPy
  • Download and install tools like Git

🤓 Posit Cloud: Much less friction

> hello world!

Install Posit Cloud

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

Install Posit Cloud

  • Step 2: Click GET STARTED.

  • Step 3: Free or Student > Sign Up. Please sign up with GitHub if you have one or use your Marquette email address.

New Projects

  • We will talk about Git/GitHub shortly.

Workspaces

  • When you create an account on Posit Cloud you get a workspace of your own.
  • You can add a new workspace (click + New Space in sidebar) and control its permissions.

Welcome to 3570 Data Science!

  • I’m sending you a link via email for joining the course workspace 2024-spring-math-3570. Please join.


  • In the bar, click workspace 2024-spring-math-3570.

  • Click New Project > New RStudio Project to get into the IDE.

  • In Untitled Project, name your project as 3570-project.

  • In the Console pane, write R code: a string "Hello WoRld!" or math 2 + 4.

  • Tools > Global Options > Appearance to select your favorite editor theme.

More Tips

Working in Posit Cloud

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.

Python Script

  • A Python script is a .py file that contains Python code.

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

Run Code

  • Run : run the current line or selection of code.
    • ctrl + enter (Win) or cmd + enter (Mac)
  • Icon right to the Run : re-run the previous code.
    • alt + ctrl + p (Win) or option + cmd + p (Mac)
  • Source : run all the code in the R script.
    • shift + ctrl + s (Win) or shift + cmd + s (Mac)
  • Source with Echo : run all the code in the R script with the code printed in the console.
    • shift + ctrl + enter (Win) or shift + cmd + enter (Mac)

Run Python Code

  • Running Python code may need to update some packages. Please say YES!

  • When you run the Python code in the R console, the console will switch from R to Python.

  • Type quit in the Python console to switch back to the R console.

Environment Tab

  • The (global) environment is where we are currently working.

  • Anything created or imported into the current R/Python session is stored in the 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.

Environment Tab

  • After we run the Python script, the object stored in the environment is
    • Object b storing a string Hello World!

History Tab

  • The History tab keeps a record of all previous commands.
    • save icon: save all history to a file
    • To Console: send the selected commands to the console.
    • To Source : inserted the selected commands into the current script.

History Tab

  • The History tab keeps a record of all previous commands.
    • save icon: save all history to a file
    • To Console: send the selected commands to the console.
    • To Source : inserted the selected commands into the current script.

In the console pane, use ⬆️ to show the previous commands.

R Packages 📦

  • When we start a R session, only built-in packages like base, stats, graphics, etc are available.
  • Installing packages is an easy way to get access to others data and functions.

and more!

Installing R Packages 📦

  • To install a package, for example, the ggplot2 package, we use the command
install.packages("ggplot2")
  • In the right-bottom pane, Packages > Install

Loading R Packages 📦

What happened when you run

ggplot(mpg, aes(x = displ, 
                y = hwy, 
                colour = class)) + 
    geom_point()
  • To use any function or data in ggplot2, we write ggplot2:: followed by the name of the function or data.
ggplot2::ggplot(ggplot2::mpg, 
                ggplot2::aes(
                    x = displ, 
                    y = hwy, 
                    colour = class)
                ) + 
    ggplot2::geom_point()
  • We can load the package into our R session using library().
  • With library(ggplot2), R knows the function and data are from the ggplot2 package.
library(ggplot2)
ggplot(mpg, aes(x = displ, 
                y = hwy, 
                colour = class)) + 
    geom_point()

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
?mean
?mpg

What does the function mean() do? What is the size of mpg?

01-Running R Script

  • Load R package ggplot2 into your Posit Cloud.
## install the package if you haven't!
________(ggplot2)
  • Create a R script named lab01-run-script.R in your 3570-project.

  • Copy and paste the code below into the script, and save it.

bar <- ggplot(data = diamonds) + 
    geom_bar(mapping = aes(x = cut, fill = cut), 
             show.legend = FALSE, width = 1) + 
    theme(aspect.ratio = 1) +
    labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()
  • Source the script. A pretty plot showing up?!