Compartmentalized | Documented | Extendible | Reproducible | Robust |
I will introduce you to RStudio and GitHub and show you how to use GitHub without having to learn Git. If you already use RStudio and Git, you can skip this week as this will be introductory level. If you have tried RStudio or Git and gotten frustrated or thought ‘I don’t have time to learn this’, this is for you. If you want to learn how to easily keep track of changes in your code, this is for you. If you have never used RStudio, this is for you.
I’m going to show you how to work with Git/GitHub with no command-line interface using GitHub Desktop. See the More Resources tab above for a nice online workshop on Git/GitHub if you want to learn more. If you want to learn to use Git with RStudio, see the same workshop or my 2020 set-up info.
Open RStudio on your computer or on RStudio Cloud. To do the latter, click this link: https://rstudio.cloud/project/2574104
When you open RStudio you will see 4 panels:
On the desktop app
On RStudio Cloud
require(graphics)
## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(lm.D9, las = 1) # Residuals, Fitted, ...
par(opar)
Keep your projects in separate folders with a uniform set of folder names. For example,
One tip is to not save your .Rdata
(your environment) when your are done for the day. Under Tools > Project Options… > General
R for Data Science is a great book to introduce you to working with data in R. Read through the following sections of the R for Data Science book and work through the examples.
Tracking your code (and project) changes. Today, I am focusing on Git for individuals not teams. If you aren’t using a change tracker (version control) already, then start just with a personal project and track only your changes. I also will not cover branches today.
.git
(so if you wanted to get rid of the history and other Git info, you could delete that folder). You have a local repo and a remote repo (on GitHub/GitLab).One-person Workflow (to get started):
Goals today:
I am going to show a workflow that is usually robust. Connecting Git on your computer and GitHub is a source of much misery, and in my experience creating the repo on GitHub first eliminates the problems. This really important for the first time you connect your computer to the remote repository server (GitHub).
New repo
Clone the GitHub repo to your computer
In the lecture, I showed this step using RStudio. I forgot that you have to tell RStudio where git.exe
is for this to work. See these instructions for doing what I did in lecture (around minute 39). In Week 2, I switched to doing this with GitHub Desktop. That is the workflow I recommend when you get started. I have updated the notes to that workflow.
Create an RStudio project from this new folder
New Project
(upper right, blue cube with R)Now you are ready to make some changes, commit them and push up to GitHub.
See these links if you want to do the cloning step from within RStudio Desktop or RStudio Cloud.
Make a change
Now add a note and push to GitHub
If you are doing this from your computer:
Commit
.If you are on RStudio Cloud:
Make sure you have RStudio Cloud set up to use Git.
Don’t Fork. That would be if you are contributing to their repo. If you just want to copy it and then adapt it for your purposes, do this.
+
in top right and click import repository
. Paste in the url and give your repo a name.Avoid making changes on GitHub directly—until you are a bit more experienced. Why? Because you’re more likely to create merge conflicts. See the week 2 material for how to fix a merge conflict.
Do not use branches (wait till you are more experienced with Git).
Do not use Git at the command line. Wait until you are much more experienced (and even then you rarely have to go to the command line).
If you use Dropbox or iCloud on multiple computers to keep folders synced up across different computers, don’t put your GitHub repos in those folders. Conflicted files are very likely if you ever work-offline.
The Git info is in the hidden folder .git
. If you need to get rid of the repository data (like history), delete that. Don’t copy that folder into another repo. The remote url is in .git/config
if you need to check it.
Pushing/Pulling to GitHub from RStudio is a hassle because there are many steps to getting the credentials (passwords) properly communicated. These steps are operating systems dependent and can especially be a hassle if you don’t have admin access on a PC.
However, you can clone repositories from GitHub and commit your changes (without pushing to GitHub) without having to do credentials. All you need to do is tell RStudio where git.exe
is on your computer.
git.exe
isgit.exe
/usr/bin/git
Just paste that in.Note AppData is a hidden folder, so click View and then click the ‘hidden files’ checkbox if you don’t see it in your Users folder.
New Project
(upper right, blue cube with R)Open RStudio Cloud and click ‘Your Workspace’ on left
Click New Project > New Project from Git Repository. Paste in the url.
In your project, click the Terminal tab (bottom left). Type ``` git config –global user.email “you@email.com”
git config –global user.name “your name” ``` I think you only do this once.
Done! Now you can commit and push from the Git tab.
For NOAA staff, GitHub can be used for publishing public projects: NOAA GitHub and a NWFSC example NWFSC Timeseries. NWFSC has a GitHub internal server if you want a repository server for your non-public projects and for internal collaboration.