---
title: "Intro to R & Positron"
author: Dr. Josh Thompson
date: today
format: html
toc: true
---


After you have downloaded R and Positron (or maybe RStudio) open Positron (or maybe RStudio) and play around with the software.  RStudio was the default IDE (Integrated Developer Environment) for R for many years.

[Positron](https://positron.posit.co/) is a new IDE similar to VSCode, and is designed as an IDE for Data Science, focusing on Python & R.  If you already use VSCode and Python, you should definitely try Positron.  


  Positron has several panes, including a Source Editor, a Workspace Browser, Plots, and a Console - each with various tabs.  Learn more [here](https://positron.posit.co/).  

  RStudio has four main panes, the Source Editor, the Workspace Browser, the Plots, and the Console - each with various tabs.  Learn more [here](https://rladiessydney.org/courses/01-basicbasics-1#your-turn).  

### Realize that R is a calculator.  

Try some calculations in the console.  While you're in the console, browse the other tabs.   

```{r}
3*4 - 5

2*4^2 - 3
```
### c()

Next, the function `c()` stands for *collection* and returns a collection, or list.  Make several collections, varying the kinds of elements it contains.  What do you learn about the behavior? 

```{r}
#| label: play

x <- c(1:25)
x^2

last_names <- c("phillips",1:5,"lebowski")

```

It won't come up as often, but you could also use 'list()', and access like this:

```{r}
# Creating a list with named components
my_list <- list(name = "Alice", age = 30, scores = c(85, 92, 78))
my_list$name
my_list$scores

```


## packages

Many data sets and much of the functionality of R exists as packages. The code below downloads the package `tidyverse` from CRAN.  See [Section 1.4.3](https://r4ds.hadley.nz/intro#the-tidyverse) of the text. 

```{r}
#| label: load-tidyverse
#| eval: false

install.packages("pak")
library(pak)
pkg_install("tidyverse")

```

## library()

The step `pkg_install("tidyverse")` above merely downloads the package to your machine.  It does not load it in R.  To use the package you *check it out from the library* as follows.  This loads many functions and datasets.

```{r}
#| label: library-call

library(tidyverse)

```

Visit section [1.4.4](https://r4ds.hadley.nz/intro#the-tidyverse) for details.

## datasets

Data is everywhere -  you're encouraged to find data on your own.  But to get started we'll use some of the many datasets that are available as packages.  To see a list of datasets available in base R

```{r}
# see a list of data
# some are actual datasets from scientific studies
# some are synthetic data used for training algorithms, academic study, etc.
data()
```


This one is from a scientific study on penguins. It's also contained in the `tidyverse` package.


```{r}
#| label: library-palmer
#| echo: false

 library(palmerpenguins)

```

## help

To examine what is in this package you can use `help` as shown below. 

```{r}
#| label: help-library-palmer
#| echo: true

help(package="palmerpenguins")
```

So now you see `penguins` is a data.frame inside this package. You can explore it in a number of ways. You can type `View(penguins)` to view the data in RStudio.  You just type `penguins`, or you can look a summary of the data with `summary(penguins)`.   To see just the beginning of penguins do this:

```{r}
#| label: head

head(penguins)

```

We have data about the species `r penguins$species[1]`  and its bill length `r penguins$bill_length_mm[1]` mm, among many other things.   We'll learn how to make all kinds of graphics from datasets like this.  One easy thing we can do is compare male and female bill lengths as below. 

Notice the `<-` symbol is an **assignment** of the right side to the left.  It is to be used to assignment, where `=` is used as options to functions.  The `<-` expression can be keyed in RStudio as `option -` in Mac or `alt -` in Windows.

```{r}
a <- c(1:5)
```

## data frames

In the syntax below, `penguins` is a data frame, (like a rectangular matrix) so we can access specific entries like this:

```{r}
penguins[1,2]

penguins[3,]

penguins[,1]
```

## pipe

The `|>` is the **pipe** which sends the left side (data, output of a function) to the right side (the right side is always a function, **note the syntax of the right side** `sum()`.  

```{r}
c(1:5) |> sum()
```

The usage of the pipe may seem weird at first, but it's ubiquitous so **get used to using |>**.  Your code will be more readable and concise.  

Here we use the pipe with to round a set of numbers
```{r}
# make 5 random numbers
a <- 10*rnorm(5)

# two ways to round them
round(a)
a |> round()

# notice that the first parameter to round() is a data frame and the second
# is digits (how many decimal places to keep)

# the pipe always sends its data to the first parameter of the function
a |> round(digits = 2)

# here's a list of numbers with some outliers
a <- c(-100, 0:10, 20)

# the mean
a |> mean()

# again, the pipe sends data to the first argument of the function
# to remove the outliers from the ends we can trim
a |> mean(trim = .1)
```

## ggplot 

The `ggplot` function is *the* main plotting tools we'll use.   Let's see how the pipe ` |> ` is used in the context of ggplot().  The next two  snippets are equivalent.

In the syntax of `ggplot` you notice that **its first argument is a data frame**, (this is similar to most functions) but in the code below it only accepts the `aes()` argument.  This is because what precedes the pipe *always* goes into the first argument of what follows.  We'll learn this in detail later.  

```{r}
#| label: plot penguins 1

penguins_complete <- penguins[complete.cases(penguins),]
  ggplot(penguins_complete,aes(x = body_mass_g,y = bill_length_mm, color = sex)) +
geom_point()
```

**is equivalent to**

```{r}
#| label: plot-male-female-bill-lengths

penguins_complete <- penguins[complete.cases(penguins),]
penguins_complete |> 
  ggplot(aes(x = body_mass_g,y = bill_length_mm, color = sex)) +
geom_point()
```

## Assignment 1 

The following datasets come with the `tidyverse` package.  Choose one and create *some* kind of plot from it.  (Datasets: midwest, USArrests, cars).  

```{r}
# This creates a file in the editor of datasets.  
# Some are clean and easy to use, some are dirty by design.
data()
```


See <https://jonpage.github.io/r-course/intro.html> for inspiration.  Note the syntax to refer to a specific variable `penguins$bill_length_mm`.  

Export the plot as an image.  You can submit the image in class on Wednesday.