class: center, middle, inverse, title-slide # Level Up 08: Writing Results with Inline Code --- class: inverse # Setup ### .orange[Tasks]: 1. Open the slides for today 1. Open/create your seminRs project 1. Download the [Rmd](https://and.netlify.app/seminr/08/level_up/levelup_08.Rmd) to your project 1. Run the .orange[pckgs] & .orange[data] chunks (& install the weights package if you need to) --- class: inverse # Inline Code 2.0 There are two ways of writing code in an Rmd: .pull-left[ #### Code chunks Generally used separate to body text <img src="./images/chunk.png" width="900" /> ] .pull-right[ #### Inline code Used within body text where R code is enclosed within single backticks <img src="./images/inline.png" width="893" /> ] <br> <br> <br> Today's seminR builds on the introduction to inline code covered in [Level Up Week 04](https://and.netlify.app/seminr/04/level_up/#1) For this session, we're focusing on using inline code to report stats - no emojis today 😢💔 --- class: inverse # Inline Code Recap - Can use functions or call on existing objects, super easy to update values & reduces rounding errors/typos - No need to enter any numbers directly – we can use code instead (i.e. nrow() for sample size) - Must use inline code .bold.italic[outside] of a code chunk & usually surrounded by body text ### Examples ```r `r function(input)` ``` ```r `r input %>% function(.)` ``` ```r `r value * value` ``` ```r `r object_name` ``` --- class: inverse # Reporting Basic Sample Descriptives Let's have a go at using inline code with an easy example: sample size The .orange[nrow()] function allows us to find the sample size of a dataset (by counting rows) There are two ways we can use this function with inline code: .pull-left[ #### Direct <img src="./images/ex1.png" width="729" /> ] .pull-right[ #### Assignment <img src="./images/ex2.png" width="729" /> <img src="./images/ex3.png" width="729" /> ] ### .orange[Task]: In the Rmd, write a sentence reporting the sample size of peng_data_2 & knit your doc! --- class: inverse # Reporting Test Results Using inline code with the results of stats tests is .italic[slightly] more tricky because each result contains multiple elements To see the names of these elements we can use the .orange[str()] function on our test object ```r str(test_object) ``` The output of this function is the list of elements, and a brief description of what they contain <br> <br> <br> ### .orange[Task]: In the console, use .orange[str()] on the ttest_out object & tell me the names of the elements! --- class: inverse # Reporting Test Results: Sub$etting We need to use subsetting to select an element from our stats test output This follows the general structure of: .orange[object$element] Some elements contain multiple values (means, CIs etc.), we can subset these further by using square brackets with the number of the element we want inside, for example: .orange[object$element[1]] would select the first value & .orange[object$element[2]] would select the second value ### Example: <img src="./images/subs.png" width="333" /> ### .orange[Task]: Report the mean flipper length (aka estimate) of the Dream island penguins with inline code --- class: inverse # Solo Tasks! 🥳 ### .orange[Task 1]: Use the .orange[str()] function in the console to see the names of the elements in the cor_out test ### .orange[Task 2]: Using inline code, report .italic[r], Lower CI, Upper CI, & .italic[p] from the cor_out test ### .orange[Task 3]: Knit your document and look at the pretty output! --- class: inverse # Piping into Additional Functions: .orange[rd()] You may notice that your pretty document isn't so pretty after all & contains way too many decimal places APA style recommends using 2 dp for numeric values, and 3 dp for .italic[p] values We can easily edit our inline code to display numeric values to 2 dp using the .orange[rd()] function from weights: <img src="./images/rd.png" width="420" /> In .orange[rd()], 2 dp are used as the default option, we can override this by giving a different value in the brackets ### .orange[Task]: Report .italic[r] from the cor.out object, & pipe it into the rd() function to show it to 2 dp Knit your document and see the difference! --- class: inverse # JennifeR's .orange[report.p()] Function - Custom function to report teeny tiny .italic[p] values properly with inline code - Any .italic[p] values smaller than .001 will show up as "< .001" - The function can be copied into a code chunk (the setup chunk is a good choice): ```r report.p <- function(x){ ifelse(x >= .001, paste0("= ", rd(x, 3)), "< .001") } ``` Then you can use it with inline code like this: <img src="./images/p.png" width="533" /> ### .orange[Task]: Try using JennifeR's report.p() function to report the *p* value from the cor_out test as per APA style --- class: inverse # Reporting Values from Tables To report values from tables, we can use the same methods as our test results previously <br> ### Solo Tasks! 🥳 Using the previous tasks to guide you, try the following tasks with the summary table .orange[desc_table]: .bold.orange[Task 1]: use str() on the desc_table to see the elements .bold.orange[Task 2]: use inline code to report the n for each species of penguin: Adelie, Chinstrap, & Gentoo .bold.orange[Task 3]: report the mean flipper length for Adelie penguins .bold.orange[Task 4]: edit the inline code to report the mean flipper length for Adelie penguins to 2 decimal places --- 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>