3  Setting up R and RStudio

Every journey starts with gathering the right equipment. This intellectual journey is not much different. The first step that every R novice has to face is to set everything up to get started. There are essentially two strategies:

or

While installing R and RStudio requires more time and effort, I strongly recommend it, especially if you want to work offline or make good use of your computer’s CPU. However, if you are unsure whether you enjoy learning R, you might wish to look at Posit Cloud first. Either way, you can follow the examples of this book no matter which choice you make.

3.1 Installing R

The core module of our programming is R itself, and since it is an open-source project, it is available for free on Windows, Mac and Linux computers. So, here is what you need to do to install it properly on your computer of choice:

  1. Go to www.r-project.org

    Screenshot of the *R* Project website homepage, called CRAN, showcasing the latest news and updates about the *R* programming language.

  2. Click on CRAN where it says Download.

  3. Choose a server in your country (all of them work, but downloads will perform quicker if you choose your country or one that is close to where you are).

    Screenshot of the CRAN Mirrors page on the *R* Project website, displaying a list of global locations and corresponding institutions for various CRAN mirror URLs.

  4. Select the operating system for your computer, for example Download R for macOS.

    Screenshot of the CRAN website, displaying options to download and install R, including precompiled binary distributions and source code for all platforms.

  5. Select the version you want to install (I recommend the latest version)

    Screenshot of the *R* for macOS page on the *R* Project website, displaying different versions of the software to download.

  6. Open the downloaded file and follow the installation instructions. I recommend leaving the suggested settings as they are.

This was relatively easy. You now have R installed. Technically, you can start using R for your research, but there is one more tool I strongly advise installing: RStudio.

3.2 Installing RStudio

R by itself is just the ‘beating heart’ of R programming, but it has no particular user interface. If you want buttons to click and actually ‘see’ what you are doing, there is no better way than RStudio. RStudio is an integrated development environment (IDE) and will be our primary tool to interact with R. It is the only software you need to do all the fun parts and, of course, to follow along with the examples in this book. To install RStudio, perform the following steps:

  1. Go to http://posit.co and click on DOWNLOAD RSTUDIO.

    Screenshot of the POSIT website.

  2. Click on DOWNLOAD RSTUDIO on this page.

    Screenshot of the RStudio Desktop download website showing a button indicating 'Download RStudio'.

  3. This leads you to the page where you can install R as a first step and RStudio as a second step. Since we installed R already, we can click on DOWNLOAD RSTUDIO DESKTOP FOR WINDOWS (or for Mac if you are not using a PC).

    Screenshot of the RStudio download website showing step 2, i.e. 'Install RStudio Desktop'.

  4. If, for some reason, the version for your operating system is not showing up correctly, you can scroll down and find other versions ready to be installed.

    Screenshot of the RStudio Download website showing different versions of RStudio available for different operating systems such as Windows, macOS and Ubuntu.

  5. Open the downloaded file and follow the installation instructions. Again, stick with the default settings as much as possible.

Congratulations, you are all set up to learn R. From now on, you only need to start RStudio and not R. Of course, if you are the curious type, nothing shall stop you from trying R without RStudio.

3.3 When you first start RStudio

Before you start programming away, you might want to tweak some of your settings right away to have a better experience (in my humble opinion). To open the Rstudio settings, you have to click on

  • RStudio > Tools > Global Options or press ⌘ + , if you are on a Mac.

  • RStudio > Tools > Global Options or press Ctrl + , if you are working on a Windows computer.

I recommend to at least make the following changes to set yourself up for success right from the start:

  1. Already on the first tab, i.e. General > Basic, we should make one of the most significant changes. Deactivate every option that starts with Restore. This will ensure that every time you start RStudio, you begin with a clean slate. At first sight, it might sound counter-intuitive not to restart everything where you left off, but it is essential to make all your projects easily reproducible. Furthermore, if you work together with others, not restoring your personal settings also ensures that your programming works across different computers. Therefore, I recommend having the following unticked:

    • Restore most recently opened project at startup,

    • Restore previsouly open source documents at startup,

    • Restore .Rdata into workspace at startup

      Screenshot of the settings window of RStudio showing the General settings and various options to enable or disable.

  2. In the same tab under Workspace, select Never for the setting Save workspace to .RData on exit. One might think it is wise to keep intermediary results stored from one R session to another. However, I often found myself fixing issues due to this lazy method, and my code became less reliable and, therefore, less reproducible. With experience, you will find that this avoids many headaches.

  3. In the Code > Editing tab, make sure to have at least the first five options ticked, especially the Auto-indent code after paste. This setting will save time when formatting your coding appropriately, making it easier to read. Indentation is the primary way of making your code look more readable and less like a series of characters that appear almost random.

    Screenshot of the settings window of RStudio showing the Code tab and its features that can be enabled or disabled.

  4. In the Display tab, you might want to have the first three options selected. In particular, Highlight selected line is helpful because, in more complicated code, it is helpful to see where your cursor is.

    Screenshort of the settings window of RStudio showing the section Display as a subsection of the Code tab.

Of course, if you wish to customise your workspace further, you can do so. The visually most impactful way to alter the default appearance of RStudio is to select Appearance and pick a completely different colour theme. Feel free to browse through various options and see what you prefer. There is no right or wrong here. Just make it your own.

Screenshot of the settings window of RStudio showing the Appearance tab and its options to change the visual look of the RStudio IDE.

3.4 Updating R and RStudio: Living at the pulse of innovation

While not strictly something that helps you become a better programmer, this advice might come in handy to avoid turning into a frustrated programmer. When you update your software, you need to update R and RStudio separately from each other. While both R and RStudio work closely with each other, they still constitute separate pieces of software. Thus, it is essential to remember that updating RStudio will not automatically update R. This can become problematic if specific tools you installed via RStudio (like a fancy learning algorithm) might not be compatible with earlier versions of R. Also, additional R packages (see Section 5.4) developed by other developers are separate pieces that require updating too, independently from R and RStudio.

I know what you are thinking: This already sounds complicated and cumbersome. However, rest assured, we look at how you can easily update all your packages with RStudio. Thus, all you need to remember is that R needs to be updated separately from everything else.

3.5 Posit Cloud

Posit Cloud is an application that runs in your web browser. It allows you to write and access R code wherever you go. It even works on your tablet because it does not require installation. However, do expect it to run slower if your internet connection is not fast or stable. To work with Posit Cloud, you also need an internet connection. However, there are many benefits to using RStudio in the cloud, such as running time-consuming scripts without using your own device. Also, it is much easier to collaborate with others, and since no installation is required, you can work on projects on any device as long as you are connected to the internet. However, Posit Cloud’s most significant advantage is that you can get started with programming within seconds compared to a desktop installation. Still, I prefer my locally run offline version of RStudio because I appreciate working offline as much as online. Nevertheless, I recommend setting up an account because you never know when you need it.

To get started with Posit Cloud, we have to undertake a couple of steps:

  1. Open your web browser of choice and navigate to https://rstudio.cloud.

    Screenshot of the Posit Cloud landing page.

  2. Click on Sign Up to create your account.

  3. On the next page, make sure you have the free plan selected and click on Sign up.

    Screenshot showing the Posit Cloud tiers of use, including the 'Cloud Free' tier which allows using Posit Cloud for free.

  4. To finalise the registration process, you are required to provide your credentials.

    Screenshot of the Posit Cloud Sign Up page.

  5. Once you complete your registration, you are redirected to Your Workspace, the central hub for all your projects. As you can tell, I already added another workspace called R for Non-Programmers. However, it is fine to use the default one.

    Screenshot of the Posit Cloud landing page after logging into the website. It shows how clicking on 'New Project enables users to choose a new RStudio project in the cloud.

  6. To start a new R project, you can click on New Project > New RStudio Project. This will open RStudio in the Cloud, which looks identical to the desktop version. You can immediately start writing your code. The example below computes a plot.1

    Screenshot of an Posit Cloud project opened, showing some coding.

  7. Now, we can execute the code as we would on the desktop version (see Chapter 5 ).

    Screenshot of an Posit Cloud project opened, showing some coding and the plot that results from running this code.

Whether you choose to use a desktop version of RStudio or Posit Cloud, you will be able to follow along in this book with no problem.


  1. RStudio and Posit Cloud warn you if you need to install certain R packages to successfully run code. More about R packages and what they are can be found in Section 5.4.↩︎