install.packages(c("tidyverse", "here", "patchwork", "gt", "gtsummary", "ggstatsplot", "easystats"))
R package installation
If you already know how to install packages in R, skip down to Section 3 to see which packages to install for the R4PhD course … otherwise keep reading.
1 What is an R package?
The standard installation of R consists of a set of base, or ‘core’, functions. It is possible to extend R by including packages with extra functionality.
These packages are developed by Posit (the company behind RStudio), by other IT companies (e.g. Google, Microsoft, IBM and others), by universities, and by individual R users.
Some of these packages have a very specific and narrow focus – e.g. Steen Harsteds package for handling motion capture data called mocapr. Other packages er more generic and you will get to know some of them during the course. For instance:
tidyverse
– a meta-package which makes it easier to work with the R syntax.ggplot2
– a package for constructing graphs or plots.ggpubr
– a package which extends ggplot2, aiming to make it easier to produce publication ready plots.
There are a couple of technical details about R packages, that you need to understand:
2 Installing versus loading
In order to use a package, you must download and install it onto your computer and then load it into memory when you need to use it.
- You use the function
install.packages()
to download and install a new package from the internet to you computer.- You only need to do this once – therefor, there is no need to include the
install.packages()
call in your scripts. - Instead, run the
install.packages()
function from the console - or use RStudios ‘package’ tab. - When running
install.packages()
you need to provide the package name in quotes, like this:install.packages("my_new_package")
- You only need to do this once – therefor, there is no need to include the
- You use either
library()
orrequire()
to make the package available to your code.- You need to do this every time you use a package in a new code script.
- Therefor your scripts should include the
library()
orrequire()
function calls early in your scripts – before you make use of them in your code. - When running
library()
orrequire()
you can provide the package name with or without quotes.
2.1 CRAN … where R packages hang out
By far the majority of packages are available online through CRAN the Comprehensive R Archive Network. You can feel pretty confident you will find everything you need there.
However, you will not be downloading from the website yourself. Instead, we let R handle the download and installation.
R packages are specific to the underlying version of R – when a new major version of R is published it may take some time for package developers to update their packages. In other words, depending on the packages you make use of, it is not always a good idea to run the absolute latest release of R, as some packages may lag behind … this is mostly an issue if you use ‘niche’ packages, however.
3 Required packages for the R4PhD course
The code below defines which packages you will need to install prior to the R4PhD course.
To install them simply copy the command to the RStudio console – did you notice the little clipboard icon that appears in the to right-hand corner of the code snippet, when you mouse hovers over the code? Click that to copy the code…
Need help installing the packages? See the text and video above.
3.1 Essential packages
You will need to have these installed
3.2 Recommended
We suggest it is a good idea to also install these.
install.packages(c("rstatix", "naniar", "readr", "readxl", "vroom", "colourpicker", "colorspace"))
3.3 Extra
You might also want to explore these packages during the course.
install.packages(c("ggthemes", "gganimate", "ggiraph", "kableExtra", "gtExtras", "glue", "gapminder", "ggside"))
Hvis du allerede ved, hvordan du installerer pakker i R, så spring ned til Section 6 for at se, hvilke pakker du skal installere til R4PhD-kurset … ellers fortsæt med at læse.
4 Hvad er en R-pakke?
Standardinstallationen af R består af et sæt basis- eller ‘kerne’-funktioner. Det er muligt at udvide R ved at inkludere pakker med ekstra funktionalitet.
Disse pakker er udviklet af Posit (virksomheden bag RStudio), af andre it-virksomheder (f.eks. Google, Microsoft, IBM og andre), af universiteter og af individuelle R-brugere.
Nogle af disse pakker har et meget specifikt og snævert fokus – f.eks. Steen Harsteds pakke til håndtering af motion capture data kaldet mocapr. Andre pakker er mere generiske, og du vil lære nogle af dem at kende i løbet af kurset. For eksempel:
tidyverse
– en meta-pakke, der gør det lettere at arbejde med R-syntaksen.ggplot2
– en pakke til at konstruere grafer eller plots.ggpubr
– en pakke, der udvider ggplot2, med det formål at gøre det lettere at producere publikationsklare plots.
Der er et par tekniske detaljer om R-pakker, som du skal forstå:
5 Installation versus indlæsning
For at bruge en pakke skal du downloade og installere den på din computer og derefter indlæse den i hukommelsen, når du skal bruge den.
- Du bruger funktionen
install.packages()
til at downloade og installere en ny pakke fra internettet til din computer. - Du behøver kun at gøre dette én gang – derfor er der ingen grund til at inkludere
install.packages()
funktionskaldet i dine scripts. - Kør i stedet ‘install.packages()’ fra konsollen – eller brug RStudios ‘pakke’-faneblad.
- Når du kører
install.packages()
skal du angive pakkenavnet i anførselstegn, som dette:install.packages("my_new_package")
- Du bruger enten
library()
ellerrequire()
for at gøre pakken tilgængelig for din kode. - Det skal du gøre, hver gang du bruger en pakke i et nyt kodescript.
- Derfor bør dine scripts inkludere
library()
ellerrequire()
funktionskaldene tidligt i dine scripts – før du gør brug af dem i din kode. - Når du kører
library()
ellerrequire()
kan du angive pakkenavnet med eller uden anførselstegn.
5.1 CRAN … hvor R-pakker hænger ud
Langt de fleste pakker er tilgængelige online via CRAN the Comprehensive R Archive Network. Du kan føle dig ret sikker på, at du vil finde alt, hvad du har brug for der.
Du vil dog ikke selv skulle downloade fra hjemmesiden. I stedet lader vi R klare download og installation.
R-pakker er specifikke for den underliggende version af R – når en ny major version af R udgives, kan det tage noget tid for pakke programmørerne at opdatere deres pakker. Alt efter hvilke pakker du gør brug af, er det med andre ord ikke altid en god ide at køre den absolut seneste udgivelse af R, da nogle pakker kan halte bagud … dette er mest et problem, hvis du bruger ‘niche’-pakker dog.
6 Nødvendige pakker til R4PhD-kurset
Koden nedenfor definerer, hvilke pakker du skal installere før R4PhD-kurset.
For at installere dem skal du blot kopiere kommandoen til RStudio-konsollen – har du bemærket det lille clipboard-ikon, der vises i højre hjørne af kodefeltet, når du holder musen over koden? Klik på det for at kopiere koden…
Har du brug for hjælp til at installere pakkerne? Se teksten og videoen ovenfor.
6.1 Vigtige pakker
Du skal have disse installeret
install.packages(c("tidyverse", "here", "patchwork", "gt", "gtsummary", "ggstatsplot", "easystats"))
6.2 Anbefales
Vi foreslår, at du også installerer disse.
install.packages(c("rstatix", "naniar", "readr", "readxl", "vroom", "colourpicker", "colorspace"))
6.3 Ekstra
Du vil måske også udforske disse pakker under kurset.
install.packages(c("ggthemes", "gganimate", "ggiraph", "kableExtra", "gtExtras", "lim", "gapminder", "ggside"))