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”?
February 23, 2018
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”?
To create a new file you use the File -> New File menu:
To execute the line of source code where the cursor currently resides you press the Ctrl+Enter
key (or use the Run toolbar button):
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”
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
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
Ctrl+O
Ctrl+S
Ctrl+1
Ctrl+2
noun
Debugging is designed to help you find bugs
To do this, you need to:
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
.
Once your code is stopped, you will enter “debug mode”
Usually in R you’re interacting with the “global environment”
In debug mode, RStudio shows the currently executing function’s environment instead.
The code window shows you the currently executing function. The line about to execute is highlighted in yellow
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.
While debugging you can use all the normal commands, plus this:
x
, typing x
at the prompt will show you the value of that variableEnter
at the console will execute the current statement and move on to the next oneThis 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.
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
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).
We know that Computational Thinking has four parts
When we see a pattern we can make loops or functions
Sometimes the easiest way to define a function is to use the same function. For example:
facto <- function(n) { m <- n-1 ans <- n * facto(m) return(ans) }
What is wrong here?
facto <- function(n) { if(n <= 1) { ans <- 1 } else { m <- n-1 ans <- n * facto(m) } return(ans) }
if <> then