Setting up Git/GitHub to work with R/RStudio can be challenging.

Here's a thread to walk you through the process step by step.

This thread also lives in blog post form. #rstats

Preamble: the best overall resource, and one I rely on extensively, is Happy Git with R by @JennyBryan and @jimhester_.

If you have additional questions about Git/GitHub + R/RStudio, this should be your first destination.

https://t.co/C7JHg8Fswb
The first step to set things up is to install Git.

Chapter 6 of Happy Git with R lays out the process for Mac, Windows, and Linux users.

https://t.co/AhzoudgIjE
I’m on a Mac so Git came pre-installed on my computer. I was able to verify that I had Git installed using the terminal in RStudio.
The next step is to configure Git.

This is covered in Chapter 7 of Happy Git with R , though I show what I believe to be a slightly easier process using the edit_git_config() function from the {usethis} package.
Now that you’ve installed and configured Git, you can use it locally.

The use_git() function will add a Git repository (often referred to as a “repo”) to an existing RStudio project.
Now that my RStudio project has an associated Git repository, I’ll see an extra tab on the top right: the Git tab.

From here, I can see the entire history of changes to my code over time (not many yet!).
Git doesn’t automatically track changes the way a tool like Google Docs does.

Instead, you have to tell Git: I made changes and I want you to keep a record of them.

Telling Git this is called making a commit and you can do it from within RStudio.
Each commit has a commit message, which is helpful because, when you look at your code history, you see what you did at each point in time (i.e. at each commit).

RStudio has a built-in tool to view your code history.
The process so far has enabled us to use Git locally.

But what if we want to connect to GitHub? How do we do that?
The first step is to sign up for a (free) GitHub account.

https://t.co/dqfYpbzTz7
Now you’ll need to enable RStudio to talk to GitHub. The process for doing so has recently changed (this is where I see the largest major difference from Happy Git with R).

The best way to connect RStudio and GitHub is using your username and a Personal Access Token (PAT).
To generate a personal access token, use the create_github_token() function from {usethis}. This will take you to the appropriate page on the GitHub website, where you’ll give your token a name and copy it (don’t lose it because it will never appear again!).
Now that you’ve created a Personal Access Token, we need to store it so that RStudio can access it and know to connect to your GitHub account.
The gitcreds_set() function from the {gitcreds} package will help you here.

You’ll enter your GitHub username and the Personal Access Token as your password (NOT your GitHub password, as I initially thought).

Once you’ve done all of this, you have connected RStudio to GitHub!
Now that we’ve connected RStudio and GitHub, let’s discuss how to make the two work together.

The basic idea is that you’ll set up projects you create in RStudio with associated GitHub repositories. Each RStudio project lives in a single GitHub repo.
How do we connect an RStudio project to a GitHub repo? Happy Git with R goes over three strategies (https://t.co/IkH3OVr7N9).

I’ll demonstrate two of them.
Sometimes you already have a project locally and you want to get it on GitHub. To do this, you’ll use the use_git() function from {usethis}, as we did above.
Then, you can use the use_github() function, which will create a GitHub repo and connect it to your RStudio project.
The most straightforward way to use RStudio and GitHub together is to create a repo on GitHub first.

Create the repo, then when you start a new project in RStudio, use the version control option, enter your repo URL, and you’re good to go.
You're all set now!

Reminder that there is a blog post form on this thread.

https://t.co/DwQ7ibM3pd
The blog post covers a bit more of the what and why of using a Git/GitHub workflow.

But I'm far from a Git/GitHub expert. To learn from those who know more, here are a few resources.
If you’re looking to learn more and I haven’t yet beat into you the idea that you should check out Happy Git with R, let me try it once more. It’s the best book to help guide you as you go deeper with using Git/GitHub in your R work.

https://t.co/m3lIngyihs
You also might check out Collaborating with git and GitHub by @datalorax_, @_bcullen, and @HOuafaa.

This chapter from the course materials for Social Data Science with R at the University of Oregon covers the basics of Git and GitHub.

https://t.co/zq94ShFEYQ
There's also materials by @juliesquid and @allison_horst from their 2019 rstudio::conf R for Excel Users workshop, which cover “version control and ... streamlines working with our most important collaborator: Future You.”

https://t.co/TR7hPjd9Vk

More from Tech

You May Also Like

1/“What would need to be true for you to….X”

Why is this the most powerful question you can ask when attempting to reach an agreement with another human being or organization?

A thread, co-written by @deanmbrody:


2/ First, “X” could be lots of things. Examples: What would need to be true for you to

- “Feel it's in our best interest for me to be CMO"
- “Feel that we’re in a good place as a company”
- “Feel that we’re on the same page”
- “Feel that we both got what we wanted from this deal

3/ Normally, we aren’t that direct. Example from startup/VC land:

Founders leave VC meetings thinking that every VC will invest, but they rarely do.

Worse over, the founders don’t know what they need to do in order to be fundable.

4/ So why should you ask the magic Q?

To get clarity.

You want to know where you stand, and what it takes to get what you want in a way that also gets them what they want.

It also holds them (mentally) accountable once the thing they need becomes true.

5/ Staying in the context of soliciting investors, the question is “what would need to be true for you to want to invest (or partner with us on this journey, etc)?”

Multiple responses to this question are likely to deliver a positive result.
The UN just voted to condemn Israel 9 times, and the rest of the world 0.

View the resolutions and voting results here:

The resolution titled "The occupied Syrian Golan," which condemns Israel for "repressive measures" against Syrian citizens in the Golan Heights, was adopted by a vote of 151 - 2 - 14.

Israel and the U.S. voted 'No'
https://t.co/HoO7oz0dwr


The resolution titled "Israeli practices affecting the human rights of the Palestinian people..." was adopted by a vote of 153 - 6 - 9.

Australia, Canada, Israel, Marshall Islands, Micronesia, and the U.S. voted 'No' https://t.co/1Ntpi7Vqab


The resolution titled "Israeli settlements in the Occupied Palestinian Territory, including East Jerusalem, and the occupied Syrian Golan" was adopted by a vote of 153 – 5 – 10.

Canada, Israel, Marshall Islands, Micronesia, and the U.S. voted 'No'
https://t.co/REumYgyRuF


The resolution titled "Applicability of the Geneva Convention... to the
Occupied Palestinian Territory..." was adopted by a vote of 154 - 5 - 8.

Canada, Israel, Marshall Islands, Micronesia, and the U.S. voted 'No'
https://t.co/xDAeS9K1kW