4.5 Using RStudio Projects
You can use devtools without using RStudio and visa versa, but it’s not as nice.
4.5.1 Benefits of RStudio Projects
- Each project is isolated and code is contained within the project.
- Easy to start up a project in a fresh instance of RStudio.
- Helps mitigate workspace overlap (e.g., function masking and overwriting accidentally same-named objects)
- Keyboard shortcuts for common code development tasks (build, load, test, document, check)
- alt-shift-k for shortcuts
4.5.2 Make an R Project for an existing package
If you already have an R package which isn’t within an R Project, you can:
- In RStudio, do File > New Project > Existing Directory.
- Call
usethis::create_package()
with the path to the pre-existing R source package. - Call
usethis::use_rstudio()
, with the active usethis project set to an existing R package. In practice, this probably means you just need to make sure working directory is inside the pre-existing package.
(using usethis::use_rstudio()
from bash fixes the problem of the other not being created)
4.5.3 R Project tidbits
A directory for an R Project will have a .Rproj
file, typically with the same name as the directory (and the package if the project is a package). It’s best case for all of these names to be the same.
This file is just a text file and you don’t need to modify it by hand. It’s possible to modify it with ‘Project Options…’ in RStudio (drop down on top right).
Launch an R Project by double clicking the .Rproj
file or from within RStudio.