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

source('MyFirstCode.R')

Note that you may first need to setwd() so your computer knows where you saved your code.3

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.

## Method 1
Rscript -e "source('CodeChunk.R')"
## Method 2
Rscript CodeChunk.R

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 * use the console to run

rmarkdown::render('DataScientism.Rmd')

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

See DataScientism_Slides.pdf

Since beamer is a pdf output, you will need to install Latex. Alternatively, you can install a lightweight version TinyTex from within R

install.packages('tinytex')
tinytex::install_tinytex()  # install TinyTeX

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

If you are still lost, try one of the many online tutorials (such as these)

Some other good packages for posters/presenting you should be aware of


  1. You can also use GUI: point-click click ‘Source > Source as a local job’ on top right↩︎