Introduction to RStudio
Last updated on 2024-11-19 | Edit this page
Estimated time: 15 minutes
Overview
Questions
- How do I use the RStudio graphical user interface?
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+Return 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+Return), 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.
Comments remind yourself of what certain functions you write do, and it
helps others understand their purpose without having to read the code.
Start your comments with a #
signs, which R takes as the
sign to ignore anything to the right of it when running the code. To
comment multiple lines in RStudio, you can select the lines you want to
comment and use Ctrl+Shift+C to
comment/uncomment.
Also remember that expressive naming of functions, arguments and
variables, as well as #'
-prefixed roxygen
documentation, document your code. Consider free-form, #-comments as
a last resort.
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.
Key Points
- Using RStudio can make programming in R much more productive.