Working in the text editor

RStudios teksteditor
Author

Søren O’Neill & Steen Harsted

Published

February 8, 2024


1 Key points

R scripts

  • have the file mime type .R, e.g. filename.R
  • contain only R code
  • can be executed manually on a per-line basis (CTRL+ENTER or button Run)
  • can be executed in its entirety (CTRL+ALT+R, button Source or menu: Code/Source)

Quarto markdown

  • have the file mime type .qmd, e.g. filename.qmd
  • contains a mix of R code in chunks and markdown between chunks (+ a YAML)
  • the R code can be executed, on a per-line basis, per chunk basis or all preceding chunks
  • the markdown can not be executed, but the entire document can be rendered.

Highlighted areas in the text editor (quarto document)

When working in a quarto document, the text should be divided into one (and only one) YAML at the beginning of the document. The YAML is delimited by two lines consisting of ---.

Below the YAML, the text will consist of markdown, with any number of inserted R chunks. Notice, the difference in background colour (which is contingent upon the chosen Editor Theme in Tools / Global Options / Appearance).

Each R chunk is delimited by lines of ```{r} and ```.

Notice the inline r-code in line 15 (orange box), which is delimited by `r and `.

2 Visual versus Source code

Try out the buttons Visual and Source in the top left hand corner. Also note the YAML line editor(line 4), which sets the default choice for visual versus source editor.

3 Document outline

Notice the icon Outline in the top right hand corner, which shows/hides a hierachial outline of the documents headings.

4 Code outline

Also notice the icon in the bottom left hand corner (purple box) – in the illustration above it is entitled ‘Subheading’, but this dynamically changes contingent upon where the cursor is located.

5 Hovedpointer

R scripts

  • har fil mime-typen (‘efternavnet’) .R, f.eks. filnavn.R
  • indeholder kun R-kode
  • kan eksekveres manuelt på per-linje basis (CTRL+ENTER eller knappen Kør)
  • kan eksekveres i sin helhed (CTRL+ALT+R, knap Kilde eller menu: Kode/Kilde)

Quarto markdown

  • har fil mime-type .qmd, f.eks. filnavn.qmd
  • indeholder en blanding af R-kode i chunks og tekst (+ en YAML)
  • R-koden kan udføres på per-linje-basis, per chunk-basis eller alle-foregående-chunks
  • markdown kan ikke eksekveres, men hele dokumentet kan render’es til et output format.

Fremhævede områder i teksteditoren (quarto-dokument)

Når du arbejder i et Quarto dokument, skal teksten opdeles i én (og kun én) YAML i begyndelsen af dokumentet og prosateksten derefter. YAML er afgrænset af to linjer bestående af --- og indholder en hierakisk definition af forskellige variabler, som f.eks title: "My Manuscript Title".

Under YAML vil selve prosa teksten følger, som består af markdown og et antal indsatte R chunks. Bemærk forskellen i baggrundsfarven i RStudios teksteditor (som er betinget af det valgte Editor Theme i Tools / Global Options / Appearance).

Hver R-chunk er afgrænset af linjer med ```{r} og ```.

Bemærk også inline r-koden i linje 15 (orange boks), som er afgrænset af `r og `.

6 Visuel versus kildekode

Prøv knapperne ‘Visuel’ og ‘Source’ i øverste venstre hjørne. Bemærk også YAML-linjen editor(linje 4), som indstiller standardvalget for visuel versus kildeeditor.

7 Dokumentoversigt

Læg mærke til ikonet Outline i øverste højre hjørne, som viser/skjuler en hierakisk kontur af dokumentets overskrifter.

8 Kodeoversigt

Læg også mærke til ikonet i nederste venstre hjørne (lilla felt) – i illustrationen ovenfor hedder det ‘Underoverskrift’, men dette ændrer sig dynamisk afhængigt af, hvor markøren er placeret.