Statistics
Create your first R Package
- By
- On 09/05/2020
- Comments (0)
- In R programing
In you RStudio run the command:
>devtools::create("mymhipackage")
After that, a new RStudio window will show up: It will have the following structure
Under the R folder we are going to create our file functions for instance: we want to create a function that checks wether a list of words contains at least one specific word:
let's call this function regex_check: for that we create the file: regex_check.R:
#' Regex check
#'
#' This function takes as parameter a target list of strings and a pattern
#' string and checks if one of the target strings contains the pattern
#'
#' @param target_list_string list of words
#' @param pattern
#' @return true or false
#' @export
regex_check <- function(target_list_string,pattern){
  for(k in 1:length(target_list_string))
  {
    if(grepl(pattern,target_list_string[k])[1]))
    {
      return(TRUE)
    }
  }
  return(FALSE)
}
 
Then we load these libraries:
> library(usethis)
> library(devtools)
> library(roxygen2) 
Next, we run the document method, which will generate the associated documentation under the folder man:
>document()
the new folder Structure of the packge will look like that:
After all, if we want to test our package we have to load and install it:
> devtools::load_all()
> devtools::install()
That's it ;-) we can go back to the main project:
and run :
>library(mymhipackage) # in order to load your first library
Finally last tip: if you want to unload the library because you want to modify something in it, then run the following comand:
>detach("package:mymhipackage", unload=TRUE)
 
                
                                            
                        

