R and RStudio

R is a programming language for statistical computing and graphics. If you are new to R coding, you are suggested to go to https://www.w3schools.com/r/ and follow the tutorial.

RStudio is a free and open-source integrated development environment (IDE) for R. It provides a powerful and productive user interface for R. You can download R and RStudio from https://posit.co/download/rstudio-desktop/, or sign up with https://posit.cloud/.

To use RStudio, the first thing is to launch RStudio. For your convenience, you may wish to set a default working directory: Tools -> Global Options -> Select a folder.

The usual Rstudio screen has four panels, with each panel having its own menus.

A script is a text file containing a set of commands and comments. The script can be saved and used later to re-execute the saved commands. The comments are only annotations that help users understand what a code piece does and do not affect the execution of the code. An R comment starts with the symbol “#”, as shown below. To create a new R script in RStudio, you can go to: File -> New -> R Script. Now, you are ready to type R code. To run your code, highlight it and click the “Run” button on the menu bar.

R Markdown

R Markdown is a formatting syntax. You can use a single R Markdown file to execute code and generate high-quality, reproducible, and dynamic reports that can be shared with an audience. The generated reports can be slideshows, pdfs, html documents, Word files, and more. R Markdown provides an authoring framework for data science. An R markdown file usually has the file extension “.rmd”.

Within a markdown document you can embed an R code trunk between triple single back quotes, as shown below:

### Your code is here

The “r” in the curly braces indicates that the code is written in R.

Here is an example:

x = 1 # This is not an equation. The left side is called an object. You are storing 1 in object x.

y = 4 # You are storing 4 in object y.

z = x+y-x*y+x^2-sqrt(y)-x/y+exp(x-3*y)   # R is used as a calculator. The result is stored in object z.

z  # Print the object z.
## [1] -0.2499833
a = rnorm(100,20,5)  # Generate 100 values from a normal distribution with mean 20 and standard deviation 5,
                     # and store the result in object a. Since a holds 100 values, it's called a numeric vector.

mean(a)  # Print the mean of data a. 
## [1] 19.95535
median(a)  # Print the median of data a. 
## [1] 20.28273
sd(a)  # Print the standard deviation of data a. 
## [1] 5.120336
summary(a)  # Produce the 5-number summary as well as the mean of data.
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   6.965  16.613  20.283  19.955  22.868  33.227
hist(a)  # Create a histogram for data a. 

boxplot(a)  # Create a boxplot for data a. 

title("Boxplot of data")  # Add a title to the boxplot

You can give your code chunk a label such as “MyRCode”. Here is how things may look like:

x=c(1,3,8,2,9,8,2,1, 3, 13, 21, 11, 6)  # Create a numeric vector holding some values.
summary(x)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.000   2.000   6.000   6.769   9.000  21.000

You can also embed plots with and without R code, for example:

boxplot(x) # Make a boxplot based on numeric vector x created in any previous code chunk.

You can re-size the plot:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Now, running the command knit by clicking the drop-down besides the “Knit” tab on the menu bar creates an output. The R package “knitr” can create outputs in different formats (HTML, PDF, and Word).

R Markdown Tutorial

Click the following for an R markdown tutorial: https://rmarkdown.rstudio.com/lesson-1.html

Note

The R markdown file for generating this page is on D2L. It’s called “Brief Introduction to …”. Download and open it. Click the “Knit” tab on the menu bar of the first panel in RStudio.