12 Small Projects
12.1 Code Chunks
Save the following code as CodeChunk.R
# Define New Function
sum_squared <- function(x1, x2) {
y <- (x1 + x2)^2
return(y)
}
# Test New Function
x <- c(0,1,3,10,6)
sum_squared(x[1], x[3])
sum_squared(x, x[2])
sum_squared(x, x[7])
sum_squared(x, x)
message('Chunk Completed')
Clean the workspace In the right panels, manually cleanup
- save the code as MyFirstCode.R
- clear the environment and history (use the broom in top right panel)
- clear unsaved plots (use the broom in bottom right panel)
Replicate either in another tab or directly in console on the bottom left, enter
Note that you may first need to setwd()
so your computer knows where you saved your code.4
After you get this working
* add a the line print(sum_squared(y, y))
to the bottom of MyFirstCode.R
.
* apply the function to a vectors specified outside of that script
# Pass Objects/Functions *to* Script
y <- c(3,1,NA)
source('MyFirstCode.R')
# Pass Objects/Functions *from* Script
z <- sqrt(y)/2
sum_squared(z,z)
CLI Alternatives (Skippable) There are also alternative ways to replicate via the command line interface (CLI) after opening a terminal.
Note that you can open a new terminal in RStudio in the top bar by clicking ‘tools > terminal > new terminal’
12.2 Reports
We will create reproducible reports via R Markdown.
Example 1: Data Scientism.
See DataScientism.html and then create it by
- Clicking the “Code” button in the top right and then “Download Rmd”
- Opening with Rstudio, change the name to your own, change the title to “Data Scientism Replication”
- then point-and-click “knit”
Alternatively, * download the source file from DataScientism.Rmd * change the name to your own, and make any other edits you like * use the console to run
Example 2: Homework Assignment. Below is a template of what homework questions (and answers) look like. Create an .Rmd from scratch that produces a similar looking .html file.
Question 1: Simulate 100 random observations of the form \(y=x\beta+\epsilon\) and plot the relationship. Plot and explore the data interactively via plotly, https://plotly.com/r/line-and-scatter/. Then play around with different styles, https://www.r-graph-gallery.com/13-scatter-plot.html, to best express your point.
Answer I simulate \(400\) observations for \(\epsilon \sim 2\times N(0,1)\) and \(\beta=4\), as seen in this single chunk. Notice an upward trend.
n <- 100
E <- rnorm(n)
X <- seq(n)
Y <- 4*X + 2*E
library(plotly)
plot_ly( data=data.frame(X=X,Y=Y), x=~X, y=~Y)
Question 2: Verify the definition of a line segment for points \(A=(0,3), B=(1,5)\) using a \(101 \times 101\) grid. Recall a line segment is all points \(s\) that have \(d(s, A) + d(s, B) = d(A, B)\).
Answer
library(sf)
s_1 <- c(0,3)
s_2 <- c(1,5)
Seg1 <- st_linestring( rbind(s_1,s_2) )
grid_pts <- expand.grid(
x=seq(s_1[1],s_2[1], length.out=101),
y=seq(s_1[2],s_2[2], length.out=101)
)
Seg1_dist <- dist( Seg1 )
grid_pts$dist <- apply(grid_pts, 1, function(s){
dist( rbind(s,s_1) ) + dist( rbind(s,s_2) ) })
grid_pts$seg <- grid_pts$dist == Seg1_dist
D_point_seg <- st_multipoint( as.matrix(grid_pts[grid_pts$seg==T,1:2]) )
D_point_notseg <- st_multipoint( as.matrix(grid_pts[grid_pts$seg==F,1:2]) )
plot(Seg1)
points(D_point_notseg, col=2, pch='.')
points(D_point_seg, pch=16)
box()
12.3 Posters and Slides
Posters and presentations are another important type of scientific document. R markdown is good at creating both of these, and actually very good with some additional packages. So we will also use flexdashboard for posters and beamer for presentations.
Poster.
See DataScientism_Poster.html and recreate from the source file DataScientism_Poster.Rmd. Simply change the name to your own, and knit the document.
Slides.
Since beamer is a pdf output, you will need to install Latex. Alternatively, you can install a lightweight version TinyTex from within R
Then download source file DataScientism_Slides.Rmd, change the name to your own, and knit the document.
If you cannot install Latex, then you must specify a different output. For example, change output: beamer_presentation
to output: ioslides_presentation
on line 6 of the source file.
12.4 More Literature
For more guidance on how to create Rmarkdown documents, see
- https://github.com/rstudio/cheatsheets/blob/main/rmarkdown.pdf
- https://cran.r-project.org/web/packages/rmarkdown/vignettes/rmarkdown.html
- http://rmarkdown.rstudio.com
- https://bookdown.org/yihui/rmarkdown/
- https://bookdown.org/yihui/rmarkdown-cookbook/
- https://dept.stat.lsa.umich.edu/~jerrick/courses/stat701/notes/rmarkdown.html
- An Introduction to the Advanced Theory and Practice of Nonparametric Econometrics. Raccine 2019. Appendices B & D.
- https://rmd4sci.njtierney.com/using-rmarkdown.html
- https://alexd106.github.io/intro2R/Rmarkdown_intro.html
If you are still lost, try one of the many online tutorials (such as these)
- https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf
- https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
- https://www.neonscience.org/resources/learning-hub/tutorials/rmd-code-intro
- https://m-clark.github.io/Introduction-to-Rmarkdown/
- https://www.stat.cmu.edu/~cshalizi/rmarkdown/
- http://math.wsu.edu/faculty/xchen/stat412/HwWriteUp.Rmd
- http://math.wsu.edu/faculty/xchen/stat412/HwWriteUp.html
- https://holtzy.github.io/Pimp-my-rmd/
- https://ntaback.github.io/UofT_STA130/Rmarkdownforclassreports.html
- https://crd150.github.io/hw_guidelines.html
- https://r4ds.had.co.nz/r-markdown.html
- http://www.stat.cmu.edu/~cshalizi/rmarkdown
- http://www.ssc.wisc.edu/sscc/pubs/RFR/RFR_RMarkdown.html
- http://kbroman.org/knitr_knutshell/pages/Rmarkdown.html
Some other good packages for posters/presenting you should be aware of
- https://github.com/mathematicalcoffee/beamerposter-rmarkdown-example
- https://github.com/rstudio/pagedown
- https://github.com/brentthorne/posterdown
- https://odeleongt.github.io/postr/
- https://wytham.rbind.io/post/making-a-poster-in-r/
- https://www.animateyour.science/post/How-to-design-an-award-winning-conference-poster
You can also use GUI: point-click click ‘Source > Source as a local job’ on top right↩︎