-
Notifications
You must be signed in to change notification settings - Fork 3
/
set-up.Rmd
195 lines (133 loc) · 9.26 KB
/
set-up.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: "Set-up"
output:
html_document:
toc: true
include:
after_body: footer.html
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Everything you'll be using is free. You will need a GitHub (free) account. Sign up here [GitHub](https://github.com/).
## For weeks 1-4, you only need a GitHub account and GitHub Desktop.
* Sign up for [GitHub](https://github.com/)
* Install [GitHub Desktop](https://desktop.github.com/)
* Open GitHub Desktop on your computer, go to Preferences, click Accounts and login to your GitHub account.
* Still in Preferences, click Git and enter your name and email.
## For weeks 5-8, you need R, RStudio and GH Desktop (or RStudio Cloud)
### Installing locally
* Install [R](https://cran.r-project.org/)
* Install [RStudio](https://www.rstudio.com/products/rstudio/download/)
* Install [GitHub Desktop](https://desktop.github.com/)
**Already have R and RStudio installed?** See [update R](#updateR) for instructions on updating R to the most recent version. This is not required for the workshop, but it is good to keep R and RStudio updated.
### Using RStudio Cloud
If you use RStudio Cloud you only need a browser. No installing of programs. Sign up for an [RStudio Cloud](https://rstudio.cloud/) account.
## Git in RStudio Cloud
In the class, I am going to use GitHub Desktop, but if you want to use Git from within RStudio Cloud or RStudio Desktop then read these two sections.
1. Tell RStudio Cloud you want use Git. Open your project then Click Tools > Global Options > Git/SVN (left nav) and check 'Enable version control for RStudio projects'
![Tools tab is on your project page](images/RStudio-Cloud-Tools.png){25%}
2. Click the Terminal tab in the lower left panel or click the blue cog below the Git tab in the upper right panel.
![Terminal location](images/RStudio-Cloud-Terminal.png){25%}
3. Type in this with your info
```
git config --global user.email "<your email>"
git config --global user.name "<your name>"
```
## Git from RStudio Desktop
There are a few steps involved. Go slow an deliberately. Nothing is hard.
### Tell RStudio where `git.exe` is
1. Open RStudio
2. Click Tools > Global Options
3. Click Git/SVN on the left
4. Click the little check box at the top
5. In the Git executable box, paste in the location of `git.exe`
*Where is git on my computer?* Open a terminal and type `which git`. That will show you where it is. Windows, if your terminal doesn't recognize the `which` command, try `dir "git.exe” /s` from your user directory. *It still can't find it.* Git is probably not installed.
* On a Mac? It is at `/usr/bin/git` Just paste that in.
* On a PC? Look (by opening a finder window) in the following places. Try the GitHub Desktop one first.
* C:/Program Files/Git/bin/git.exe
* C:/Users/your.username/AppData/Local/Programs/Git/bin/git.exe
* C:/Users/your.username/AppData/Local/GitHubDesktop/app-2.8.3/resources/app/git/cmd/git.exe
6. Restart R. From within RStudio go to Session > Restart R...
### Tell RStudio your GitHub username and password
*New in 2020!*
In the newest RStudio, there is browser authentication so you don't need to deal with personal access tokens. When you try to push the first time, you should see a pop-up window that will allow you to log into GitHub to authenticate. *I don't see this pop-up.* Before giving up, try closing and reopening RStudio. Also you need an updated version of RStudio for this to work.
*Method using Personal Access Tokens*
Go to [HappyGitwR](https://happygitwithr.com/credential-caching.html) to read all about how to pass your credentials (e.g. username and password) to GitHub so you can interact (read push/pull changes) with your GitHub reponsitories.
**Here is the simple version**
Install these two packages:
```
install.packages("usethis")
install.packages("gitcreds")
```
Run this code to generate your Personal Access Token (PAT) for your GitHub account. It doesn't matter what folder you are in when you run this. Just go to the R command line in RStudio within any folder or project. For most people, you can just accept the default scope that **usethis** selects for you. If you are not one of those people, then you will know what extra boxes to check. Everyone else, just accept the default scopes. Next choose a time limit for your PAT. You can choose "forever" but you might not want to do that. I set up mine for 12 months.
```
usethis::create_github_token()
```
SAVE the token, that long string of letters!! **You need it for the next step.**
Next set up R so it knows where this PAT is:
```
gitcreds_set()
```
If you have an old PAT that you need to replace, it will give you an option for that.
Now pushing and pulling to GitHub should work fine from R (and RStudio).
If you need to make another PAT, because your PAT expired or you messed up the scope, then go here
https://github.com/settings/tokens
to delete the old one before setting up a new one.
## Git in the shell (works for RStudio and Visual Studio Code)
Before you start, get a Personal Access Token (PAT) from GitHub. You'll need this as you GitHub password. GitHub > Profile (far right drop down) > Settings > Developer settings (far right bottom) > Personal Access Token. Generate a token with write access to repo and copy the PAT (very long).
[Show me how](https://youtu.be/tkioJhF_gO8)
Now that you have your PAT, you are ready to set-up Git from the shell/terminal.
1. Open a terminal window
2. Tell Git who you are. Type
```
git config --global user.name Your Name
git config --global user.email [email protected]
```
3. Tell Git how to get your username and password for GitHub
Type `git config --get credential.helper`
If you are on a PC, you should see `manager` and if on a mac, `osxkeychain`
If not type, Mac: `git config --global credential.helper osxkeychain` Windows: `git config --global credential.helper manager`
4. The first time you do a push of a commit, you will be asked for your GitHub user name and password. Do not enter your password. Enter a Personal Access Token (PAT).
Note. Instead of the PC or Mac specific credential managers, you can use the [GitHub credential manager](https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git)
[Show me how](https://youtu.be/4xrp_AZuyEo)
## Updating R/RStudio {#updateR}
### IT installs everything on my computer
Talk to them. They have their own procedure for this and they install/update R and RStudio all the time (probably).
### I can install stuff
**R**
Install the latest R from CRAN. When you restart RStudio, it will use the updated version of R.
**Windows users** can try using the package `installr` (only for Windows). This [installr tutorial](https://www.r-statistics.com/2015/06/a-step-by-step-screenshots-tutorial-for-upgrading-r-on-windows/) is very helpful. Or download from [CRAN](https://cloud.r-project.org).
**Mac users**
1. Go to https://cloud.r-project.org/bin/macosx/
2. Find the latest pkg file for your macOS and double-click on that.
3. Double-click on the downloaded file to install. Follow instructions. You should be able to click "Next" to all dialogs.
4. I delete old R versions from my computer after I am happy with the upgrade.
**RStudio**
Open RStudio. Go to "Help" tab. Click "Check for Updates"
**Getting your packages to the new R version**
If you want to automatically re-install these you can follow these instructions. I do not do this, because I tend to accumulate so many packages just when I am trying out stuff and I use my R updates to clean-up. I have a small list of packages that I install on a fresh R install. But if you want to just install all the packages you have installed, here is how you could do that.
```
tmp <- installed.packages()
installedpkgs <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
save(installedpkgs, file="installed_old.rda")
```
Once you have the new R installed, open RStudio (or R) and navigate to where you have that `installed_old.rda` file.
```
load("installed_old.rda")
tmp <- installed.packages()
installedpkgs.new <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
missing <- setdiff(installedpkgs, installedpkgs.new)
if(length(missing)!=0) install.packages(missing)
update.packages()
```
Does R say, "package not available" and you know it is? Try a different mirror.
So what do I do? I run this code
```
install.packages("tidyverse")
install.packages(c("knitr", "rmarkdown", "markdown"))
install.packages(c("kableExtra", "gridExtra", "raster", "sp"))
```
I say "no" to install from binary (unless for some reason I need the very latest version, which I never do). That gets me 75% of the way and then I install the other stuff as I need it. *Notes* 1) I don't write scripts with `library()` buried deep in the script. Those calls always go at the top so they break early if I am running something. 2) I tend to bundle my code into packages with a DESCRIPTION file with all the dependencies and suggests. When I install that with `install.packages(..., dependencies=TRUE)` it will install all the packages required or used in the vignettes and examples. 3) I prefer to use `::` when using functions from other packages, like `forecast::forecast()`. Exception is **ggplot2** stuff. Otherwise I spend forever trying to figure what package a function came from.
**Updating packages**
To update packages, Go to "Tools" tab and then "Check for Package Updates".