class: title-slide <br> <br> .pull-right[ # Writing Functions ## Dr. Mine Dogucu ] --- ## Definining functions ```r double <- function(number) { number*2 } ``` -- The name of the function is `double()` and it takes one argument `number`. --- ```r double(number = 3) ``` ``` ## [1] 6 ``` -- ```r double(3) ``` ``` ## [1] 6 ``` --- ## Setting a default argument ```r double <- function(number = 5) { number*2 } ``` --- ```r double() ``` ``` ## [1] 10 ``` --- ## Having multiple arguments ```r multiply <- function(number, by) { number*by } ``` -- ```r multiply(number = 10, by = 3) ``` ``` ## [1] 30 ``` --- ## Goal Can we get the top 50 highest ranked feature movies for any year? --- [Featured Movies](https://www.imdb.com/search/title/?title_type=feature&year=2010-01-01,2010-12-31&sort=user_rating,desc) <img src="img/featured.png" width="50%" style="display: block; margin: auto;" /> --- ## Scraping the 2010 movie titles ```r 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" ``` --- ## Scraping the 2011 movie titles ```r 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. --- 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. --- class: center middle <img src="img/url.001.jpeg" width="80%" style="display: block; margin: auto;" /> --- class: center middle <img src="img/url.002.jpeg" width="80%" style="display: block; margin: auto;" /> --- class: center middle <img src="img/url.003.jpeg" width="80%" style="display: block; margin: auto;" /> --- ```r 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() } ``` --- ```r 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" ```