carpentries-incubator / lesson-R-packaging

R Packaging
https://carpentries-incubator.github.io/lesson-R-packaging/
Other
5 stars 10 forks source link

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

Open bvreede opened 1 year ago

bvreede commented 1 year ago

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

#' 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 are some useful insights:

What do you think?

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

PabRod commented 1 year ago

My two cents:

multimeric commented 6 months ago

Exporting is already explained here: https://carpentries-incubator.github.io/lesson-R-packaging/documentation.html#the-export-field.

At most I think there could be another callout that distinguishes between load_all() and install() in the Documenting section.