carpentries-incubator / carpentries-incubator/lesson-R-packaging

distinction between `devtools::load_all()` and `devtools::install()`

Open
#101 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
5
Forks
9
PR merge metrics
No merged PRs in 30d

Description

here is an example of the distinction between `devtools::load_all()` and `devtools::install()`

```R
#' hello
#'
#' @param name name to say hello to
#' @param birth_year year of birth of `name`
#'
#' @return says hello and tells you the age
#' @export
hello <- function(name, birth_year) {
age <- calculate_age(birth_year)
print(paste0("Hello ", name, ". You are ", age, " years old."))
}

#' calculate_age
#'
#' @param birth_year year of birth
#'
#' @return age as of today. NOTE: no export
calculate_age <- function(birth_year) {
current_year <- as.numeric(format(Sys.Date(), "%Y"))
return(current_year - birth_year)
}

```

then try:
```
load_all()
calculate_age(2000) # success
hello("luke", 2000) # success
# then click `Build>Install>Clean & Install; or `devtools::install()`
hello("luke", 2000) # success
calculate_age(2000) # fails
```

I am not sure what the difference between `devtools::install()` and `Build > Install > Clean and install` is. Both install the package locally. After `Build > Install > Clean and install` , I also get a message "Restarting R session" in the console, but I it seems to be that `devtools::install()` does the same. In both cases, local variables and functions that I defined interactively are preserved after the process, which is a bit surprising to me.

To cut a long story short, in line with the documentation of `devtools` I would suggest to mostly use `devtools::load_all()`, and `install()` only "when necessary". I have no clear rules when exactly, but for instance after adding a new feature, corresponding tests and documentation, it makes sense to `install()` and see what the package looks like for the user.

[Here](https://devtools.r-lib.org/) are some useful insights:
- [load_all()](https://devtools.r-lib.org/reference/load_all.html) **simulates** installing and reloading your package, loading R code in R/, compiled shared objects in src/ and data files in data/. During development you would usually want to access all functions (even un-exported internal ones) so [load_all()](https://devtools.r-lib.org/reference/load_all.html) works as if all functions were exported in the package NAMESPACE.
- [install()](https://devtools.r-lib.org/reference/install.html) **reinstalls** the package, detaches the currently loaded version then reloads the new version with [library()](https://rdrr.io/r/base/library.html). Reloading a package is not guaranteed to work: see the documentation for [unload()](https://pkgload.r-lib.org/reference/unload.html) for caveats.

What do you think?

_Originally posted by @f-hafner in https://github.com/carpentries-incubator/lesson-R-packaging/issues/92#issuecomment-1408264508_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.