- Data Exploration
- ggplot2
Ben Weinstein
If you have to repeat the same few lines of code more than once, then you really need to write a function. Functions are a fundamental building block of R. You use them all the time in R and it's not that much harder to string functions together (or write entirely new ones from scratch) to do more.
body(), the code inside the function.formals(), the "formal" argument list, which controls how you can call the function.environment()` which determines how variables referred to inside the function are found.args() to list arguments.f <- function(x) x
f
## function(x) x
formals(f)
## $x
environment(f)
## <environment: R_GlobalEnv>
Question: How do we delete this function from our environment?
Variables defined inside functions exist in a different environment than the global environment. However, if a function is not defined inside one, it will look one level above.
x <- 2
g <- function() {
y <- 1
c(x, y)
}
g()
## [1] 2 1
first <- function(x, y) {
z <- x + y
return(z)
}
first(5, 7)
## [1] 12
add <- function(a, b) {
return(a + b)
}
vector <- c(3, 4, 5, 6)
add(vector[[1]], vector[[2]])
## [1] 7
x <- 5
f <- function() {
y <- 10
c(x = x, y = y)
}
f()
## x y
## 5 10
temp <- function(a = 1, b = 2) {
return(a + b)
}
temp()
## [1] 3
temp(5, 6)
## [1] 11
f <- function(x) {
if (x < 10) {
0
} else {
10
}
}
f(5)
## [1] 0
f(15)
## [1] 10
It's easy to generate reports dynamically in R.
Basic idea: Write data + software + documentation (or in this case manuscripts, reports) together.
Literate programming involves with three main steps:
Results from scientific research have to be reproducible to be trustworthy. We do not want a finding to be merely due to an isolated occurrence. Instill confidence, share data, results and scripts using github.
An incredibly simple semantic file format.Markdown makes it easy for even those without a web-publishing background to write any sort of text (including with links, lists, bullets, etc.) and have it displayed in a variety of formats.
In RStudio, choose new R Markdown file (easiest way)
or create a new file and save it with extension .Rmd.
```r
# some R code
```
Hit the Knit HTML button
What just happened?
knitr reads the Rmd file, finds and runs the code chunks identified by the backticks, and replaces it with the output of the functions.
Knitr will auto make tables, graphs, and even slides.
Everything in this course was made by typing into Rstudio and using knitr. Period. It is a powerful tool.
Make your life easier, safer, and more fun.
git config global user.name your GitHub account name git config global user.email your email
A repository is the location and name for all the files associated with a particular project. The first step is to log into your GitHub account and create a new repository. Make sure you check the box Initialize this repository with a README.
Open Rstudio and go to Project > Create Project > Version Control > Git and paste the url in the github repo
Now do some work in your new R project and create and save some files. The next step is to commit your work essentially making a copy of all of your script files (i.e., .R files) associated with the R project.
*Commit your work, give it some description
*push to the web.
sleep easy.