Skip to content
Snippets Groups Projects
Commit afa1e5c7 authored by Carine Rey's avatar Carine Rey
Browse files

initial commit

parent fbc29114
No related branches found
No related tags found
No related merge requests found
Pipeline #1128 passed
.Rproj.user
.Rhistory
.RData
.Ruserdata
intro_rmarkdown.Rproj
# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/plain-html
pages:
stage: deploy
image: registry.gitlab.com/quarto-forge/docker/polyglot
script:
- make
interruptible: true
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
---
title: "USEFUL INFORMATION: Short introduction to Rmarkdown"
subtitle: "UE NGS - ENS LYON"
author: "NGS-team - 2023"
title-block-banner: true
format:
html:
toc: true
toc-title: Contents
theme: united
number-sections: true
embed-resources: true
anchor-sections: true
code-overflow: wrap
code-line-numbers: false
code-copy: true
code-block-border-left: true
code-block-bg: true
execute:
echo: true
eval: false
warning: false
editor: source
---
```{r}
#| include: false
1+1
#keep this chunk, quarto need at least one R chunk for a good rendering
```
# Making your research reproducible
We have already made a case about reproducibility in this training. In this lesson we will focus on one of the tools to enable and empower you to perform analysis reproducibly.
When you do lab work, you use lab notebooks to organize your methods, results, and conclusions for future retrieval and reproduction. The information in these notebooks is converted into a more concise experimental description for the Methods section when publishing the results. Computational analysis requires the same diligence! The equivalent of a lab notebook for computational work is a detailed log of the workflow used, the tools at each step, the parameters for those tools and last, but not least, the versions of the tools.
<p align="center">
<img src="img/reproducibility.jpg" width="800">
</p>
*Image source: "Reproducible Research in Computational Science", Peng 2011 https://doi.org/10.1126/science.1213847*
## RMarkdown for R analysis
Creating the "gold standard" code is not always easy depending on what programming language you are using. For analyses within R, RStudio helps facilitate reproducible research with the use of R scripts, which document all code used to perform a particular analysis. However, we often don't save the version of the tools we use in a script, nor do we include or interpret the results of the analyses within the script.
In the first part of this session we will be learning about **[RMarkdown](https://rmarkdown.rstudio.com/)**. RMarkdown is a file format in its most basic form, that can eventually be converted into a shareable document, e.g HTML, PDF and many others. It allows you to document not just your R (Python and SQL) code, but also enables the inclusion of tables, figures, along with descriptive text. **Thus resulting in a final document that has the methods, the code and interpretation of results all in a single document!**
To elaborate, you write a file using the **Markdown language** and within it **embed executable R code chunks**. The code chunks are paired with **knitr syntax**, so that once your document is complete, you can easily convert it into one of several common formats (i.e. HTML, PDF, PPT) for sharing or documentation.
**Nothing better than an example to convince you !**
<div class="boxy boxy-clipboard-list boxy-orange">
**Exercices #1**
1. Open a new Rmarkdown file (File > New File > R Markdown... ), a dialog box will open, add your name and keep the other values by default and click on **OK**.
- A template file will be generated to give you a starting point to modify for your analyses.
<p align="center">
<img src="img/rmd_default_template.png" width="450">
</p>
2. Save it as "default_template.Rmd" and "knit" the document to generate an HTML document
<p align="center">
<img src="img/r-knit-button.png" width="150">
</p>
3. A web page will pop-up (You may have to allow the page to pop-up)
<p align="center">
<img src="img/rmd_default_template_html.png" width="450">
</p>
4. Change the title and knit a new time
5. Add a sentence below the "## R Markdown" and knit once again
</div>
## RMarkdown basics
[Markdown](https://en.wikipedia.org/wiki/Markdown) is a lightweight markup language with **plain-text-formatting syntax**. It is often used for formatting README files, writing messages in online discussion forums, and creating *rich text* documents using a plain text editor. The Markdown language has been adopted by many different coding groups, and some have added their own "flavours". RStudio implements an **"R-flavoured markdown"**, or **"RMarkdown"**, which has really nice features for text and code formatting.
The [RStudio cheatsheet for Rmarkdown](https://github.com/rstudio/cheatsheets/blob/master/rmarkdown-2.0.pdf) is quite daunting, but includes more advanced Rmarkdown options that may be helpful as you become familiar with report generation, including options for adding interactive plots RShiny.
### Components of a `.Rmd` file
Let's take a closer look at the "raw" file and understand the components therein.
**1. A file header in YAML format**
```
---
---
title: "Super title"
author: "Toto"
date: "2023-03-24"
output: html_document
---
```
This section has information listed in [YAML format](https://yaml.org/), and is usually used to specify metadata (title, author) and basic **configuration** information (output format) associated with the file. You can find detailed information about specifications that can be made in this section on [this webpage](https://bookdown.org/yihui/rmarkdown/html-document.html).
**2. Descriptive text**
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
```
The syntax for formatting the text portion of the report is relatively easy. You can easily get text that is **bolded**, *italicized*, ***bolded & italicized***. You can create "headers" and "sub-headers" to organize the information by placing an "#" or "##" and so on in front of a line of text, generate numbered and bulleted lists, add hyperlinks to words or phrases, and so on.
Let's take a look at the syntax of how to do this in RMarkdown:
<p align="center">
<img src="img/rmd-syntax.png" width="650">
</p>
You can also get more information about Markdown formatting [here](http://rmarkdown.rstudio.com/lesson-1.html) and [here](http://rmarkdown.rstudio.com/authoring_basics.html).
**3. Code chunks**
<img src="img/rmd_chunk.png" width = "350">
The basic idea behind RMarkdown is that you can describe your analysis workflow and provide interpretation of results in plain text, and intersperse chunks of R code within that document to tell a complete story using a single document. Code chunks in RMarkdown are delimited with a special marker (\`\`\`). Backticks (\`) commonly indicate a chunk of code.
Each individual code chunk should be given a **unique** name. The string after `r` between the curly brackets (`{r name}`) at beginning of chunks. The name should be something meaningful, and we recommend using [snake_case](https://en.wikipedia.org/wiki/Snake_case) for the names whenever possible.
There is a handy `Insert` button within RStudio that allows you to insert an empty R chunk in your document without having to type the backticks etc. yourself.
<p align="center">
<img src="img/rmd_chunk_insert_button.png" width = "400">
</p>
Alternatively, there are keyboard shortcuts available as well.
* <kbd>Ctrl</kbd> + <kbd>Alt</kbd> + <kbd>i</kbd> **for PC users**
* <kbd>Command</kbd> + <kbd>option</kbd> + <kbd>i</kbd> **for Mac users**
Finally, you can write inline R code enclosed by single backticks (\`) containing a lowercase `r`. This allows for variable returns outside of code chunks, and is extremely useful for making report text more dynamic. For example, you can print the current date inline within the report with this syntax: `` `r Sys.Date()` ``. See how we implement this in the YAML header.
For the **final code chunk in your analysis, it is recommended to run the `sessionInfo()`** function. This function will output the R version and the versions of all libraries loaded in the R environment. Documenting the versions of the tools you used is important for reproduction of your analysis in the future.
### Generating the report
Once we have finished creating an RMarkdown file, we finally need to "knit" the report. You can knit the files by using the `knit()` function, or by just clicking on "knit" in the panel above the script as we had done in our first activity in this lesson.
<div class="boxy-red boxy-exclamation">
Note that when creating your own reports, you will very likely find yourself knitting the report periodically as you work through rather than just once at the end. It is an iterative process usually since you may have to turn off warnings, or if you decide you need a figure to be larger/smaller, or updating the descriptive text in the document to be informative (for others and your future self).
</div>
When you click on the "knit" button, by default an HTML report will be generated. If you would prefer a different document format, this can be specified in the YAML header with the `output:` parameter as discussed above, or you can also click on the button in the panel above the script and click on "Knit" to get the various options as shown in the image under the 5th part of the exercise above.
<div class="boxy-red boxy-exclamation">
**Note**: *PDF rendering is sometimes problematic, especially when running R remotely, like on the cluster. If you run into problems, it's likely an issue related to [pandoc](http://pandoc.org) and latex instalation.*
Only html file can be responsive.
</div>
<div class="boxy boxy-clipboard-list boxy-orange">
**Exercices #2**
1. Scroll down to the end of your Rmd template file document. **Add a new code chunk.** Within the code chunk place the operation 1+1. Knit your document.
2. **Add a new section header** ("New section") above the newly created code chunk and **a new sub section header** ("New sub section").
3. You can click on the **Outline** button on the top right corner to have the table of content of your document and thus more easily navigate in your document.
<p align="center">
<img src="img/rmd_outline_button.png" width = "150">
</p>
4. Add a **bold** text, an *italic* text, a list
5 . Add an image to your Rmd. (First, save the image you want to display on your VM)
```{r , eval=F, accordion=TRUE}
![](img.png){width=250px}
```
![](img/R.png){width=250px}
You can try to center it
```{r , eval = F, out.width = "30%", fig.align = "center", accordion=TRUE}
#using option of the chunk: {r , out.width = "30%", fig.align = "center"}
knitr::include_graphics("img/R.png")
```
```{r echo = FALSE, out.width = "30%", fig.align = "center", eval=T}
#using option of the chunk: {r , out.width = "30%", fig.align = "center"}
knitr::include_graphics("img/R.png")
```
</div>
___
These materials have been developed by members of UE NGE teaching team of the ENS de Lyon. These are open access materials distributed under the terms of the Creative Commons Attribution license (CC BY 4.0), which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited.
![](img/cc_by_sa.png)
___
all: public/index.html
public/:
mkdir -p public
public/index.html: public/ Intro_Rmd.qmd
quarto render Intro_Rmd.qmd
mv Intro_Rmd.html public/index.html
# intro_Rmarkdown # UE NGS: Short introduction to Rmarkdown
USEFUL INFORMATION UE NGS - ENS LYON
## Getting started https://ue.gitbiopages.ens-lyon.fr/ue-ngs/presentations/intro_Rmarkdown/
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
```
cd existing_repo
git remote add origin http://gitbio.ens-lyon.fr/ue/ue-ngs/presentations/intro_rmarkdown.git
git branch -M main
git push -uf origin main
```
## Integrate with your tools
- [ ] [Set up project integrations](http://gitbio.ens-lyon.fr/ue/ue-ngs/presentations/intro_rmarkdown/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
img/R.png 0 → 100644
img/R.png

149 KiB

img/cc_by_sa.png

1.48 KiB

img/r-knit-button.png

20.6 KiB

img/reproducibility.jpg

59.2 KiB

img/rmd-syntax.png

185 KiB

img/rmd_chunk.png

7.08 KiB

img/rmd_chunk_insert_button.png

58.9 KiB

img/rmd_default_template.png

89.3 KiB

img/rmd_default_template_html.png

121 KiB

img/rmd_outline_button.png

4.41 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment