15 Small Projects
15.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.7
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’
15.2 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.
15.3 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↩︎