Introduction to R: Using R in RStudio
Table of contents
1. Credit and license.......................................................................................2
2. Working in RStudio.....................................................................................3
3. Working in the Console Panel......................................................................3
3.1. R can sometimes tell that you’re not finished yet (but not often)............................3
3.2. Autocomplete using “tab......................................................................................... 3
3.3. Browsing your command history..............................................................................4
4. Using the History panel for accessing your command history........................4
5. Using the Environment Pane for managing the workspace............................4
5.1. Listing the content of the workspace.......................................................................4
5.1.1. Doing it using the R console..............................................................................5
5.2. Removing variables from the workspace.................................................................5
5.2.1. Doing it using the R console..............................................................................5
6. Using the Files pane for navigating the file system (and more!)....................5
6.1. Setting the working directory...................................................................................5
7. Using the Help panel to, well, get help........................................................6
7.1. How to reach the help documentation.....................................................................6
7.2. How to read the help documentation.......................................................................6
8. Using the Packages pane for dealing with R packages..................................6
8.1. Downloading and installing new R Packages...........................................................7
8.2. Loading R packages................................................................................................. 7
8.3. Unloading a package............................................................................................... 7
8.4. Packages and the environment................................................................................7
8.5. Updating R packages............................................................................................... 7
8.6. A few extra comments............................................................................................. 8
9. Using the Files or Environment panes for importing data files......................8
10. Using the Plots panel................................................................................8
11. Using scripts in the Source panel..............................................................8
11.1. Why use scripts?.................................................................................................... 9
11.2. Writing our first script............................................................................................ 9
11.3. Running our first script.......................................................................................... 9
11.4. Using RStudio to write and run scripts...................................................................9
11.5. Commenting your script......................................................................................10
11.6. Differences between scripts and the command line............................................10
1
,12. Quitting R............................................................................................... 10
1. Credit and license
Not important
2
, 2. Working in RStudio
RStudio has different panes choosing which panes you see and where by going
to View/Panes/Panes layout (recommendation: stay in the default setting)
Two different ways of doing things
o Using the menu-based or panel-based options provided by RStudio
o Command-based (= text-based interface) options from R
3. Working in the Console Panel
Panel labelled “Console”: R will execute the commands you ask it to perform here
3.1. R can sometimes tell that you’re not finished yet (but
not often)
R can sometimes tell that you’re not finished yet, for example:
o Recap: adding 10 and 20: 10 + 20
If you type in 10+ R thinks you haven’t finished typing so there appears a
blinking cursor next to the plus sign on the new line
o If you type in citation( R thinks you haven’t finished typing because it
misses the )
R cannot always tell that you’re not finished yet, for example:
o You can’t hit enter in the middle of a word: after citat you will get an error
because R thinks you are interested in an object called citat and can’t find
it
o If you type citation: a lot of output, but ignore it
If you make an error, for example: citblation( and you don’t want R to execute the
command, just hit the “escape” key and R will return to the normal command
prompt without attempting to execute the command (in R: CTRL-C)
3.2. Autocomplete using “tab
R has a lot of functions with their own arguments you don’t have to remember
them
Autocomplete ability in RStudio, for example:
3
, o If you want to round a number, you start typing ro and then hit the “tab”
key you will get a display with a little window with two panels and you
can choose an option by clicking it or using the arrow keys
o Left: list of variables and functions that start with the letters you
typed in and grey text that had to do with packages
o Right: information about the option you choose
o If you already typed the full name of the function and you are trying to
type the arguments, you can hit the “tab” key you will get a display with
a little window with two panels
o Left: list of argument names of the function
o Right: information about the option you choose
3.3. Browsing your command history
RStudio keeps track of your command history and you can access it in different
ways
o Using the up and down arrow keys very handy if you’ve typed a long
command that had one typo in it
o If you hit the up key, R console will show you the most recent
command you’ve typed
o Hit the “escape” key if you want the text to go away
o If you start to type some text and hit the “command” key and the up arrow
together, you get a window showing the recent command that started with
the same text as you typed
4. Using the History panel for accessing your
command history
History panel on the upper right-hand side of the RStudio window to see all your
recent commands
If you want one of the commands copied to the R console
o Double click on the command in the History panel
o Select the command you want and click on the “to console” button
5. Using the Environment Pane for managing the
workspace
The workspace or the global environment: abstract location in which R variables
are stored
5.1. Listing the content of the workspace
To examine the contents of the workspace: Environment panel in the top right-
hand corner lists all the variables and their values
4
Table of contents
1. Credit and license.......................................................................................2
2. Working in RStudio.....................................................................................3
3. Working in the Console Panel......................................................................3
3.1. R can sometimes tell that you’re not finished yet (but not often)............................3
3.2. Autocomplete using “tab......................................................................................... 3
3.3. Browsing your command history..............................................................................4
4. Using the History panel for accessing your command history........................4
5. Using the Environment Pane for managing the workspace............................4
5.1. Listing the content of the workspace.......................................................................4
5.1.1. Doing it using the R console..............................................................................5
5.2. Removing variables from the workspace.................................................................5
5.2.1. Doing it using the R console..............................................................................5
6. Using the Files pane for navigating the file system (and more!)....................5
6.1. Setting the working directory...................................................................................5
7. Using the Help panel to, well, get help........................................................6
7.1. How to reach the help documentation.....................................................................6
7.2. How to read the help documentation.......................................................................6
8. Using the Packages pane for dealing with R packages..................................6
8.1. Downloading and installing new R Packages...........................................................7
8.2. Loading R packages................................................................................................. 7
8.3. Unloading a package............................................................................................... 7
8.4. Packages and the environment................................................................................7
8.5. Updating R packages............................................................................................... 7
8.6. A few extra comments............................................................................................. 8
9. Using the Files or Environment panes for importing data files......................8
10. Using the Plots panel................................................................................8
11. Using scripts in the Source panel..............................................................8
11.1. Why use scripts?.................................................................................................... 9
11.2. Writing our first script............................................................................................ 9
11.3. Running our first script.......................................................................................... 9
11.4. Using RStudio to write and run scripts...................................................................9
11.5. Commenting your script......................................................................................10
11.6. Differences between scripts and the command line............................................10
1
,12. Quitting R............................................................................................... 10
1. Credit and license
Not important
2
, 2. Working in RStudio
RStudio has different panes choosing which panes you see and where by going
to View/Panes/Panes layout (recommendation: stay in the default setting)
Two different ways of doing things
o Using the menu-based or panel-based options provided by RStudio
o Command-based (= text-based interface) options from R
3. Working in the Console Panel
Panel labelled “Console”: R will execute the commands you ask it to perform here
3.1. R can sometimes tell that you’re not finished yet (but
not often)
R can sometimes tell that you’re not finished yet, for example:
o Recap: adding 10 and 20: 10 + 20
If you type in 10+ R thinks you haven’t finished typing so there appears a
blinking cursor next to the plus sign on the new line
o If you type in citation( R thinks you haven’t finished typing because it
misses the )
R cannot always tell that you’re not finished yet, for example:
o You can’t hit enter in the middle of a word: after citat you will get an error
because R thinks you are interested in an object called citat and can’t find
it
o If you type citation: a lot of output, but ignore it
If you make an error, for example: citblation( and you don’t want R to execute the
command, just hit the “escape” key and R will return to the normal command
prompt without attempting to execute the command (in R: CTRL-C)
3.2. Autocomplete using “tab
R has a lot of functions with their own arguments you don’t have to remember
them
Autocomplete ability in RStudio, for example:
3
, o If you want to round a number, you start typing ro and then hit the “tab”
key you will get a display with a little window with two panels and you
can choose an option by clicking it or using the arrow keys
o Left: list of variables and functions that start with the letters you
typed in and grey text that had to do with packages
o Right: information about the option you choose
o If you already typed the full name of the function and you are trying to
type the arguments, you can hit the “tab” key you will get a display with
a little window with two panels
o Left: list of argument names of the function
o Right: information about the option you choose
3.3. Browsing your command history
RStudio keeps track of your command history and you can access it in different
ways
o Using the up and down arrow keys very handy if you’ve typed a long
command that had one typo in it
o If you hit the up key, R console will show you the most recent
command you’ve typed
o Hit the “escape” key if you want the text to go away
o If you start to type some text and hit the “command” key and the up arrow
together, you get a window showing the recent command that started with
the same text as you typed
4. Using the History panel for accessing your
command history
History panel on the upper right-hand side of the RStudio window to see all your
recent commands
If you want one of the commands copied to the R console
o Double click on the command in the History panel
o Select the command you want and click on the “to console” button
5. Using the Environment Pane for managing the
workspace
The workspace or the global environment: abstract location in which R variables
are stored
5.1. Listing the content of the workspace
To examine the contents of the workspace: Environment panel in the top right-
hand corner lists all the variables and their values
4