Why doesn't RStudio send with R.

R for psychos

If you've never used a programming language before ... that's actually not that bad, because R behaves a little differently for the start anyway.
“Programming language” sounds so daunting because it sounds like complicated computer science stuff, but if you managed to use a calculator in your school days, you can get started with R too.
What the term “programming language” means for us is quite simple:
R follows instructions. Instructions that we can either write in the console (in RStudio the window at the bottom left) and then confirm with the Enter key. R then looks to see if it knows what you are talking about, and then robs it off - and if it knows what to do with your command, it will give you an answer directly.

Good practice5 by the way, is it a Script to create, the script window is then in RStudio at the top left. There you can write one command per line and save it as a file, so you don't have to memorize each command individually during your work, but can simply run the script again and your results will all appear again.

3.1 RStudio

When you open RStudio, you will likely see the following:

Figure 3.1: A fresh RStudio window

On the right side under the tab “Environment” you have an overview of your files and created variables, as well as below with the help and various other stuff that we are not interested in at first.
The large window on the left is the console, which we'll deal with first.

But before we do anything here, let's first create a bit of structure and create a new project.
A project is just a specific folder that you can work in. Ideally, it is also a folder that you can easily find on your computer.

Figure 3.2: Create a new RStudio project

At the end you should have created your first project.
Here you can stay for the rest of the introduction and create scripts and anyway and at all, and if you have saved everything well, you can still access it in three weeks by simply selecting the project from the corresponding bar in RStudio.

Figure 3.3: RStudio remembers your last few projects

Alternatively, you can also open the folder with the project on your computer6, then simply open the file with the RStudio logo and the extension. If you are using Windows, the file extensions are not displayed by default - but the file has the same name that you gave the project and the icon looks something like this:

Figure 3.4: An RStudio project folder

Next we can console and Text editor dedicate: You can usually only write in one of the two areas, and you can recognize the active area by the blinking cursor (a bit like in word programs á la Word) - the console and editor are the most important areas in RStudio, and the Most of your work done.

3.2 Console

Now you have a fresh project and can get started.
The first thing we need to do is familiarize ourselves with the console (the part on the bottom left).
Here you can enter question-answer-like commands, confirm with Enter, and receive an answer.
Here's the calculator analogy again - try the following:

Figure 3.5: Oh dear, it actually works!

The math basics in R:

  • Addition:: -> 417
  • Subtraction:: -> 1999
  • Multiplication:: -> 84
  • Division:: -> 78
  • Exponentiation:: -> 1024
  • Brackets:,:
    • As with the pocket calculator: Better two more than necessary than one too few
    • ... and yes, every open bracket needs a closed bracket, otherwise there will be errors
  • The decimal separator is the point: 12.1 is entered as

Otherwise there are a number of other mathematical functions, and we can make the whole thing look as complex as we want:

Here we see several new things:

First: is really, well, \ (\ pi \). The one with the circle. Already pre-stored as a constant in R, because nobody can find \ (\ pi \) on the keyboard7.
Second: and are Functions. They're pretty important, but we'll get to them in the next section.
What we wrote there looks like this when translated into math ...

\ [2 + \ sin \ left (\ frac {2 \ pi} {3} \ right) \ cdot e ^ {5} \]

... and results in about 130.5. But that's not really the point.

3.2.1 Common Problems

What happened to most people during the R introduction is that they type a command into the console and hit Enter, but the command doesn't complete properly (terminated correctly) has been. This happens, for example, if you forget a closing bracket or have one at the end of the line. In these cases you press Enter and R takes your command, but it realizes that something is missing and waits for the rest of the command.
You can recognize this by the fact that the symbol on the left side of your console is suddenly a symbol instead of one and even pressing the Enter key repeatedly does not change anything.

Figure 3.6: Plus what ?!

You have two options at this point:

  • Press Escape () to abort the command and try again
  • Executes the command correctly, i.e. closes any open brackets etc.

3.3 Text editor

Everything that happens in the console is well and good, but it is fleeting. Think of it like a timeline on Twitter or a Snapchat ... Snapchat stuff or whatever these young people are using these days.
As soon as you have entered more than four or five commands, you have to scroll up to find your old results again. This is perfectly okay for trying something out quickly, but rather impractical for your work, which usually requires something like reproducibility.

There are scripts for this. Scripts are basically just text files in which you enter R commands.
Nice and good one command per line, like in the console.
You can save scripts and send them to other people or upload them or print them out and staple them to the cheek - there are no limits to creativity! You write and save scripts so that you can later find and understand your commands / evaluation / code. You can reproduce the results by executing the code from the script again.

To create your first script, click the button in the upper left corner of RStudio that follows "New file" looks like:

Figure 3.7: Dear Diary: Today I clicked a button. It was very beautiful.

Then the window pops up on the top left and greets you with an empty text field:

Figure 3.8: Well, here too?

At the moment your script is still called - that means your script has no name and is not saved yet. You want to change the latter immediately because all your nice commands will be ruined if you don't save your stuff.
You can either click the anachronistic diskette button to save and give your script a nice name, or you can press or on the Mac - probably the most important keyboard shortcut in the world. If you have problems finding your buttons or naming them, then look in the glossary or google it.

Figure 3.9: The “Save file…” dialog

Give your script a meaningful name. You still want to know what you've been doing in two weeks' time.
You should also make sure that only numbers and letters as well as and are used. Spaces and umlauts (äöü) are indeed in theory no problem, but believe me, as soon as you send your script to fellow students with other operating systems, even the most harmless-looking script can Ü suddenly lead to a series of small problems, the cause of which you would only find after hours of trying (or never). Incidentally, this is not an R-thing, but also applies to everything else; Word & Excel files, PDFs, porn videos ...

Something similar also applies to the text in your script:
Avoid special characters such as emoji whenever possible (even if they are displayed correctly in theory). Spaces are not a problem and should even be used liberally for the sake of readability.

What also helps a lot for readability: Comments.
R generally ignores everything that is to the right of you in scripts and in the console. We'll call this sign either, by the way Rhombus, Paling or Octothorpe. who it hashtag calls unfortunately has to throw € 5 into the millennial box.

With that we can do something like this:

This gives your commands context, and both you and your fellow students can easily find out what the hell you were thinking.
Comments are also useful when you're running a lengthy script but a command causes problems. If you just put a in front of it, that's the line commented out, and is ignored by R.

When you have accumulated a few lines of code, you can run your script.
A script runs from top to bottom (and from left to right) when you click or press “” (on the Mac).
If you only want to execute the current line (where your cursor is right now, is “current”), is enough (Mac:). Here, too, the glossary can help.

3.4 And that over there on the right?

On the right side in RStudio you can find help (Help), the files in your project folder (Files), an overview of the installed packages (Packages), an overview of variables (Environment) and graphics created by you (Plots). If you're reading this in the right order, you probably have no idea what this is all about - and that's why we're going to turn it over to step and step in later sections when you have a little more overview of the basics.