Chapter 6 Coding in a package
6.1 Edit code, run, modify, repeat
When developing a package, find whatever method is most efficient for you to develop the package quickly.
You want to be able to edit code, then run/test it, modify, rerun, modify, etc. until you are happy with it.
For some this may mean working in an IDE (RStudio) - for others this means a text editor and console - for others maybe Vim or Emacs.
Get used to the keyboard shortcuts or buttons to make this flow easy and quick.
devtools::load_all()
(or equivalent buttons/shortcuts in IDE) is your friend - though beware you still want to check your package outside of a load all context (See 9)
6.2 Code
6.2.1 Exercise
Write your own function
- Write a function
- Put it in a file in your R package
6.3 Docs
Time to document your function!
Above your function start each line with #'
. There’s many, many
options for documenting functions.
6.3.1 Exercise
Let’s do a minimal set:
- Title
- Export tag
- Parameter definitions
- Return tag
- Examples
6.4 Generate documentation
In RStudio push the document button or keyboard shortcut, or on the command line
run devtools::document()
or similar.
Notice what state your NAMESPACE
file is in before and after.
6.5 Install the package
Now install the package.
- in RStudio: Install and Restart
- command line:
devtools::install()
- restart your R session
- load your package
library(packagename)
- Can you run your function?
- if not, let’s dig in and sort it out