[R Markdown](https://rmarkdown.rstudio.com/) is a flavor of [[Markdown]] for [[R]]. Create R Markdown files in [[RStudio]] (*File > New File > R Markdown*). Convert to HTML or PDF using [[knit]].
You can name a code block with `{r <name>}` when opening a code block (three backticks).
Use `{r <name>, echo=FALSE}` when opening a code block to exclude the code from the final document.
Use `{r <name>, message=FALSE, warnings=FALSE}` to suppress messages and warnings, respectively.
## best practices
- Include all libraries used in the first code cell
- Use relative paths
- Put any data you are using in a data folder
- `if(!file.exists("data")){dir.create("data")}`
- Comment out (or delete) debugging code
- Include test cases and their output (when relevant)
- End with a code block that has `sessionInfo()` to print key details for reproducibility
## worst practices
- Use `setwd` to change the working directory
- Install packages in R Markdown (you may end up installing a package on someone else's machine)
## tips
- Name code blocks (each name must be unique). This helps with debugging during knit.
- `{r <name>}`
- Knit frequently: errors in code execution will translate to knitting errors that can be difficult to debug. You can set `eval = FALSE` to turn off code blocks one by one for debugging.
- Use the console to test unfamiliar code. In the History tab, click `To Source` to copy the code into the document at the cursor.