R script is a simple text file (with extension
.R
, for example my_script.R
) containing
commands written in R language.
When an R script is sourced, the lines of the script
are one-by-one copied to the Console
and immediately
executed.
An R script allows to save R commands (without saving, the commands
given in the Console
are executed and forgotten).
⚠️In this course R scripts are not discussed.
An R
Markdown document (with extension .Rmd
, for
example my_report.Rmd
) is an extension of an R
script.
It is a format for developing elegant and reproducible reports.
It is a simple text file consisting of:
The Knit operation converts an R Markdown document
into a report file.
Many report output formats are available: HTML, PDF (printable pages or
presentations), Microsoft Word documents, etc.
(For example: this page is written in R Markdown and knitted to
HTML)
🎥 Watch R Markdown introduction video from RStudio.
➡️Go to Markdown tutorial to quickly practice writing text with Markdown notation.
⚠️Before creating a new document, first check whether you work in the Learning_R project.
From the RStudio menu choose:
File
↣New File
↣R Markdown...
.
In the dialog box you may specify some details of the analysis (later
change is possible):
.Rmd
file; this name
will be given when the document gets saved.)Once accepted, a new document Untitled1
is shown.
The document is not empty; it contains an example written in R
Markdown.
Knitting is the name of the process of conversion from R Markdown text file to a report in one of the target formats (for example: HTML).
Knit
button to start the conversion.Knit
.Viewer
pane.Lesson_1.Rmd
and was knitted to HTML
file Lesson_1.html
):An R Markdown document consists of: header, chunks and free text.
The header:
---
(three minus
symbols).🏃Locate the header in the Figures above and below.
🏃Create a new R Markdown document and make it empty (remove the example text). Then write a header at the top based on the Figure below. Add your name as the author and change the title. Knit to see the effect.
🏃Check the Files panel. Find there the HTML file which was created by knitting.
A chunk:
```{r}
(three backticks,
then r
in curly braces).```
🏃Locate two chunks in the Figure below. Locate a chunk (with a more complex R code) in the Figure above.
🏃Type a new chunk in your document, based on the Figure below. Put a simple R expression into the chunk. Knit to see the effect.
🏃In your document add a new chunk with another R expression. Press the green play button in the upper right corner of the chunk area. Change the expression. Press play again.
A free text:
🏃Locate two free text regions in the Figure below.
🏃In your document add some free text. Try to add a section and possibly subsections. Knit to see the effects.
Important difference:
Environment
).Knit
button processes the whole R Markdown with no
variables in memory (empty Environment
).Useful hints:
Code
↣Insert Chunk
or the keyboard shortcut
shown in the menu.Code
↣Run Region
↣Run Current Chunk
or the keyboard shortcut shown in the menu.Ctrl-Enter
key combination.In the next parts of the course we will discuss how to build a report
for a table loaded from a file pulse.csv
.
We will open a new report file Lesson_2.Rmd
and load the
table. Then we show the table. In case of troubles consult the images
provided below.
🏃Follow the steps from the sections above to create a new R
Markdown document Lesson_2.Rmd
. Be sure to work in the
project Learning_R
(where you copied the table
file).
🏃In the R Markdown document: fill the header of the file with correct author/date and remove all demo chunks.
In this course we extend R with functionality provided by the tidyverse
package.
It might be needed once to install this package using
menu Tools
↣Install Packages...
: type
tidyverse
in Packages
field and press
Install
.
Usually there are many packages installed, but you need only a few of
them.
The following command loads the library – allows to use
the package tidyverse
:
library( tidyverse )
🏃In your R Markdown document create the first chunk with this command. Run the chunk (some messages will be shown).
Next, the function read_csv
reads the
table from the file provided in the argument.
We assign the result (which is the loaded table) to the variable
pulse
:
pulse <- read_csv( "pulse.csv" )
🏃Type this command in a second chunk. No error should be shown when you run this chunk. Also no result because it gets assigned to the variable.
Finally, when you print the variable, it should show you a few lines of the table.
pulse
🏃Add the above line at the end of the second chunk. Run the chunk.
🏃Finally knit the complete document. There should be no errors and a report should be shown.
Copyright © 2024 Biomedical Data Sciences (BDS) | LUMC