class: center, middle, inverse, title-slide # Essentials 04: R Markdown Recap --- class: inverse # Setup & Suggested Workflow - Create one R project file for all seminR sessions, create an r_docs & data folder & save all Rmds and datasets to these folders respectively - Make a cheat sheet of useful functions and .orange[#] comment their meaning and usage as you go through seminRs, practicals, tutorials etc. - Use the [Padlet](https://uofsussex.padlet.org/de84/seminrs) to suggest seminR topics ### .orange[Task]: Open/create your seminRs project #### .orange[Reminder]: File > New Project... New Directory > New Project > *Give your project a name & location* --- class: inverse # Session Objectives ### Writing Pretty Reports with R Markdown - Recap R Markdown Basics - YAML - Code Chunks - Formatting Text - Good Practice & Common Errors --- class: inverse # R Markdown - Package for executing code & writing reports - Makes our work reproducible & dynamic - One file that renders to multiple file types (html, doc, pdf..) ## 3 Main Components - YAML (metadata) - Code (chunks or inline) - Text To create our nicely formatted report, we use the Knit button <img src="./images/knit.png" width="7%" /> which renders our document based on the YAML & includes any code/text in the output --- class: inverse # YAML - First part of an Rmd, enclosed by --- - Contains metadata about your document, i.e. title, author, output type... - Most common cause of errors are forgotten "" or incorrect indentation <br> ### .orange[Task]: Open a new Rmd file Change the YAML so that 1. The author is your name 1. The title is "Essentials 04: Writing Pretty Reports with R Markdown" Save this file to the r_docs folder of your seminRs project (shortcut: ctrl/cmd + s) --- class: inverse # Code Code Chunks Inline Code (today's level up topic) ### Demo - Chunk structure - The 'setup' chunk - How to insert, name & run chunks - Other ways to run chunks - Chunk options - The Knit button --- class: inverse # Code ### .orange[Tasks]: - Delete out the unnecessary code chunks (i.e keep 'setup') - Load tidyverse & palmerpenguins in the setup chunk - Insert a chunk, rename it to be called data - Copy the below code into your data chunk & knit your doc: ```r data <- palmerpenguins::penguins ``` - Change the chunk options so the data chunk is not included in the knitted doc & knit again - Insert a chunk, rename it to be called plot - Copy the below code into your plot chunk & knit your doc: ```r plot(data$body_mass_g) ``` - Change the chunk options so that the code from the plot chunk is not shown & knit again --- class: inverse # Formatting Text We have to 'mark up' our text - Headings specified by # - Emphasis (bold/italic) by enclosing in 1 or 2 * or _ - For new paragraphs/lines end the line with two spaces - For lists, use numbers or hyphens for each line (remember to double space) - Superscript by enclosing in ^, subscript by enclosing in ~ - Equations enclosed by $ <br> <br> If you're super confused with formatting, check out the .italic[Visual Editor] in RStudio for a MS Word-like interface or use the R Markdown [cheat sheet](https://rmarkdown.rstudio.com/lesson-15.html) --- class: inverse # Formatting Text Try out your formatting skills by downloading the [R Markdown file](https://and.netlify.app/seminr/04/essentials/essentials_04.Rmd) .bold.orange[Task]: Edit the extract from Field et al. (2019) to match the one below when it is knitted <p align="center"> <img src="./images/extract.png" width="55%" /> </p> --- class: inverse # Top Tips ### Good Practice (IMO) - Use # comments in your code chunks for future reference/to aid readability - Keep your code chunks nice & tidy - Break down super long code chunks into smaller chunks - Relatedly, don't use one code chunk per line of code - When naming code chunks, avoid spaces (use - instead) - Give your chunks useful names to help organise your analyses easier - For assignments, set your global chunk options to echo = F to hide code by default - Search the R Markdown cheat sheet for additional options --- class: inverse # Top Tips ### Most Common Mistakes & Errors - Using install.packages() in a chunk - Using View() in a chunk - Duplicate code chunk labels - Forgetting an empty line above a heading # - Forgetting two spaces at the end of a line (e.g. lists, new paragraphs) - Incorrect indentation in the YAML --- class: center, middle <div class="padlet-embed" style="border:1px solid rgba(0,0,0,0.1);border-radius:2px;box-sizing:border-box;overflow:hidden;position:relative;width:100%;background:#F4F4F4"><p style="padding:0;margin:0"><iframe src="https://uofsussex.padlet.org/embed/nrud4gk8x63gbfdc" frameborder="0" allow="camera;microphone;geolocation" style="width:100%;height:608px;display:block;padding:0;margin:0"></iframe></p><div style="padding:8px;text-align:right;margin:0;"><a href="https://padlet.com?ref=embed" style="padding:0;margin:0;border:none;display:block;line-height:1;height:16px" target="_blank"><img src="https://padlet.net/embeds/made_with_padlet.png" width="86" height="16" style="padding:0;margin:0;background:none;border:none;display:inline;box-shadow:none" alt="Made with Padlet"></a></div></div>