-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathmaintaining-r.qmd
99 lines (67 loc) · 3.76 KB
/
maintaining-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
# Maintaining R
## How to upgrade an installed package to the latest version.
Sometimes you would like to upgrade a particular package to the latest
available version. Often this is because you have heard about a new feature, or
maybe you have run into a bug that may have been fixed.
### In RStudio
![RStudio has an update dialog (Packages Tab -> Update). Check packages to update them](images/rstudio-update.png)
### R terminal
Devtools has a function `update_packages()` which will upgrade a package (from
the same source) for _any_ CRAN or development package.
`devtools::update_packages("pkgname")`
In addition if the given package is _not_ already installed it will install it.
## How to upgrade all out-of-date packages
### In RStudio
![RStudio also allows you to update all packages (Packages Tab -> Update -> Select All)](images/rstudio-update-all.png)
### CRAN packages
`devtools::update_packages(TRUE)`
## How to downgrade a package
First if unsure what version -> [CRAN page](https://cran.r-project.org/web/packages/devtools/index.html) -> [pkgname archive](https://cran.r-project.org/src/contrib/Archive/devtools/)
```{.r}
devtools::install_version("devtools", "1.13.3")
```
## How to transfer your library when updating R
Often you will not need to do anything when updating R. For 'patch' R versions,
the 'z' in 'x.y.z' the R core developers ensure package compatibility across
versions. So if you are updating from R 3.5.0 to R 3.5.1 you can use the same
packages you are currently using.
For 'minor' version changes, the 'y' in 'x.y.z' the package interface _can_
change, so packages need to be re-installed.
::: {.callout-warning}
You may see some suggestions that you can just copy your packages even when the
'minor' version changes. **DO NOT DO THIS**. While it may work some (even most)
of the time, R-core does not guarantee compatibility between these versions and
things could break (even break silently).
:::
I suggest you keep R's base and recommended packages separate from the other packages you install from CRAN or elsewhere.
This makes it easy to re-install R if needed, without touching your CRAN packages.
You also want to make sure the package library is specific to the minor version of R.
`R_LIBS_USER` is actually set by default to this scheme:
* Windows, R < 4.2: `~/R/win-library/x.y`
* Windows, R >= 4.2: `~/AppData/Local/R/win-library/x.y`
* macOS: `~/Library/R/x.y/library`
Note, however, that this directory does not necessarily exist and will not necessarily be created automatically.
Therefore, to adopt this lifestyle next time you upgrade R, make sure that this directory exists before you start to re-install your add-on packages (`fs::dir_create(Sys.getenv("R_LIBS_USER"))`).
You can also alternatively set `R_LIBS_USER` to a different path; but make sure
to include the `%v` wildcard. e.g. `~/R/library/%v`. The `%v` is
automatically expanded to the major and minor version of R, so with R 3.5.1
this path becomes `~/R/library/3.5`. See [Renviron](#renviron) for how
to edit your `.Renviron` file.
::: {.callout-warning}
Paths in `R_LIBS_USER` are only used if the directories they specify actually
exist. So in addition to adding `R_LIBS_USER` to your `.Renviron` you need to
create the directory as well.
:::
Once this is setup, the process for transferring your package library becomes.
(assumes `R_LIBS_USER` is set to `~/Library/R/3.5/library`).
```{.r}
# Install new version of R (lets say 3.5.0 in this example)
# Create a new directory for the version of R
fs::dir_create("~/Library/R/3.5/library")
# Re-start R so the .libPaths are updated
# Lookup what packages were in your old package library
pkgs <- fs::path_file(fs::dir_ls("~/Library/R/3.4/library"))
# Filter these packages as needed
# Install the packages in the new version
install.packages(pkgs)
```