Analysis Version Control Using Github in RStudio

What’s Github, and how is it useful?

Nate Thomas, NH Occupational Health Surveillance Program | Institute on Disability, University of New Hampshire

12 October 2023

Purpose

Demonstrate:

  1. the benefits of utilizing github as a file retention, collaboration, and version control toolset.

  2. a quick How-To, to set up github and utilize it within RStudio.

Disclaimer: This is only intended for familiarization purposes - github’s functionality and use far exceeds what I’ll be able to go over here. I’m not affilidated with Github in any way.

Why Use Github?

Have you ever updated a file, and regretted it - wishing you could revert back to a previous version? - I mash Ctrl+S continuously during file/document updates as a matter of course… but sometimes I wish I hadn’t.

Are your analysis files stored in a less than easy to use and unintuitive C:/users/[username]/Documents folder, like a filing cabinet without folders? Sometimes if one is just “doing this one thing quickly” and lax about keeping files rigorously organized you can really put yourself in a bind - if you have to then update it.

Do you have to pick up analysis from projects months, even years later - and have a hard time remembering where you were at in the process? Long term memory should be reserved for your important moments in life… project status shouldn’t have to be something you need to keep track of…

What does Github actually do? [www.github.com]

Github provides a free (with limitations) version control and collaboration system.

Repository (Repo) - a github project container.

Fetch - retrieve new work by others using the repo.

Merge - combine fetched work with your local work.

Pull - Fetch and Merge combined (run at the same time).

Push - Send local work to the repo.

Clone - clones a repo to your local computer.

Every github repo has a unique URL - this exact project is here: https://github.com/unh-iod/CSTE_github_demonstration

Amit Prajapati, https://medium.com/mindorks/what-is-git-commit-push-pull-log-aliases-fetch-config-clone-56bc52a3601c

A quick aside - SAS can be run in RStudio

My personal preference is to use Github within the RStudio integrated development environment (IDE). This isn’t just for people using R as their prefered scripting language.

#library(SASmarkdown)

Example dataset (class) from SAS from the sashelp library

proc means data=sashelp.class;
output out=temp;
run;

proc export data=temp
outfile="sas_data.csv"
dbms=csv
replace;
run;
                            The MEANS Procedure

 Variable    N           Mean        Std Dev        Minimum        Maximum
 -------------------------------------------------------------------------
 Age        19     13.3157895      1.4926722     11.0000000     16.0000000
 Height     19     62.3368421      5.1270752     51.3000000     72.0000000
 Weight     19    100.0263158     22.7739335     50.5000000    150.0000000
 -------------------------------------------------------------------------

Passing data from SAS into R:

read.csv("sas_data.csv") %>% data.frame() %>% flextable()

X_TYPE_

X_FREQ_

X_STAT_

Age

Height

Weight

0

19

N

19.000000

19.000000

19.00000

0

19

MIN

11.000000

51.300000

50.50000

0

19

MAX

16.000000

72.000000

150.00000

0

19

MEAN

13.315789

62.336842

100.02632

0

19

STD

1.492672

5.127075

22.77393

Where to start…?

Source: https://resources.github.com/github-and-rstudio/

  1. Download and install Git

  2. Download and install RStudio. (1.1.383 or higher).

  3. Create a remote repository on Github:

  1. Copy the unique URL from github Code tab:

https://github.com/unh-iod/CSTE_github_demonstration.git

  1. Open RStudio and create a new project New Project > Version Control > Git`:

> >

  1. Paste the unique github repo URL and select a directory in which to create the R project:

  1. This will create the folder and copy all contents of the github repo into the new local directory. This is a new repo and so only contains a .gitignore file (more on this later):

Adding files to the repo:

  1. Select Git > Commit:

  1. Stage the new file for Commit - a “Commit message” is required:

  1. The new repo branch “Main” is created:

Updating the file

This how you update files already in the repo:

  1. Save the file locally.

  2. Commit the changes:

  1. Push the changes to the remote github repo:

Reviewing changes to the project on github:

  1. Navigate to the github repo url: Example

  1. Select a file to review and select History:

  1. Select a Commit to review exactly what changed:

Inviting others to collaborate:

  1. Setting tab in the github repo select Collaborators:

  1. Adding github users is as easy as adding them here.

  2. An email invitation will be sent to the prospective collaborator - and they will be able to then accept the invitation and work on the repo.

Ignoring files in the local directory

  1. Select Git > Commit and click Ignore

  2. Enter file names for files that should only be stored locally (ignored by the repo)

  1. Select Save - this updates the .gitignore file

Please clone this repo…

https://github.com/unh-iod/CSTE_github_demonstration

If you clone this repo file which generates the entire presentation is “demonstration.Rmd”

Install any libraries required by

Click Knit…

Questions?

Resources

https://resources.github.com/github-and-rstudio/

Install RStudio: https://posit.co/download/rstudio-desktop/

Github quickstart: https://docs.github.com/en/get-started/quickstart/hello-world