Notes from a guest lecture I delivered in March 2019 at the Department of Computer Science at Stella Maris College, Chennai, offering a simple introduction to GitHub with R and R Markdown.
But what is version control?
- Simple analogy: Google Drive for developers
- Track and manage changes to your code
- Version control lets developers safely work through branching and merging
- Work with online Git repository
- Make changes in your own branch
- Merge with online cloud branch
- Changes are tracked and can be reverted if needed
Make your account and install into RStudio
install.packages("devtools")
- If this doesn't work, you might need to update your R
- Update at https://www.r-project.org/Start your first project
Quick note
*.Rproj
in your .gitignore
)readme.md
More information: https://www.datacamp.com/community/tutorials/git-setup
Very simple guide on the workflow: http://rogerdudler.github.io/git-guide/
A more detailed guide: https://happygitwithr.com/rstudio-git-github.html
Use man git
in Shell for help
git commit -m "Commit message"
git push origin master
git pull
“R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code, like the document below.” - https://rmarkdown.rstudio.com/articles_intro.html
Very simple. Next time you make a new file, make it a R Markdown file instead of a R script and save it as x.Rmd
Chunks
A mini-R-script in the middle of your text, headings, etc. in Markdown
E.g.:
library(tidyverse)
# Making a random dummy dataframe
x <- data.frame(replicate(5,sample(0:1,1000,rep=TRUE)))
# Print three rows of our dummy df
x[1:3,]
## X1 X2 X3 X4 X5
## 1 1 0 0 0 0
## 2 1 1 0 1 0
## 3 0 1 1 1 0
Chunk Options
You might not want to show your code, the warnings, and messages but show the output—Rmd let's you control that.
E.g.: There's a hidden chunk below but you'll only see the table it outputs:
Cool table |
---|
X1 |
0 |
1 |
Global Options
You can also initialize a setup chunk with global chunk options like below.
knitr::opts_chunk$set(include = TRUE, warning = FALSE) # Random chunk options
Here's a cheat sheet!
Cmd or Ctrl + Shift + K
to knit to chosen formatAlternately, edit the YAML header at the top of the file. Options include:
output:
pdf_document: default
word_document: default
html_document: default