Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 983
Sebastian Avatar asked Oct 28 '25 17:10

Sebastian


2 Answers

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

like image 92
Conor Neilson Avatar answered Oct 30 '25 07:10

Conor Neilson


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()

like image 41
bathyscapher Avatar answered Oct 30 '25 06:10

bathyscapher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!