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



Writing Functions

Dr. Mine Dogucu

1 / 20

Definining functions

double <- function(number) {
number*2
}
2 / 20

Definining functions

double <- function(number) {
number*2
}

The name of the function is double() and it takes one argument number.

3 / 20
double(number = 3)
## [1] 6
4 / 20
double(number = 3)
## [1] 6
double(3)
## [1] 6
5 / 20

Setting a default argument

double <- function(number = 5) {
number*2
}
6 / 20
double()
## [1] 10
7 / 20

Having multiple arguments

multiply <- function(number, by) {
number*by
}
8 / 20

Having multiple arguments

multiply <- function(number, by) {
number*by
}
multiply(number = 10, by = 3)
## [1] 30
9 / 20

Goal

Can we get the top 50 highest ranked feature movies for any year?

10 / 20

Scraping the 2010 movie titles

library(tidyverse)
library(rvest)
page <- "https://www.imdb.com/search/title/?title_type=feature&year=2010-01-01,2010-12-31&sort=user_rating,desc"
read_html(page) %>%
html_nodes(".lister-item-header a") %>%
html_text()
## [1] "Lebensraum/Living Space"
## [2] "Revolver: Poor Boyz"
## [3] "Bohlol Dana - A Sage of Baghdad"
## [4] "Tebaatusasula"
## [5] "Townbiz"
## [6] "Die Haushaltshilfe"
## [7] "Min pinlige familie og mutantdræbersneglene"
## [8] "Dhinveynugehithaamaigaa"
## [9] "It's Always Sunny in Philadelphia: Cast & Creators Live at the Paley Center"
## [10] "DranDeh"
## [11] "Truth & Life Dramatized Audio Bible"
## [12] "Les palmiers blessés"
## [13] "Staying Gold"
## [14] "Love Equation"
## [15] "The Final Journey"
## [16] "The Ballad of Frankie Silver"
## [17] "Les Misérables in Concert: The 25th Anniversary"
## [18] "Doraleous and Associates"
## [19] "A3D Ayumi Hamasaki Arena Tour 2009 A: Next Level"
## [20] "Kisses, Chloe"
## [21] "Barriere"
## [22] "Unbelehrbar"
## [23] "New Kids on the Block: Coming Home"
## [24] "In Focus"
## [25] "Inception"
## [26] "Nobody Smiling"
## [27] "American Falls"
## [28] "The Clock"
## [29] "Heroes Crossing"
## [30] "The Corners"
## [31] "The Toll"
## [32] "Rat Rod Rockers!"
## [33] "Urban Wolf"
## [34] "Grand Slammed"
## [35] "Jen"
## [36] "Supériorité"
## [37] "The New Normal"
## [38] "Freedom Hills"
## [39] "Vecinos"
## [40] "Sesame Street: C is for Cookie Monster"
## [41] "Green Day: 21st Century Breakdown"
## [42] "Life of the Party: Vol. II"
## [43] "National Theatre Live: Nation"
## [44] "The Wizard of Agni"
## [45] "Slednecks 13"
## [46] "Writer's Block"
## [47] "Black Eyed Peas 3D: Live"
## [48] "Step Away from the Stone"
## [49] "The Beekeepers"
## [50] "Out of Focus"
12 / 20

Scraping the 2011 movie titles

page <- "https://www.imdb.com/search/title/?title_type=feature&year=2011-01-01,2011-12-31&sort=user_rating,desc"
read_html(page) %>%
html_nodes(".lister-item-header a") %>%
html_text()
## [1] "Ashi Fasli Nanachi Tang"
## [2] "Imported Affairs"
## [3] "Deswa"
## [4] "Ka Oryang"
## [5] "Year of the Rat"
## [6] "Love on a Leash"
## [7] "Losers in Love"
## [8] "The Script: Homecoming"
## [9] "Rescue Team"
## [10] "Howling at the Moon"
## [11] "Sans pudeur ni morale"
## [12] "Kill Bill: The Whole Bloody Affair"
## [13] "Virtual Trek Wildlife Park"
## [14] "The Nutcracker and the Mouse King"
## [15] "Flashback"
## [16] "Myth & Mystery"
## [17] "American Actor"
## [18] "Blood Eclipse"
## [19] "So Much Pain So in Love"
## [20] "The Phantom of the Opera at the Royal Albert Hall"
## [21] "The Second Times of Troubles"
## [22] "At the Jersey Shore"
## [23] "Stalemate"
## [24] "Gente mala del norte"
## [25] "Mr. Plinkett's Indiana Jones and the Kingdom of the Crystal Skull Review"
## [26] "S.a.l.i.m & Co.'s Café"
## [27] "Villa Vevrier"
## [28] "Big Weekend"
## [29] "The Racket Boys"
## [30] "The Soft Touch"
## [31] "Men's Egg Drummers"
## [32] "Frankenstein"
## [33] "Egression"
## [34] "She's Out of His Mind"
## [35] "Stan"
## [36] "Faust Sonnengesang"
## [37] "Röhrls Katze"
## [38] "Much Ado About Nothing"
## [39] "Marija's Own"
## [40] "Bettada Jeeva"
## [41] "Deadmau5 Live @ Earls Court"
## [42] "40-Life"
## [43] "Payin' the Price"
## [44] "Acharya"
## [45] "Nine Lives: A Musical Adaptation Live"
## [46] "Tangled Up in Blue"
## [47] "South of Southern"
## [48] "Chocolate"
## [49] "Taken in"
## [50] "Erase una vez en Bolivia"

All we are changing is the date in the URL.

13 / 20

We cannot just keep copying and pasting our code for each year. It gets tiring!!!

14 / 20

We cannot just keep copying and pasting our code for each year. It gets tiring!!!

Whenever you catch yourself copying and pasting code and only modifying a tiny part (e.g. year in the URL), write a function to automate the process. The tiny part that needs the modification can be the argument of the function.

15 / 20

16 / 20

17 / 20

18 / 20
scrape_movie_title <- function(year) {
page <- paste0("https://www.imdb.com/search/title/?title_type=feature&year=", year, "-01-01,", year, "-12-31&sort=user_rating,desc")
read_html(page) %>%
html_nodes(".lister-item-header a") %>%
html_text()
}
19 / 20
scrape_movie_title(2002)
## [1] "Venganza"
## [2] "Welcome to San Pedro"
## [3] "The Cloud of Unknowing"
## [4] "Americanos"
## [5] "Punarjani"
## [6] "Ayodhya Ramayya"
## [7] "JJ Cale in Session"
## [8] "Papsajt"
## [9] "Mage Wam Atha"
## [10] "Check Mate"
## [11] "Bakshis"
## [12] "Destiny 101"
## [13] "Engaging Peter"
## [14] "A Mero Hajur"
## [15] "Regarding Penelope's Wake"
## [16] "Montagna con Forza"
## [17] "Book of Danny"
## [18] "2 Fläschchen"
## [19] "Dreaming in Black and White"
## [20] "Until Morning"
## [21] "The Wishing Stone"
## [22] "Only in Hollywood"
## [23] "Living with the Fosters"
## [24] "Someone Is Sleeping in My Pain: An East-West Macbeth"
## [25] "Blackmailing Santa"
## [26] "The Lord of the Rings: The Two Towers"
## [27] "My Father's Daughter"
## [28] "Phulkumar"
## [29] "West Bank Brooklyn"
## [30] "My Brother's Light"
## [31] "Matir Moina"
## [32] "City of God"
## [33] "Manmandir"
## [34] "Khandan"
## [35] "Simhadriya Simha"
## [36] "Crackaparkz"
## [37] "Appu"
## [38] "Developing Sheldon"
## [39] "Le Chant de la Noria"
## [40] "Who the Hell Is Bobby Roos?"
## [41] "Mutual Admiration Society"
## [42] "Filantropica"
## [43] "Sexgunsmoney@20"
## [44] "The Pianist"
## [45] "Nomad"
## [46] "Kanyadaan"
## [47] "Yuva Ratna"
## [48] "Peggy and Fred in Hell: The Complete Cycle"
## [49] "Enki-Benki"
## [50] "Yearbook"
20 / 20

Definining functions

double <- function(number) {
number*2
}
2 / 20
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