RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (2024)

Deze blog legt uit hoe je kan programmeren in R door gebruik te maken van de integrated developer environment (IDE) RStudio. Deze worden vaak hand in hand gebruikt en wij raden ook iedere data scientist aan die in R wil programmeren om gebruik te maken van RStudio.

Heb je R, RStudio of beiden nog niet geïnstalleerd, volg dan eerst onze handleiding voor R installeren.

Wist je dat R een steeds belangrijke rol speelt in de wereld van Data Science? De belangrijke packages van Tidyverse, zoals ggplot2 en dplyr maken R een uitstekende taal om te gebruiken voor Data Science.

Wil je meer leren over R? Lees dan deze uitgebreide blog: wat is R?

Nadat beide programma's op je computer geïnstalleerd zijn, gaan we ons verdiepen in de belangrijkste onderdelen van RStudio.

Wat is RStudio?

RStudio is de standaard R IDE. De interface bestaat uit drie vensters:

  • De console (rood deelvenster)
  • De omgeving (groen deelvenster)
  • Bestanden, plots, hulp, etc. (blauwe deelvenster)

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (1)

Er bestaat nog een vierde venster, de teksteditor, maar bij het opstarten wordt deze niet automatisch geopend. Om hem te openen kun je bijvoorbeeld een nieuw R Script creëren. Klik op Bestand > Nieuw bestand > R Script of klik op de knop die een wit blad voorstelt, gemarkeerd met een klein groen kruisje in de linkerbovenhoek, en vervolgens op R Script. Voor een uitgebreide uitleg over het creëren van R scripts verwijzen we je naar ons blog over R scripts maken.

Lees ook: Hoe maak ik een histogram in R?

De console in RStudio

Laten we beginnen met het rode deelvenster: de console. De console is waar je je code kunt uitvoeren door middel van commando’s. Probeer bijvoorbeeld 1+1 te typen en vervolgens op Enter te drukken. Je zult als het goed is het antwoord onderaan de console zien verschijnen. Hoewel het mogelijk is om alles via de console te doen, is het veel handiger om een R Script te creëren en van daaruit je commando’s te runnen. Zo kun je al je werk makkelijk bijhouden en later bijvoorbeeld opnieuw uitvoeren.

Met vertrouwen waardevolle inzichten halen uit data met R? Bekijk dan onze opleiding R voor data analyse.

De Omgeving (The Environment) in RStudio

De omgeving houdt alle variabelen en functies bij die door de gebruiker gedefinieerd worden. Stel jij runt het volgende commando:

a <- 1

Met de bovenstaande code definieer je een nieuwe variabele a met een waarde van 1. Dit is niet anders dan elke andere programmeertaal, zoals Java of Python. Het unieke aan RStudio is het overzicht die het biedt van alle in het geheugen opgeslagen variabelen. Zo zal na het uitvoeren van bovenstaande regel je omgeving er als volgt uitzien:

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (2)

Dit betekent ook dat je nu berekeningen kunt uitvoeren met a. Probeer maar eens de volgende regel uit te voeren:

a + 1

Je zult zien dat RStudio als resultaat in de console 2 weergeeft. Om de geschiedenis van al je uitgevoerde code te zien kun je op het rechter tabblad History of Geschiedenis klikken.

Overige (Bestanden, Plots, Packages, Hulp)

Het laatste deelvenster (het blauwe vlak in de eerste afbeelding) is waar je al het overige vindt, zoals je bestanden, de visualisaties, de geïnstalleerde pakketten en de hulp documentatie.

Bestanden

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (3)

Hier zie je een simple bestandenverkenner. Zo kun je rechtstreeks vanuit RStudio bestanden openen, hernoemen en verplaatsen. Je kunt hier ook je ‘working directory’ instellen. Dit is de map waarin R zoekt naar bestanden bij bijvoorbeeld het importeren van data.

Plot

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (4)

Hier zie je alle gegenereerde plots. Dit tabblad opent zichzelf automatisch zodra er een plot aangemaakt wordt. Probeer maar eens de volgende regel uit te voeren:

plot(1:10)

Je zult zien dat er vanzelf een grafiek tevoorschijn komt. Indien je meer dan één grafiek hebt geplot, kun je tussen de plots navigeren door middel van de blauwe pijlen. Wil je het venster in een nieuw (groter) venster bekijken? Dat kan! Druk hiervoor op de knop met het vergrootglas genaamd Zoom. Daarnaast kun je een plot exporteren door op Export te klikken. Deze knoppen bevinden zich net onder het tabblad Plot (zie de afbeelding boven).

Packages

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (5)

Hier zie je alle geïnstalleerde pakketten. Alleen basis-functionaliteiten worden standaard met R meegeleverd. Alle extra functionaliteiten moeten vanuit packages geïnstalleerd worden. Vanwege het feit dat R open source is kan iedereen code schrijven en publiceren als een package. Je kunt elk pakket dan ook gratis gebruiken. Sommige packages worden standaard geïnstalleerd, andere moeten later geïnstalleerd worden door de gebruiker. Nieuwe pakketen installeren kan met een commando:

install.packages("naam_van_package") 

Vergeet hierbij niet de "" rond de naam van het package te plaatsen! Zodra het pakket is geïnstalleerd, moet je het pakket eerst nog laden en voordat je alle functies kunt gebruiken die het bevat.

Om een package te laden, run je:

library(naam_van_package)

Deze keer is "" rond de naam van de package optioneel, maar mag indien gewenst nog steeds gebruikt worden.

Er bestaat ook de mogelijkheid om packages te installeren en te laden via de knoppen onder de tab Packages. Klik hiervoor op de knop Installeren onder Packages, typ de naam van het pakket dat je wilt installeren en klik vervolgens op Installeren. Je zult zien dat er code in de console verschijnt. Om het pakket te laden, zoek je het pakket dat je wilt laden in het venster Packages (je kunt het zoekvak gebruiken), en klik op het selectievakje naast de naam van het gewenste pakket. Ook nu wordt er code in de console uitgevoerd. Zie de figuren hieronder indien nodig. Merk op dat je packages slechts eenmaal hoeft te installeren, maar dat je packages opnieuw moet laden elke keer dat je RStudio opnieuw opstart. Bovendiend is een internetverbinding vereist om een package te installeren, maar dit is niet vereist om een package te laden. De package staat namelijk al geïnstalleerd op je lokale computer.

Hulp in het gebruik van RStudio

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (6)

Hier vind je documentatie over alle functies die je in R kunt gebruiken. Om de hulppagina van een functie te openen, voer je het commando help(naam_van_functie) uit. Je kunt ook gewoon simpelweg ?naam_van_functie uitvoeren. Bijvoorbeeld, om de hulp over de mean functie te zien run je:

?mean

Als laatste optie kun je ook simpelweg op F1 drukken terwijl je de cursor op een functie hebt.

Samenvatting

In dit blog hebben we uitgelegd hoe jij aan de slag kan gaan met R door het te gebruiken in RStudio. We hebben de verschillende panelen uitgelegd en de functionaliteit die elke biedt. Hopelijk kan jij nu snel aan de slag zodat ook jij een data science pro kunt worden.

Wil jij goed leren werken in R? Tijdens onze Opleiding R leer je alles wat je nodig hebt om zelfstandig analyses uit te voeren in R.

RStudio: wat is het en hoe werk ik met R in RStudio? [+ installatie] (2024)

FAQs

How to install RStudio step by step? ›

To install RStudio Desktop:
  1. Install R.
  2. Go to the RStudio website, navigate to the RStudio Desktop Download page, and download RStudio Desktop.
  3. Run the installer and follow the prompts.
  4. Click the desktop icon to initialize RStudio.

How to check if R is installed? ›

How to check if R is installed on a Windows PC
  1. Check if there is an “R” icon on the desktop of the computer that you are using. If so, double-click on the “R” icon to start R. ...
  2. Click on the “Start” menu at the bottom left of your Windows desktop, and then move your mouse over “All Programs” in the menu that pops up.

Do I need to install both R and RStudio? ›

Even if you use RStudio, you'll still need to download R to your computer. RStudio helps you use the version of R that lives on your computer, but it doesn't come with a version of R on its own.

How to install an R package in RStudio? ›

or click Tools > Install packages. Write the package name in the dialog, then click install. Once you install the package, you need to load it so that it becomes available to use. Simply use library() function.

How do I install R for the first time? ›

To Install R

Click on the "Download R for Windows" link at the top of the page. Click on the "install R for the first time" link at the top of the page. Click "Download R for Windows" and save the executable file somewhere on your computer. Run the .exe file and follow the installation instructions.

How do I run an R program in RStudio? ›

To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter. In the next figure data was read, and a histogram created, with the lessR Histogram function.

How to see where R is installed? ›

Use the whereis command:

This will show you the location of the R binary and other R-related files.

How do I know my R version in RStudio? ›

Alternatively, if you're in RStudio, you can type and run “sessionInfo()” in the R Console. The first line that the console returns is the version of R that you're using. You can then download and install the latest version of R here for Mac, and here for Windows.

Can I use R without installing? ›

By compiling R to WebAssembly a user can visit a website and run R code directly within the web browser, without R installed on their device or a supporting computational R server. All that is required is a normal web server, including the type of cloud hosting service provided by Github Pages or Netlify.

Why install R before RStudio? ›

We need to install two things onto your computer. The first is R, this is the programming language we will use. After that we need to install RStudio, this is a front end program that lets you write R code, view plots, and do many other useful things.

What is the difference between R and RStudio? ›

First time users of R are often confused as to the difference between R and RStudio. RStudio is actually an add-on to R: it takes the R software and adds to it a very user-friendly graphical interface.

How do I switch between R versions in RStudio? ›

Switching R versions in Windows
  1. In RStudio, goto "Tools" then "Global Options"
  2. Click "Change" next to the "R version" drop down menu and a window will pop out, allowing you to change the R version.
  3. For Mac users, download rswitch to change R version used https://rud.is/rswitch/.

How to check if R package is installed? ›

To see what packages are installed, use the installed. packages() command. This will return a matrix with a row for each package that has been installed.

How do I create a new R package in RStudio? ›

Package creation

Once you've come up with a name, there are two ways to create the package. Call usethis::create_package() . In RStudio, do File > New Project > New Directory > R Package. This ultimately calls usethis::create_package() , so really there's just one way.

How do I install a new version of RStudio? ›

If you want to update R and RStudio:

One way is to run the actual R program. There, you can go to the “R” menu and click “Check for R Updates” (see image below). If you do that, R will tell you the current version you're on, and whether or not there is a more updated version that you can download (circled in blue).

How to install RStudio on Mac? ›

Install R and RStudio for Mac
  1. To install R, go to cran.r-project.org. ...
  2. Click Download R for (Mac) OS X.
  3. Check the Latest release: section for the appropriate version and follow the directions for download.
  4. Once the file download is complete, click to open the installer. ...
  5. Once the R installer has finished, click Close.
Dec 14, 2023

How to install free RStudio? ›

Installing RStudio Desktop
  1. Select the blue Download button under the free, open-source version of RStudio Desktop.
  2. Select the installer associated with your computer's OS.
  3. Once the download completes, open the installer and follow the directions to install RStudio Desktop on your computer.

Can I use RStudio without installing R? ›

R and RStudio are not the same thing. We can run R without RStudio if we need to, but we cannot run RStudio without R. Remember that!

Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6749

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.