+ - 0:00:00
Notes for current slide
Notes for next slide

Level Up 04: Inline code & emo::ji()

1 / 11

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 # comment their meaning and usage as you go through seminRs, practicals, tutorials etc.

  • Use the Padlet to suggest seminR topics

Task:

Open/create your seminRs project & create a new R Markdown document for this week

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!

2 / 11

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









To check your own work against the examples, download the R Markdown doc & the shortcut emojis script

3 / 11

Inline Code

Two ways of writing code in R Markdown docs

Code Chunks:

Inline Code:

Inline code is embedded into your write-up & shows the output of your code when knitted

4 / 11

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 outside of a code chunk
`r function(input)`
`r input %>% function(.)`
`r value * value`
`r object_name`

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! (Hint: use example 3 as a template)

5 / 11

Inline Code Examples

Task:

`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

Task:

`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


Top Tip: Run inline code by placing your cursor on the line & pressing ctrl/cmd + enter to check it works before knitting

6 / 11

Inline Code Examples

Task:

# create the object in a code chunk
object_name_1 <- "some_text"
object_name_2 <- a_value
`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

7 / 11

emo::ji()

  • emojis 'are of substantial importance in this day and age' (Anon, 2021)
  • Absolutely vital for modern communication
  • The emo package allows us to use emojis easily in R Markdown docs with the ji() function
  • Don't use emojis in your assignments (as much as we would love to see them)


Task: Install the emo package

install.packages("devtools") # if you need to install devtools
devtools::install_github("hadley/emo") # to install the emo package

Task: Load the emo package & take a look at the full list of emojis in the console

library(emo)
View(emo::jis) # views the jis dataframe of emoji data
8 / 11

Inline code with emo::ji

Using Functions

`r emo::ji("money_mouth_face")` # prints emoji matching the keyword/name/alias

Using Assignment

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

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!!

9 / 11

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 any R Markdown document - all you need in the .Rmd is the file path to the script file

source("filepath/scriptfile.R")

Task:

Create a script (.R file) of some of your fave emojis as objects <-

Use source() to read the script into your R Markdown document

Use inline code to include some of them in your R Markdown & knit

Reminder: when using file paths, to jump up a level use ../

10 / 11

Made with Padlet
11 / 11

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 # comment their meaning and usage as you go through seminRs, practicals, tutorials etc.

  • Use the Padlet to suggest seminR topics

Task:

Open/create your seminRs project & create a new R Markdown document for this week

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!

2 / 11
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow