knitr::opts_chunk$set(echo = TRUE) library(learnr) tutorial_options(exercise.timelimit = 10, exercise.blanks = "___+")
output <- do_the_thing(input1, input2)
Inputs (arguments) can include:
the objects on which the function acts
additional parameters that specify how the function acts (options)
Calculating mean of a vector
num_vec <- 1:10 mean(num_vec)
Functions don't need to provide output, or take inputs
save(object_to_save, file = 'data_file')
getwd()
Functions that operate on individual values:
sqrt()
abs()
log()
exp()
Functions that estimate statistics across a set of values:
mean()
median()
max()
x <- c(1, 1, 2, 3, 5, 8, 13)
?my_function help(my_function)
Help
tab in RStudio to search.Use tab-complete to quickly check function inputs.
Best to name arguments explicitly (at least the optional ones)
round(pi, digits = 2)
See examples of function being used!
example('round')
What are the inputs and outputs?
glengths <- c(4.6, 3000, 580)
name_of_function <- function(argument1, argument2) { *code that does something based on inputs* return(some_results) }
function
is a special keyword for creating functions
Assign the function to a variable (give it a name)
List inputs ('arguments')
Use curly braces to wrap text that performs some action on any inputs
(Optionally) return
a value from the function
combine_two_numbers <- function(num1, num2) { result <- num1 + num2 return(result) } combine_two_numbers(1, 2)
Don't forget to return
a result
Assign a default value to inputs with =
to make them optional
subtract_from_vec <- function(data_vec, subtract_val = 1) { new_data_vec <- data_vec - subtract_val return(new_data_vec) } test_vec <- 1:10 subtract_from_vec(test_vec) subtract_from_vec(test_vec, subtract_val = 2)
Collections of functions, and data, designed to add specific functionality beyond 'base R'
Need to install before you can use (only do this once)
install.packages('tidyverse')
library(tidyverse)
help
functionhelp(package = 'lubridate')
CRAN is the default 'app store' for R packages
Bioconductor is another that has many useful bioinformatics related packages
install.packages("BiocManager") BiocManager::install("GenomicRanges")
(Looks weird, but just copy-paste)
Once installed they're just like other packages
library(GenomicRanges)
Multiple packages can have the same names for different functions.
Which one R thinks you're using in a given instance can be hard to know.
Cause of many painful 'bugs'
When in doubt it's best to specify which package the function comes from:
library(lubridate) library(here) lubridate::here() here::here()
Pay attention to error messages and warning messages
Using Google
Search error messages verbatim (but remove stuff specific to your usage).
Add "r" "error"
Stackoverflow: almost every problem you encounter is answered here (often 10 different ways!)
Using functions:
output <- do_a_thing(input_obj, param1 = my_val)
Writing your own functions:
do_a_thing <- function(input_obj, param1) { **STUFF** return(result) }
Getting help with functions: help(my_fun)
.
install.packages()
and library()
to install/use packages
Google (and stackoverflow, Biostars, etc.) are your friends!
I'll open up breakout rooms
Go to broad.io/cp_r_bootcamp
Work through Chapters 1 and 2 Practice
There are clickable 'hints' throughout.
Use slack for Q&A.
Use 'ask for help' button and TA can jump into Breakout room to help
Group Slack channels and 'huddle' audio spaces might be helpful too.
Your web browser will save your progress!
You can reset your work in a single code chunk with the 'start over' button
Your work in each code chunk is independent (results don't carry over)
You can reset the whole notebook with the 'Start over' button on the left panel (careful!)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.