Git and GitHub

Eric C. Anderson

1/23/2020

Rmarkdown Leftovers I: RMarkdown documents vs R Notebooks

Typical output formats

Most output formats from RMarkdown, like:

use the knitr model for evaluating code and “knitting” the document to produce output.

Basically: if you want to see the HTML (or PDF or WORD) output, you need to hit the “knit” button which will:

  1. evaluate all the code (in a completely fresh, new R Session)
  2. compile up the results with the markdown to make the document.

This can be exceedingly slow if you just made a small tweak to the text or formatting and you want to see the result.

Enter the html_notebook output format, or R Notebook…

R Notebook (html_notebook) output

YAML header:

---
title: "A Title"
output: html_notebook
---

instead of

---
title: "A Title"
output: html_document
---

Rmarkdown Leftovers II: Code Blocks, for Evaluation or Show?

Evaluated Code

If if put this in your document:

```{r}
x <- 1:10
```
```{bash}
PS1='[\W]--% '
```

Then the code within the blocks get evaluated. In the first case it is evaluated in R, and in the second it get evaluated in a bash shell.

Non-evaluated code (code for show only!)

But what if you just want to document a snippet of code, but don’t actually want to to run it in your RMarkdown document (I do this all the time when documenting short scripts I wrote quickly on a cluster or when just noodling at the command line. There are two options:

eval=FALSE option

Just add eval=FALSE as an option to the code block:

```{r, eval=FALSE}
x <- 1:10
```
```{bash, eval=FALSE}
PS1='[\W]--% '
```

Advantages

Disadvantages

The “no-curly-braces” option

Write it as a regular markdown “non-evaluated” chunk.

```r
x <- 1:10
```
```bash
PS1='[\W]--% '
```

Note that you still tell it what language is used, immediately after the back-tick. That tells it how to do the syntax highlighting.

Advantages

All your cheat sheets in one document

RStudio has all (or most) of its 2019 Cheat Sheets in one spot here: https://rstudio.com/wp-content/uploads/2019/01/Cheatsheets_2019.pdf

Great for a mind-blowing introduction to a lot of the new stuff coming from RStudio.

Git Basics. Overview

Goals for Today:

An overview of Version Control Systems (VCS)

A typical VCS for a non-computer programmer

The Git model — Distributed Version Control

Git versus GitHub

Is everything on GitHub public?

Using git through RStudio

The status/staging panel

The Diff window

Making a Commit

The History window

Go for it everyone!

How does git store and keep track of things

What is inside of the .git directory?

We can use R to list the files. It is going to look something like this:

  [1] "#MERGE_MSG#"                                       "COMMIT_EDITMSG"                                   
  [3] "COMMIT_EDITMSG~"                                   "config"                                           
  [5] "description"                                       "FETCH_HEAD"                                       
  [7] "HEAD"                                              "hooks/applypatch-msg.sample"                      
  [9] "hooks/commit-msg.sample"                           "hooks/post-update.sample"                         
 [11] "hooks/pre-applypatch.sample"                       "hooks/pre-commit.sample"                          
 [13] "hooks/pre-push.sample"                             "hooks/pre-rebase.sample"                          
 [15] "hooks/prepare-commit-msg.sample"                   "hooks/update.sample"                              
 [17] "index"                                             "info/exclude"                                     
 [19] "logs/HEAD"                                         "logs/refs/heads/gh-pages"                         
 [21] "logs/refs/heads/master"                            "logs/refs/remotes/origin/gh-pages"                
 [23] "logs/refs/remotes/origin/master"                   "objects/00/906f99e192ff64b4e9e9a0e5745b0a4f841cbd"
 [25] "objects/01/ab18d4ce04fb06532bb06ed579218fef89d478" "objects/02/74554e0b574b9beb2144f26ad3925830056870"
 [27] "objects/03/2d224bf78798e8b9765af6d8768ade14694a9d" "objects/04/03d552ab37b0bcaeebed0ac3068d669261c456"
 [29] "objects/04/4a12f8ccc12a4a5ba84ab2bf5a1ae751feea6f" "objects/04/9ec3065bb0434ded671fa83af5ade803bc11a1"
 [31] "objects/04/ea8efb1367727b081dea87e63818be0a4d02f0" "objects/05/b22ecc373d5058e36d7ca773a4475c46daef77"
 [33] "objects/07/8831b46c9b63e8c2d50b79304ed05de9274c28" "objects/07/b57af2a0cbd0545a6cd3e93f10cc5d768e42ba"
 [35] "objects/08/674e6e4d534b3424e2629510d20bb6d1b0be94" "objects/08/8b282d5b978dc1ff6eef3871d3fb3a9256246a"
 [37] "objects/09/565dcd10d7adc0551783b443e8fd71486b3997" "objects/09/6828a0cfb96f30d6e99cfc04a5c1686b9e318c"
 [39] "objects/0a/30fe678abc342c58daab0ad42163b371babda0" "objects/0a/b71109dae6e5711755feddfb06b81b13766496"
 [41] "objects/0b/442fdfc183783537985c17151ae3483fa00cf6" "objects/0b/c0451fd0e7081a7db05fdd38b12870bdcabd13"
 [43] "objects/0c/0f7cf8c73d901795dad4bd5f504c53c3bf2093" "objects/0d/14a7a2a19ffc3b9820f011e3270c965a5fefce"
 [45] "objects/0e/35cfb4d55e52d27083b8d2eccab9296b920d76" "objects/0e/7bba5882077a8b00a76d3eabe6b23cadc658e6"
 [47] "objects/0e/8abf4cc0885a727ee2459fdbb272828e267cc4" "objects/0f/1f3f7be7787d5d44dc1155f3b7a44eddc9f0be"
 [49] "objects/10/54d2e7a9baf61618521c522b15db40855b3431" "objects/11/7d874e1616500b5fba51b9f0ee1e8d0fbe1dc2"
 [51] "objects/11/c33cc1d5c8de7c7cbf7257b7d32f7ca3d458ef" "objects/14/1ccea514e106e20eef47b791a23e036d1fa1d2"
 [53] "objects/15/cc3a6f15dadb3446ad0af34a3ecde8d81d65f9" "objects/15/ddaf45bf00c3ef2d8f499ebd6dc3a86bf9c3ab"
 [55] "objects/16/0c9386dfa9707d81fbbbcc52f0c7638703f9a9" "objects/16/8ee93b6a4612dbd76bc06a49460df9f9f6c41b"

Yikes!

How does git know a file has changed?

What should I keep under version control?

This is a Wildy Important Topic

How can I make git ignore certain files?

It is best to set git up so that you don’t “accidentally” commit a whole bunch of files that you don’t want to

Pushing and Pulling With GitHub

Creating a Repository on GitHub and the initial push

When you have an RStudio project under git version control on your laptop or desktop computer, creating a remote repository on GitHub is quite easy. A few steps:

  1. Upper right corner: “create new” button (a “+” with a little triangle.) Choose “New Repository”
  2. Give it a name. It makes most sense to name it the same as the RStudio project you want to push up there. So, for example, if my project file was boing.Rproj, I would name the repository boing.
  3. Add a 5 or 6 word description if you want.
  4. Choose public or private
  5. DO NOT choose to “Initialize this repository with a README”. You likely already have a README. Initializing the repository with one will create headaches.
  6. Also, don’t add a .gitignore or a license (select “none”, which should be the default, for both of those)
  7. Click the green “Create Repository” button

That will take you to another screen. In the middle find the code box below the heading, …or push an existing repository from the command line.

  1. Copy that two lines of code from you web browser. It will look something like this: git remote add origin https://github.com/eriqande/boing.git git push -u origin master but it will be specific to the repository you just made, so the URL and name of the repo will be different than what you see above. Note, you can copy the lines by clicking the “copy this text” icon on the right side of the page.
  2. Go to RStudio, in the project that you want to push to GitHub, and open a terminal window (Tools -> Terminal -> New Terminal). That will give you a terminal window with a working directory set to the project directory. Paste the commands you copied into that terminal window and hit return.
  3. It might ask you for your GitHub username and password.

Voila!

Subsequent pushes

Once you have pushed the repo up there. Try making some changes on your laptop, committing them, and then hitting the “Push” button on the git panel…

A first look at Forking and Pull Requests

Getting Ready for Next Week