I made a package in R using these instructions. I use RStudio and I'd like to add a new function to the package.
Do I just put the functions into an R script and drag it into the folder in the package called R? If I do that, do I need to change the contents of the folder named man?
Say you have written a new function called my_function
my_function <- function(){
  print("New function!")
}
You need to document it in the same R file. So your complete R file would look something like this
#' my_function
#' 
#' A function to print the words "New function!"
#'
#' @return A character vector
#' @export
#'
#' @examples
#' my_function()
my_function <- function(){
  print("New function!")
}
Now save this file in your R/ directory in the package
Run devtools::document() and that will update your man/ directory.
You have now added a new function to your package
In my opinion, the book R Packages is the best guide. You can read it for free at that link
Or using utils::prompt():
my_function <- function(){
  print("New function!")
}
prompt(my_function)
... which creates a prefilled my_function.Rd, which can be moved into the corresponding* man/ directory and completed.
* If you still need the structure of an R package, have a look at utils::package.skeleton()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With