RStudio

Table of Contents

1. First Steps in Programming and Markdown: RStudio

Programming means writing instructions for a computer. R is a common programming language that is designed for facilitating statistical analyses and visualizations. In order to make programming easier there are other programs called Integrated Development Environments (IDE) that provide an editor for writing, syntax checking to highlight important code features and facilitate catching errors. They may also allow for visualizations of results and looking up help and other documentation. For this chapter we will install the programming language R, the IDE Rstudio, and will learn how to use a simple markup language to combine text and code in a single document. The markup language we will use for this chapter is called RMarkdown.

2. Overview Of The Steps To Be Taken

  1. Download R
  2. Download RStudio
  3. Verify they are working
  4. Generate a simple Rmd file to see some of the basic components of the markdown syntax
  5. Add some simple computing constructs including a
    • variable
    • for loop
    • function
  6. Produce an Rmd file from the above
  7. And export it to an html file that we can open in a web browser and that displays correctly.

2.1. Download R

R is free statistical software. As it is a very popular language of academic statisticians many new and cutting edge statistical techniques are available for R users that are well vetted. The comprehensive R network (CRAN) maintains download links and instructions.

The CRAN webpage also has links to manuals that are useful for learning more about the language and its functions. While there is great material elsewhere, and you can find it by internet searches, it is still advisable to start at CRAN first. You know that information is curated by the body that funds, develops, and supports the language.

Install R and RStudio on an M2 Mac in March 2023 from Britt Anderson on Vimeo.

A few years ago I recorded some videos about the download process using a Windows computer. If you have trouble following the instructions from this Mac demo you can also look at those older options. This video walks through the R download process. And this one shows a download of Rstudio, but note that some of the website addresses have changed, but the process is largely the same.

2.2. Download RStudio

RStudio is not R. It is a development environment wrapped around R. You can use R without RStudio (that is the way I use it and what I recommend). However, RStudio does provide an easy way to get started. The people who make RStudio have a free desktop version that will provide all the functionality that we will need. They also make commercial tools that build on the basic RStudio functionality.

  1. Here is where you can get RStudio. Choose the right version for your operating system and follow the instructions. Make sure you have downloaded R first (and that you get the correct version for your operating system).

2.3. Verify That Both R and RStudio Are Working

The way to do this is to open the RStudio program and type in some simple equation such as \(2 + 2\) and it should print 4.

2.4. Generate an Rmd file; Try some simple programming; export a webpage

One of our course goals is to promote reproducible research. One of the ways you do this is by sharing the actual code you used for an analysis. Ideally this is embedded in the actual file you use to generate your report and manuscript. But you don't want the code showing in the finished version. Using a markup language to write the report allows you to accomplish all this. You write the article in text. You write the code for statistical tests and plots in the code. You blend the two together to produce a manuscript with the results of the code executed and embedded. You can share the raw file with anyone who wishes to repeat your analysis or edit it.

RStudio supports several different ways of interacting with R and generating output. We will use the Rmd file, which stands for R Mark Down.

We are using R because we will use the R language for our programming later on and markdown because we are using the R markdown syntax (there are other varieties of markdown with slight differences in syntax and functions) for telling RStudio how to format our export file when it runs the code and reads our program. We will use this below to make the file we open in our browser. In short, you will author a web page.

2.4.1. A Video Example of Using RStudio

Pay attention to the difference between the editor window and the console. The console evaluates the code and prints out the answer. You can use it to test whether your R code does what you think it should be doing. The editor is where you mix the code with text. It lies static until you tell it to run a line, a block, or the entire document. It will then ouput the result (if there were no errors that you have to fix) in a viewer that is internal to RStudio. You want to be able to save that output somewhere (or know where RStudio saves it). Then you can open that file in your browser to see the full power of being able to write a simple text file and get produced a fully featured web page.

RStudio Walkthrough from 2020 (should be very similar to 2023 version) from Britt Anderson on Vimeo.

Demonstrating the Power of RMarkdown (2020) from Britt Anderson on Vimeo.

2.5. Using R and RStudio

Programming languages can look very different, but most of the common ones have the same basic constructs. If you familiarize yourself with these common constructs you will be able to get started more quickly, and more easily transition to other programming languages that might fit your future needs better.

2.5.1. Variables, Loops and Functions

  1. Variables

    A variable is something that holds a value. A value doesn't have to be a number. It could be name, character, or a list of pupil diameters, of even some larger data structure. As the name implies a variable may vary in most programming languages. You could for instance use a variable to "count" how many times you have done an operation. Repeating an operation or action several times is often referred to as "looping."

    a = 3
    a = a + 1
    print(a)
    
    4
    
  2. Loops

    Often we use programs to automate things that need to be done over and over again. In many statistical and experimental procedures this need for repetition is common. Consider finding the mean reaction time for a large number of experimental participants who each completed a large number of experimental trials.

    When you have a task to do that needs to run until some condition is met you will often use a while loop. When you need to do something for a set number of time or through a list of items then you will typically use a for loop. A for loop iterates through a structure and executes the same code, the code in the body of the loop each time through.

    for (i in seq(1,10)) {
      print(i)
    }
    
    [1] 1
    [1] 2
    [1] 3
    [1] 4
    [1] 5
    [1] 6
    [1] 7
    [1] 8
    [1] 9
    [1] 10
    

    Org-babel is a way to include code in an org document and have it properly execute. There is, of course, an Emacs manual : C-h i Search for org mode and in the org mode manual look for the "Library of Babel". A pretty extensive tutorial can be found here. A more recent workshop gives examples here.

    We will want some basic text in our init.el file to make things work. See here as well for some hints on getting started with your init.el file.

    (setq org-confirm-babel-evaluate nil)
    (setq org-babel-python-command "python")
      (org-babel-do-load-languages
      'org-babel-load-languages
      '((emacs-lisp . t) 
      (R . t)
      (python . t)))
    
  3. Functions

    Functions in programming languages are black boxes for taking input and returning output. You, the author of the function, fill in the details of the black box, but we users only know what we feed in and what we get back. Each programming language has its own way of telling the processor what is and what is not a function. In R we use the keyword function and assign our code to a name that users can type to call our code. It also allows us to say what the input can be and to give names to the input that are used locally in our function.

    For example, let's say we want to know if a number is even. We can use R's built in function to tell us the remainder of a division. This function is an infix function as we put it INside its arguments. It uses the symbols %%. For example ~ 5 %% 2~ equals 1 because if you divide 5 by 2 you have 1 left over.

    To know if a number is odd or even we only need to know if the remainder when divided by two is or is not zero. To do this we can use a predicate. A predicate is a test that comes back true or false depending on whether a condition is met. Putting this altogether we can write a function that evaluates if a number is odd.

    isOdd <- function(n) {
               return(n %% 2 != 0)
    }
    

    And then we can use our function in a test:

    print(isOdd(5))
    
    [1] TRUE
    

    We could then use this output value to test whether to print the number by using an if statement.

3. Testing Yourself

Try to produce an Rmd file from the above examples and see if you can get it and export it to an html file that we can open in a web browser and that displays correctly.

3.1. First Rmd Assessment

3.1.1. Tasks

Create and save an Rmd file making use of basic markdown conventions.

  1. Put your name in bold.
  2. Put your student ID or other value in italic.
  3. A functioning link to a page on the web like brittlab
  4. An image link that will inline on export. For example:

    linux_user_at_best_buy.png

  1. Comments

    We will be using various markdown dialects to combine text, references, code, data, and analyses in making reproducible research reports. The goal of this assignment is to get you started on discovering and using some of the markdown conventions that will enable you to do so. RStudio has a built in cheat sheet for markdown, but you can easily find others on line. There are several different dialects of markdown.

3.2. Rmd Export to HTML Assessment

3.2.1. Task

Expand on your Rmd file submitted for Your First Rmd File to include additional material.

3.2.2. Goal/Purpose

Markdown allows you to use plain text to give instructions to a compiler to assemble a file with appropriate formatting. By assembling an html file with code and links from a markdown starter file you will see the basics of this approach.

  1. A simple function definition (illustrations and examples will be provided in the lessons).
  2. A code block using your function to produce output.
  3. A for loop that counts from 1 to 10 and prints the even numbers.

Your submission should include both the Rmd file and the compiled html file produced in RStudio (see lessons for example of what these terms mean and what I am looking for).

3.2.3. Instructions

  1. Take your Rmd file from the first Rmd assessment as a starting point.
  2. Add a code block with a simple function definition.
  3. Add another code block using your function to produce output.
  4. Add a code block that executes a for loop that counts from 1 to 10 and prints the even numbers.
  5. Submit both the Rmd file and the compiled html file produced in RStudio (you get the html by using the knit to html button in RStudio.

Date: 2023-03-10 Fri 00:00

Author: Britt Anderson

Created: 2023-05-15 Mon 10:00

Validate