22 Temmuz 2014 Salı

Step by step creation of R packages (manually and automatically) [en]

Step by step creation of packages in R manually

Many are interested in step by step creation of R packages and ask how to create R packages. (If you are interested in "Step by Step Creation of Packages in R Automatically", please scroll a little bit below :) ) OK. Let the name of the package that we wanna create be "package1".
Step 0. Load the required packages:
library(roxygen2)
library(devtools)
library(digest)
Step 1. From Windows Explorer, create a folder/directory (whose name is the name of the package we are gonna create) in the R’s working directory (My R's working directory: C:/Users/erdogan/Documents/Revolution , you can find yours via R>getwd()) (Via R>list.files(), one can see the folders and files in R's working directory).
Hence, you are gonna have the following folder in Windows Explorer:
C:/Users/erdogan/Documents/Revolution/package1
Step 2. In the package1 directory, create a file with name "DESCRIPTION" and put the following content into the file DESCRIPTION (edit DESCRIPTION as you desired)(Notice that you can put the DESCRIPTION of some other package into the package1 directory; you can later change this DESCRIPTION via R’s menus “File – Open – File...- DESCRIPTION” as you desired. By the way, I strongly suggest you to use "Revolution R" GUI of R rather than "base R" GUI or "RStudio" GUI since "Revolution R" is much more developed than the two others; anyway, it is up to you not to use the Ferrari):
DESCRIPTION
Package: package1
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2014-07-18
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: GPL (>= 3)
Step 3. In the directory package1, create a folder/directory named “R”, and put the .r file of each function to this R folder (OR, if you want, you can put all the functions to the same file). For the same of argument, (assume packages has 3 functions and) create 3 files: func1.R, func2.R, func3.R whose contents are the followings:
func1.R
#' func1
#' @param x numeric
#' @export
func1 <- function(x){
    rnorm(100*x)
}
func2.R
#' func2
#' @param y numeric
#' @export
func2 <- function(y){
    rnorm(200*y)
}
func3.R
#' func3
#'
#' This is the Description section
#'
#' This is the Details section
#'
#' @param x numeric. this is multiplied by 100 to determine the length of the returned vector
#' @return a numeric vector of random deviates of length \code{100 * x}
#' @author your name
#' @seealso \code{\link{func2}}
#' @examples
#' func3(2)
#' length(func3(20))
#' @export
func3 <- function(x){
    rnorm(100*x)
}
Step 4. roxygenize, build, install, call the package you are gonna create:
roxygenize("package1")
build("package1")
install("package1")
(At this stage, carefully look at R's screen. If you get an error like “package3-package.Rd:33: All text must be in a section”, then delete the file paket3-package.Rd whose location is R's working directory\package3\man folder. And once again run the following command:
install("package1") 
This time, you will get no errors!).
library(package1)
func1(20)

Step by step creation of packages in R automatically

Let the name of the package we are gonna create be package3.
Step 0. Load the required packages:
library(roxygen2)
library(devtools)
library(digest)
Step 1. Put the .r files of the functions (funcc1.R, funcc2.R, funcc3.R) of the package you are gonna create to the R's working directory (My R working directory is: C:/Users/erdogan/Documents/Revolution, find that of you via R>getwd()). Run the following from R's prompt:
package.skeleton(name = "package3", code_files = c("funcc1.R","funcc2.R", "funcc3.R"), path = ".")
Upon executing the above command, the following folders and files exist in R's working directory,
[1] "DESCRIPTION"           "man/funcc1.Rd"        "man/funcc2.Rd"       
[4] "man/funcc3.Rd"        "man/package3-package.Rd" "NAMESPACE"            
[7] "R/funcc1.R"           "R/funcc2.R"           "R/funcc3.R"          
[10] "Read-and-delete-me"
(, which you can see via:
list.files("package3", recursive=TRUE)
).
Go to Windows Explorer and delete the file "Read-and-delete-me" (after you read if you wish). The created DESCRIPTION and NAMESPACE files at this stage are:
DESCRIPTION
Package: package3
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2014-07-18
Author: Who wrote it
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: What license is it under?
NAMESPACE
exportPattern("^[[:alpha:]]+")
Step 2. roxygenize, build, install, call the package you are gonna create:
roxygenize("package3")
(After roxygenize, the new situation of the folders and files:
NAMESPACE
# Generated by roxygen2 (4.0.1): do not edit by hand

export(funcc1)
export(funcc2)
export(funcc3)
).
build("package3")
(The following file exist in R's working directory:
[1] "C:/Users/erdogan/Documents/Revolution/package3_1.0.tar.gz"
).
install("package3")
(At this stage, carefully look at R's screen. If you get an error like “package3-package.Rd:33: All text must be in a section”, then delete the file paket3-package.Rd whose location is R's working directory\package3\man folder. And once again run the following command:
install("package3") 
This time, you will get no errors!).
library(package3)
funcc1(20) # Use the functions of the package3 happily
Erdogan CEVHER
The Ministry of Science, Industry and Technology of Turkey
e-mail: erdogancevher at gmail dot com

1 yorum:

  1. Thank you for your clear and nice explanation. It worked for me...

    YanıtlaSil