Sie sind auf Seite 1von 6

Introduction to R Studio

R is an open source statistical program, so it is free to use for everyone. It will save much time if you
already install R Studio on your own laptop and practice using it before the tutorial. You can do this
via the following website: https://www.rstudio.com/
You may need to install the base R program first, which can be reached through here:
https://www.r-project.org/

During the tutorial, all commands for R will be displayed as follows:

I am a command!

We will work with R Studio as it is more user friendly than the original R interface. The interface of R
has four compartments.

Note: The R script compartment only opens after you click File - New File - R script.

R Console
Here, you run the commands which tell R what to do. You can run commands from the script you
made (See R Script below), or you can type commands directly in the console. For example, type the
following command in the console and press enter:

print("Hello R!")

Alternatively, you could have written print(“Hello R!”) in your script, select this line, and pressed the
‘Run’ button in the upper bar.
R Script
In the R script, you can type all your commands you perform on the data and save them for next
time. To save the file you click File - Save As, or click the blue disk in the top of the R script screen.
You may want to save your commands so you can rerun them the next time you do the analysis.

Furthermore, while typing commands in the R script, you may want to add some comments for
yourself about the type of analyses you are doing. You can do this by adding a hashtag (#) at the
beginning of a line. This will make anything you type after a comment, and R will not read this as a
command.
R environment
In the tab ‘Environment’ you can see what data and values you have used in each session. For
example, type the following and see it appear in the Environment:

x <- "Hello R!"

Similarly, if you load in a table, it will appear in the R environment. You can then click it and R will
show a how the data looks in a separate screen.

Navigation and Graphics


In this screen, you can navigate through directories in your computer via ‘Files’, and you can view
the graphical representation of any figures you have made with R in ‘Plots’.
Setting up working directories in R
In the "Files" compartment you see some directories.

R chooses a default folder to open in automatically. You want to navigate to a different folder and
create a folder named “MAE_Tutorial” to which you will download the data from Blackboard. You
can press the three dots at the right side of your Navigation screen, which will open a new window
in which you can browse to the desired space. Navigate to your M-disk in this manner and click
‘Open’.
The folders in your M-disk should now appear in your Navigation screen. Click ‘New folder’ to make
a new folder in your M-disk named “MAE_Tutorial”. Then click the newly made folder to enter it.

Next, you should make new subfolders for each part in the tutorial (e.g. Part i, Part ii, etc.). R needs
you to set a working directory, so it knows from which directory it needs to pull data files, and
where it can store any graphs you have created. Do this now.

To set the working directory, navigate in the Files compartment to the appropriate folder, and then
click "More" – "Set as Working Directory". If you have clicked ‘Set as Working Directory’ note that
the console puts our your command via setwd() including the path to your directory. If you want to
include this path in your script you can copy this from the console into your script.

If at some point don’t remember what your current working directory is, you can simply run the
command getwd() and the console will display your working directory.

Note: Do not forget to set a new folder as your working directory every time you change to a new
part of the tutorial!

Packages in R
Not all analyses are standardly available in R. To be able to perform such analyses, you need to
install and load in packages. Packages are collections of R functions, data, and compiled code. Once
installed, they have to be loaded into each session to be used.

Installing packages can be done simply in R by running a command like the one below, where the
package ade4 is an example:

install.packages("ade4")

You might encounter an error saying you don’t have access to a certain folder to install your
libraries. In that case do the following.

.libPaths()

R will give you the paths in which it saves libraries. If you have more than two paths here, copy the
one which saves in the C:/ disk. So not the wurnet one! Then paste that path within the .libPaths()
command within quotation marks as follows.
.libPaths(“C:/the / entire/ path”)

If you re-run .libPaths() now, it should output only one line which is the path you just pasted in. You
can now re-install your library and it should work!

Loading the package in works as follows:

library(ade4)

Loading in the library is something you have to do each time you start up R studio!

End of R intro
If you want to practise a bit further yourself, please use the package swirl in R. It is a very easy way
of getting used to R! You can load it in via:

install.packages("swirl")

library (swirl)

swirl ()

It will explain the rest itself. Enjoy!

Das könnte Ihnen auch gefallen