rnorm(n=100, mean=1, sd=2) |> mean()
[1] 1.277393
How to work effectively in RStudio
Søren O’Neill & Steen Harsted
February 8, 2024
In the following we explain some simple but important points related to your workflow in R and RStudio.
From experience, we have know this causes some confusion and frustration to R-newcomers, so please pay attention to each of the following videos.
A typical workflow consists of ping-pong’ing back and forth between i) testing code in real-time (console and short term memory) and ii) storing working code in the right order in a text file.
Let’s say you want to use the functions mean
and rnorm
to calculate the observed mean of a sample of 100 from a normal distribution with an expected mean of 1. The following code should do the trick and before using that code in you manuscript, you check it out in the console:
You then realize that the result has too many digits and you amend your code:
Open RStudio and create and empty R script by menu: File / New File / R Script or keyboard: CTRL-SHIFT-n
rnorm(n=100, mean=1, sd=2) %>% mean()
into the console and execute it by pressing ENTER – what happens in the text editor?rnorm(n=100, mean=120, sd=25) %>% mean()
into the text editor and press ENTER – what happens in the console?rnorm(n=100, mean=20, sd=250) %>% mean()
into the text editor and press CTRL+ENTER – what happens in the console?Bonus question Amend the code in the text editor so that the output of the R command is ascribed to a variable called x. What happens in the Environment tab of the Environment pane, i) when you added the code to the text file and ii) when you copy/paste/execute (CTRL+ENTER) the code in the console?
When you close an R project, RStudio will ask you whether you want to save a ‘Workspace image’.
A workspace image stores the information currently in the console and the short term memory (Environment) and restores it, the next time you open the project.
I det følgende forklarer vi nogle enkle, men vigtige punkter relateret til din arbejdsgang i R og RStudio.
Af erfaring ved vi, at dette forårsager en vis forvirring og frustration for R-nytilkomne, så vær opmærksom på hver af de følgende videoer.
En typisk arbejdsgang består af ping-pong’er frem og tilbage mellem i) test af kode i realtid (konsol og korttidshukommelse) og ii) lagring af arbejdskode i den rigtige rækkefølge i en tekstfil.
Lad os sige, at du vil bruge funktionerne “middelværdi” og “rnorm” til at beregne det observerede middelværdi af en stikprøve på 100 ud fra en normalfordeling med et forventet middelværdi på 1. Følgende kode bør gøre tricket, og før du bruger den kode i dig manuskript, du tjekker det ud i konsollen:
Du indser så, at resultatet har for mange cifre, og du ændrer din kode:
Åbn RStudio og opret og tøm R-script efter menu: Fil / Ny fil / R Script eller tastatur: CTRL-SHIFT-n
rnorm(n=100, mean=1, sd=2) %>% mean()
ind i konsollen og kør den ved at trykke ENTER – hvad sker der i teksteditoren?rnorm(n=100, mean=120, sd=25) %>% mean()
i teksteditoren og tryk ENTER – hvad sker der i konsollen?rnorm(n=100, mean=20, sd=250) %>% mean()
i teksteditoren og tryk CTRL+ENTER – hvad sker der i konsollen?Bonusspørgsmål Ret koden i teksteditoren, så outputtet af R-kommandoen tilskrives en variabel kaldet x. Hvad sker der på fanen Miljø i ruden Miljø, i) når du tilføjede koden til tekstfilen og ii) når du kopierer/indsætter/udfører (CTRL+ENTER) koden i konsollen?
Når du lukker et R-projekt, vil RStudio spørge dig, om du vil gemme et ‘Workspace-billede’.
Et arbejdsområdebillede gemmer de oplysninger, der aktuelt er i konsollen og korttidshukommelsen (Environment) og gendanner det, næste gang du åbner projektet.