How do I insert a new function into my R package?

时间:2019-01-18 18:12:18

标签: r

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?

1 个答案:

答案 0 :(得分:1)

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