options(htmltools.dir.version = FALSE) knitr::opts_chunk$set(fig.align="center", fig.width=5, fig.height=5, warning = FALSE, message = FALSE)
library(xaringanthemer) duo_accent( primary_color = "ivory", secondary_color = "midnightblue", header_font_google = google_font("Roboto", "400"), text_font_google = google_font("Lato", "300"), code_font_family = "Fira Code", code_font_url = "https://cdn.rawgit.com/tonsky/FiraCode/1.204/distr/fira_code.css", header_color = "#f54278", title_slide_text_color = "#354a66" )
function: reusable code that performs a well-articulated task
--
--
What types of task:
--
--
--
arguments: the inputs to functions
placed inside the ()
some arguments may be mandatory; others optional
--
?plot
We can think of 3 sources of functions:
--
--
--
A package: is a collection of functions and data.
Some examples:
ggplot2: a package with graphical functions
dplyr and tidyr: packages for data wrangling
caret: a package for machine learning
data.table: a package for making operations with extremely large data sets fast
Comprehensive R Archive Network (CRAN): The official repository of packages for the R language.
knitr::include_graphics("images/CRANScreen.png")
install.packages
only needs to be run once to install the package.
gets the package from the Comprehensive R Archive Network (CRAN)
install.packages("genius")
library: gives you access to the functions and data of a package that is already installed
sg <- genius_lyrics(artist = "Kes", song = "Savannah Grass")
--
Now use library:
library(genius) sg <- genius_lyrics(artist = "Kes", song = "Savannah Grass")
sg
reactable::reactable(sg)
Write a function th * input: * a vector of variables * a number n
What are the steps?
Calculate the counts of
Sort the counts
Identify the n most common
return the identified values
Do these steps correspond to functions we are familiar with?
Calculate the counts of
Sort the counts
Identify the n most common
return the identified values
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.