forked from PsyTeachR/ads-v2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app-updating-r.qmd
111 lines (67 loc) · 5.98 KB
/
app-updating-r.qmd
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
# Updating R, RStudio, and packages {#sec-updating-r}
From time-to-time, updated version of R, RStudio, and the packages you use (e.g., ggplot) will become available. Remember that each of these are separate, so they each have a different process and come with different considerations. We recommend updating to the latest version of all three at the start of each academic year.
## Updating RStudio
RStudio is the easiest component to update. Typically, updates to RStudio won't affect your code, instead they add in new features, like spell-check or upgrades to what RStudio can do. There's usually very little downside to updating RStudio and it's easy to do.
Click <if>Help > Check for updates</if>
```{r img-updaterstudio, echo=FALSE, fig.cap="Updating RStudio"}
knitr::include_graphics("images/appx/update_rstudio.jpg")
```
If an update is available, it will prompt you to download it and you can install it as usual.
## Updating R
Finally, you may also wish to update R itself. The key thing to be aware of is that when you update R, if you just download the latest version from the website, you will lose all your packages.
### Windows
The easiest way to update R on Windows and not cause yourself a huge headache is to use the <pkg>installr</pkg> package. When you use the `updateR()` function, a series of dialogue boxes will appear. These should be fairly self-explanatory but there is a [full step-by-step guide available](https://www.r-statistics.com/2015/06/a-step-by-step-screenshots-tutorial-for-upgrading-r-on-windows/) for how to use `installr`, the important bit is to select "Yes" when it asked if you would like to copy your packages from the older version of R.
```{r installr, eval = FALSE}
# Install the installr package
install.packages("installr")
# Run the update function
installR::updateR()
```
### Mac
For a Mac, you can use the [<pkg>updateR</pkg>](https://github.com/AndreaCirilloAC/updateR) package. You'll need to install this from GitHub. You will be asked to type your system password (that you use to log into your computer) in the console pane. If relevant, it will ask you if you want to restore your packages for a new major version.
```{r, eval = FALSE}
# install from github
devtools::install_github("AndreaCirilloAC/updateR")
# update your R version, you will need your system password
updateR::updateR()
```
## Updating packages
Package developers will occasionally release updates to their packages. This is typically to add in new functions to the package, or to fix or amend existing functions. **Be aware that some package updates may cause your previous code to stop working**. This does not tend to happen with minor updates to packages, but occasionally with major updates, you can have serious issues if the developer has made fundamental changes to how the code works. For this reason, we recommend updating all your packages once at the beginning of each academic year (or semester) - don't do it before an assessment or deadline just in case!
To update an individual package, the easiest way is to use the `install.packages()` function, as this always installs the most recent version of the package.
```{r update_one, eval = FALSE}
install.packages("tidyverse")
```
To update multiple packages, or indeed all packages, RStudio provides helpful tools. Click <if>Tools > Check for Package Updates</if>. A dialogue box will appear and you can select the packages you wish to update. Be aware that if you select all packages, this may take some time and you will be unable to use R whilst the process completes.
```{r img-updateall, echo=FALSE, fig.cap="Updating packages with RStudio"}
knitr::include_graphics("images/appx/update_packages.png")
```
## Troubleshooting {#sec-package-install-troubleshooting}
Occasionally, you might have a few problem packages that seemingly refuse to update. For me, `rlang` and `vctrs` cause me no end of trouble. These aren't packages that you will likely every explicitly load, but they're required beneath the surface for R to do things like knit your Markdown files etc.
### Non-zero exit status
If you try to update a package and get an error message that says something like `Warning in install.packages : installation of package ‘vctrs’ had non-zero exit status` or perhaps `Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : namespace 'rlang' 0.4.9 is being loaded, but >= 0.4.10 is required` one solution I have found is to manually uninstall the package, restart R, and then install the package new, rather than trying to update an existing version. The `installr` package also has a useful function for uninstalling packages.
```{r problem_packages, eval = FALSE}
# Load installr
library(installr)
# Uninstall the problem package
uninstall.packages("package_name")
# Then restart R using session - restart R
# Then install the package fresh
install.packages("package")
```
### Cannot open file
You may get the following error after trying to install any packages at all:
> Error in install packages : Cannot open file 'C:/.....': Permission denied
This usually indicates a permissions problem with writing to the default library (the folder that packages are kept in). Sometimes this means that you need to install R and RStudio as administrator or run it as administrator.
One other fix may be to change the library location using the following code (check in "C:/Program Files/R" for what version you should have instead of "R-3.5.2"):
```{r, eval = FALSE}
# change the library path
.libPaths(c("C:/Program Files/R/R-3.5.2/library"))
```
If that works and you can install packages, set this library path permanently:
1. Install the <pkg>usethis</pkg> package
2. Run `usethis::edit_r_profile()` in the console; it will open up a blank file
3. Paste into the file (your version of): `.libPaths(c("C:/Program Files/R/R-3.5.2/library"))`
4. Save and close the file
5. Restart R for changes to take effect
The code in your .Rprofile will now run every time you start up R.
As always, if you're having issues, please ask on Teams or come to office hours.