February 23, 2018

We are building R programs, no R documents

Last semester we built documents, like papers and slides

These are files with .Rmd extension

This semester we build programs and scripts

These are files with .R extension

Question What are “file extensions”?

Editing and Executing Code

To create a new file you use the File -> New File menu:

Executing Code

Executing a Single Line

To execute the line of source code where the cursor currently resides you press the Ctrl+Enter key (or use the Run toolbar button):

Executing Multiple Lines

We have seen two ways to execute multiple lines:

  • Select the lines and press the Ctrl+Enter key (or use the Run toolbar button)

  • To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).

Here Source means “run all code from the file”

Source on Save

When editing functions you may wish to set the Source on Save option for the document

Enabling this option will cause the file to automatically be executed every time it is saved

Setting Source on Save ensures that the function is always in sync with its source

It is also a good way to check the syntax as you write a function

Keyboard Shortcuts

Beyond the keyboard shortcuts described above, there are a wide variety of other shortcuts available. Some of the more useful ones include:

Ctrl+Shift+N
New document
Ctrl+O
Open document
Ctrl+S
Save active document
Ctrl+1
Move focus to the Source Editor
Ctrl+2
Move focus to the Console

Bugs

bəɡ

Bugs

noun

  1. a small insect.
    • informal a harmful microorganism, as a bacterium or virus.
  2. an insect of a large order distinguished by having mouthparts that are modified for piercing and sucking.
  3. a miniature microphone, typically concealed in a room or telephone, used for surveillance.
  4. an error in a computer program or system.

Debugging with RStudio

Debugging is designed to help you find bugs

To do this, you need to:

  • Begin running the code
  • Stop the code at the point where you suspect there is a problem
  • Walk through the code, step-by-step

Stopping on a line

Editor breakpoints

The most common way to stop on a line of code is to set a breakpoint.

You can do this by clicking to the left of the line number, or by pressing Shift+F9.

Using the debugger

Once your code is stopped, you will enter “debug mode”

Environment window

Usually in R you’re interacting with the “global environment”

In debug mode, RStudio shows the currently executing function’s environment instead.

  • The objects you see in the Environment pane belong to the current function
  • your commands will be evaluated in the context of the function

Environment window

Code window

The code window shows you the currently executing function. The line about to execute is highlighted in yellow

Console

Console

While debugging, you’ll notice two changes to the R console

The first is that the prompt is different:

Browse[1]> 

This prompt indicates that you’re inside the R environment browser.

Console

While debugging you can use all the normal commands, plus this:

  • Commands are evaluated in the current environment
    • if your function has a variable named x, typing x at the prompt will show you the value of that variable
  • Simply pressing Enter at the console will execute the current statement and move on to the next one
  • Several special debugging commands are available

New toolbar on top of the console:

This toolbar provides buttons for debug control commands

There’s no difference between using the toolbar and entering the commands at the console directly, so it’s helpful to learn the command shortcuts if you spend much time debugging.

Extra commands when debugging

Command Shortcut Description
n or Enter F10 Execute next statement
s Shift+F4 Step into function
f Shift+F6 Finish function/loop
c Shift+F5 Continue running
Q Shift+F8 Stop debugging

You can also type help at the Browse[N]> prompt

Traceback

The traceback shows you how execution reached the current point, from the first function that was run (at the bottom) to the function that is running now (at the top).

Recursive functions

Application

Another kind of pattern

We know that Computational Thinking has four parts

  • Decomposition
  • Pattern recognition
  • Abstraction
  • Algorithms

When we see a pattern we can make loops or functions

A function can be part of itself

Sometimes the easiest way to define a function is to use the same function. For example:

  • A tree has a trunk and branches. Each branch is a trunk and branches.
    • In other words, each branch is a little tree
  • Each Fibonacci number is the sum of the last two Fibonacci numbers
    • 0, 1, 1, 2, 3, 5, 8, 13, 21, …
  • The factorial of a \(n\) is \(n!\), defined as \[n! = n\cdot(n-1)!\]

Let’s write our own factorial

facto <- function(n) {
    m <- n-1
    ans <- n * facto(m)
    return(ans)
}

What is wrong here?

Exit condition

facto <- function(n) {
    if(n <= 1) {
        ans <- 1
    } else {
        m <- n-1
        ans <- n * facto(m)
    }
    return(ans)
}

Conditional

if <> then

if then else