3

What is the difference between the Load All command and the Build & Reload command in RStudio when working with packages?

My primary workstream now is to incorporate the current changes of the package I'm building so that I can actually use the latest functions on my machine in a different RStudio session.

matsuo_basho
  • 2,833
  • 8
  • 26
  • 47

1 Answers1

0

I've been exploring this sort of workflow and will use your question to document what I've found.

Case 1: Pkg and Code in Same Session

In RStudio choose File / New Project ... New Directory / R Package. Put scripts that define a library of functions in the R directory. Put any open code elsewhere (e.g., in a subdirectory named "scripts"). Use dev_tools::load_all (Ctrl-Shift-L) to load any objects defined in the R directory into the current session. This works for simple objects and avoids a slower Build & Reload cycle.

If you like working in this mode, recognize that any Build operation is going to make a copy of many of these files. For code this is fine but if you have large data files in a subdirectory it can be slow to build and consume a lot of storage. Consider putting them in a directory that is ignored at Build or outside the project altogether if that is more appropriate.

One nice side-effect of Build in RStudio is that you can build documentation for all the objects. We can still do this without installing the package by running roxygen2::roxygenise().

Case 2: Pkg and Code in Separate Sessions

This happens when you have functions that may be used by different projects but you need to go back and edit one of the packages.

In the first session, suppose we load a package named "greetings". Do this with RStudio as above and it will define a "hello world" function: hello(). Run Build & Reload to install this package in the local package library. Open a second session to confirm you can call this function:

greetings::hello()
#> "Hello, world!"

In the first session, modify greetings::hello() to say goodbye instead. Build & Reload (Ctrl+Shift+B) to install the changes into the package library. The second session won't pick up the change:

greetings::hello()
#> "Hello, world!"

devtools:reload() doesn't help because it exits silently if the package is not attached:

reload(inst("greetings"))
greetings::hello()
#> "Hello, world!"

However, I have found that this combination will pick up the changes in the second session:

unload(inst("greetings"))
reload(inst("greetings"))
greetings::hello()
#> "Goodbye, cruel world!"

So development in Case 2 has some limitations.

Stanwood
  • 268
  • 1
  • 10