class: center, middle, inverse, title-slide # Level Up 04: Inline code & emo::ji() --- 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 & create a new R Markdown document for this week #### .orange[Reminder]: File > New Project... New Directory > New Project > *Give your project a name & location* File > New File > R Markdown... *Remember to save this file in your r_docs folder!* --- class: inverse # Session Objectives ### Inline Code & emo::ji() 💃 🤑 - Learn how to use inline code - Learn the fundRmental skill of adding emojis to R Markdown files 😀 - Learn how to use the source() function with scripts <br> <br> <br> <br> <br> <br> <br> <br> To check your own work against the examples, download the [R Markdown doc](https://and.netlify.app/seminr/04/level_up/levelup_04.Rmd) & the [shortcut emojis script](https://and.netlify.app/seminr/04/level_up/shortcut_emojis.R) --- class: inverse # Inline Code Two ways of writing code in R Markdown docs .orange[Code Chunks]: <img src="./images/chunk.png" width="75%" /> .orange[Inline Code]: <img src="./images/inline.png" width="75%" /> Inline code is embedded into your write-up & shows the output of your code when knitted --- class: inverse # Inline Code Examples - Can use functions or call on existing objects, super easy to update values & reduces rounding/typing errors - 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 ```r `r function(input)` ``` ```r `r input %>% function(.)` ``` ```r `r value * value` ``` ```r `r object_name` ``` ### .orange[Task]: In your .Rmd file, add the sentence 'My year of birth is', insert inline code of the current year - your age & knit your doc to see the magic happen! (.orange[Hint]: use example 3 as a template) --- class: inverse # Inline Code Examples ### .orange[Task]: ```r `r function(input)` ``` Following the example above, use the mean() function on the following values: 1, 4, 6, 9, 12, report it using inline code & knit your doc ### .orange[Task]: ```r `r 1.1111 %>% round(., digits = 3)` ``` Following the example above, round 1.2937438047 to 4 decimal points, report it using inline code & knit your doc <br> .orange[Top Tip]: Run inline code by placing your cursor on the line & pressing .bold[ctrl/cmd] + .bold[enter] to check it works before knitting --- class: inverse # Inline Code Examples ### .orange[Task]: ```r # create the object in a code chunk object_name_1 <- "some_text" object_name_2 <- a_value ``` ```r `r object_name_1` ``` Following the examples above: Create an object called my_name which contains your name Create an object called my_age which contains your age Create an object called my_star_sign which contains your star sign Create an object called age_in_50_yrs which contains your age + 50 Report your name, age, star sign, & age in 50 years using inline code & knit your doc --- class: inverse # emo::ji() - emojis 'are of substantial importance in this day and age' (Anon, 2021) - Absolutely vital for modern communication - The [emo package](https://www.rdocumentation.org/packages/emo/versions/0.0.0.9000) allows us to use emojis easily in R Markdown docs with the ji() function - .italic.bold[Don't] use emojis in your assignments (as much as we would love to see them) <br> .orange[Task]: Install the emo package ```r install.packages("devtools") # if you need to install devtools devtools::install_github("hadley/emo") # to install the emo package ``` .orange[Task]: Load the emo package & take a look at the full list of emojis in the console ```r library(emo) View(emo::jis) # views the jis dataframe of emoji data ``` --- class: inverse # Inline code with emo::ji ### Using Functions ```r `r emo::ji("money_mouth_face")` # prints emoji matching the keyword/name/alias ``` ### Using Assignment ```r monaay <- emo::ji("money_mouth_face") # create an object in a chunk using <- `r monaay` # use inline code to call on the name of the object anywhere in your doc ``` ### .orange[Task]: Use View(emo::jis) to find your fave emojis & following the examples above, input 5 of them into your R Markdown doc and knit!! --- class: inverse # Using source() The source() function allows you to run R code from a script in your R Markdown document So, you could create a script of all your favourite emojis saved as objects, & use the source() function to read these into .italic[any] R Markdown document - all you need in the .Rmd is the file path to the script file ```r source("filepath/scriptfile.R") ``` ### .orange[Task]: Create a script (.R file) of some of your fave emojis as objects .orange[<-] Use source() to read the script into your R Markdown document Use inline code to include some of them in your R Markdown & knit .orange[Reminder]: when using file paths, to jump up a level use ../ --- 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>