Skip to content

Latest commit

 

History

History
71 lines (55 loc) · 3.17 KB

01-supp-intro-rstudio.md

File metadata and controls

71 lines (55 loc) · 3.17 KB
layout title subtitle minutes
page
Programming with R
Introduction to RStudio
15

Learning Objectives {.objectives}

  • Get familiar with RStudio interface.
  • Understand the difference between script file and console.
  • Demonstrate useful shortcuts.

Let's start by learning about our tool.

  • Point to the different panels: Console, Scripts, Environments, Plots.
  • Code and workflow are more reproducible if we can document everything that we do.
  • Our end goal is not just to "do stuff" but to do it in a way that anyone can easily and exactly replicate our workflow and results.
  • The best way to achieve this is to write scripts. RStudio provides an environment that allows you to do that.

Interacting with R

There are two main ways of interacting with R: using the console or by using script files (plain text files that contain your code).

The console window (in RStudio, the bottom left panel) is the place where R is waiting for you to tell it what to do, and where it will show the results of a command. You can type commands directly into the console, but they will be forgotten when you close the session. It is better to enter the commands in the script editor, and save the script. This way, you have a complete record of what you did, you can easily show others how you did it and you can do it again later on if needed. You can copy-paste into the R console, but the Rstudio script editor allows you to 'send' the current line or the currently selected text to the R console using the Ctrl-Enter shortcut.

At some point in your analysis you may want to check the content of variable or the structure of an object, without necessarily keep a record of it in your script. You can type these commands directly in the console. RStudio provides the Ctrl-1 and Ctrl-2 shortcuts allow you to jump between the script and the console windows.

If R is ready to accept commands, the R console shows a > prompt. If it receives a command (by typing, copy-pasting or sent from the script editor using Ctrl-Enter), R will try to execute it, and when ready, show the results and come back with a new >-prompt to wait for new commands.

If R is still waiting for you to enter more data because it isn't complete yet, the console will show a + prompt. It means that you haven't finished entering a complete command. This is because you have not 'closed' a parenthesis or quotation. If you're in RStudio and this happens, click inside the console window and press Esc; this should help you out of trouble.

Commenting

Use # signs to comment. Comment liberally in your R scripts. Anything to the right of a # is ignored by R.

Assignment Operator

<- is the assignment operator. It assigns values on the right to objects on the left. So, after executing x <- 3, the value of x is 3. The arrow can be read as 3 goes into x. You can also use = for assignments but not in all contexts so it is good practice to use <- for assignments. = should only be used to specify the values of arguments in functions, see below.

In RStudio, typing Alt + - (push Alt, the key next to your space bar at the same time as the - key) will write <- in a single keystroke.