Skip to content

Exercises

In this section, you will find the R code that we will use during the course. We will explain the code and output during correction of the exercises.

Reporting - R Markdown

If a data analysis project involves many steps and generation of various plots, one easy and very practical way to bundle and organize all steps of analysis together is to use R markdown files to generate PDF or html reports. These reports both display the R code used as well as the output generated, such as graphics, tables, statistical test results, …

The difference between an R script and an R markdown file is that the code is organized within chunks in the R markdown file. In between the chunks, the user can write text that contains information about the analysis, package versions, links to websites, etc.

To create an R markdown file, go to File > New File > R markdown. Add a name. This will create a new file that already has some example content. As you can see, the R code is organized in chunks highlighed in grey, with details written as free text in between the chunks. We can see that the pound sign (#) is used outside of the R code chunks. In this case, the # symbol does not correspond to a comment, but will indicate header levels for the titles and subtitles within your final document obtained after report generation.

Once the Rmd is ready, the report can be generated by hitting the “Knit” button at the top of the window.

The example Rmd generates the following html report (saved in the same folder as the Rmd file by default), that shows both the code and the resulting output:

You can find a short video that introduces some of the principles of R markdown on Youtube, from the beginning up to minute 23:30. Starting at minute 23:30, this video also introduces ggplot2.

Let’s practice 11

To practice creating your own R markdown script, modify the one that is generated with the example content when you select File > New File > R Markdown.

Create a new Rmd file with the following options at the top (in the top YAML instructions within the 2 dash sequences “- - -“)

  • Title: «Let’s practice»
  • Author: your name
  • Select the «use current date when rendering object» option
  • Default output format: HTML

We will re-run the dimensional reduction exercise, but this time by creating a report. We will create an .Rmd file, and modify it to include the code for analysis and results of dimensional reduction of the FR_FCM_Z3WR data, using the code of Exercise 4 of Day 2.

Now edit your .Rmd template:

1) Modify the YAML metadata section to include a table of content with numbered sections of 2 header levels, and buttons to hide the code.

2) Write a paragraph with a level-1 header, that describes the content of the document.

3) Create a chunk with global options that will print the R source code, include chunk outputs, and hide warnings and messages.

4) Create a chunk that will load the required libraries: flowCore, CATALYST, ggplot2, cowplot.

5) Create a code chunk for each of the following steps, that will run and/or print the output of the following analysis:

a) Import the cleaned flowSet generated after cleaning with flowAI during Exercise 3, as well as the panel from the csv file. Important: modify the path that becomes relative to the location of the .Rmd script. Eg: load("../course_datasets/FR_FCM_Z3WR/fcs_clean.RData"). Create a SingleCellExperiment object.

b) Calculate the UMAP with default parameters, as in Exercise 4.

c) Plot the UMAP, coloring the cells according to donor id and faceting according to time point. Set the chunk option to center the figure, using fig.align = "center" within the chunks option.

d) Create additional chunks by playing with the plots: coloring according to some marker genes, etc. You can also arrange plots in grids using plot_grid() of the cowplot package. Have fun!

6) Save the Rmd file, knit the document to an html report and admire it in your web-browser!

Download solution Rmd file

For tweaking your reports, such as choosing different output formats, or hiding or showing the code within the report, we recommend that you consult the R markdown documentation provided in this Definite guide eBook.

Another useful resource is RStudio’s R Markdown tutorial.

End of Day 4, good job!