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

21 Temmuz 2014 Pazartesi

R da (Elle ve Otomatik) Paket Oluşturumu [tr]

ELLE PAKET YAPIMI
Oluşturulacak paketin adı, paket1 olsun.
0. Gerekli paketleri yükle:
library(roxygen2)
library(devtools)
library(digest)
1. R’ın çalışma dizininde (C:/Users/erdogan/Documents/Revolution) paket1 adlı dizin (klasör) oluştur (R>list.files() ile çalışma dizininde oluşturulan dosya ve klasörler görülebilir).
2. paket1 dizininde, DESCRIPTION adlı bir dosya oluştur ve aşağıdaki içeriği DESCRIPTION’ın içine koy (dilediğin gibi DESCRIPTION’ı düzenle)(Başka bir paketin DESCRIPTION’ınını da koyup, R’da “Dosya – Aç – Dosya...- DESCRIPTION” ile koyduğun bu DESCRIPTION’ı istediğin gibi değiştirebilirsin):
DESCRIPTION
Package: paket1
Type: Package
Title: What the package does (short line)
Version: 0.0.1
Date: 2012-11-12
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. paket1 dizininde, “R” adlı dizin oluştur ve her işlevin .r dosyasını ekle (veya istersen tüm işlevlerini aynı dosyaya koyabilirsin). Üç dosya oluşturdum: islev1.R, islev2.R, islev3.R
islev1.R
#' islev1
#' @param x numeric
#' @export
islev1 <- function(x){
    rnorm(100*x)
}
islev2.R
#' islev2
#' @param y numeric
#' @export
islev2 <- function(y){
    rnorm(200*y)
}
islev3.R
#' islev3
#'
#' 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{fun2}}
#' @examples
#' islev3(2)
#' length(fun3(20))
#' @export
islev3 <- function(x){
    rnorm(100*x)
}
4. Oluşturulacak paket, roxygenize et:
roxygenize("paket1")
Updating collate directive in  /home/garrett/tmp/package1/DESCRIPTION
Updating namespace directives
Writing islev1.Rd
Writing islev2.Rd
Writing islev3.Rd
build("paket1")
install("paket1")
library(paket1)
islev1(20)
------------------------------------------------------------------------------------------------------
OTOMATİK PAKET YAPIMI
Oluşturulacak paketin adı, paket3 olsun.
0. Gerekli paketleri yükle:
library(roxygen2)
library(devtools)
library(digest)
1. Pakette yer alacak işlevlerin .R dosyalarını (islevv1.R, islevv2.R, islevv3.R) R’ın çalışma dizinine (C:/Users/erdogan/Documents/Revolution) koy. 
package.skeleton(name = "paket3", code_files = c("islevv1.R","islevv2.R", "islevv3.R"), path = ".")
komutu sonrasında R’nin çalışma dizininde aşağıdaki klasörler ve dosyalar oluşur:
> list.files("paket3", recursive=TRUE)
 [1] "DESCRIPTION"           "man/islevv1.Rd"        "man/islevv2.Rd"      
 [4] "man/islevv3.Rd"        "man/paket3-package.Rd" "NAMESPACE"           
 [7] "R/islevv1.R"           "R/islevv2.R"           "R/islevv3.R"         
[10] "Read-and-delete-me"

Read-and-delete-me dosyasını Windows Explorer’a gidip sil.

DESCRIPTION
Package: paket3
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:]]+")

2. Oluşturulacak paketi, roxygenize et:
roxygenize("paket3")
Updating collate directive in  /home/garrett/tmp/package1/DESCRIPTION
Updating namespace directives
Writing islev1.Rd
Writing islev2.Rd
Writing islev3.Rd

roxygenize sonrasında: klasörler ve dosyaların yeni durumu:
NAMESPACE
# Generated by roxygen2 (4.0.1): do not edit by hand

export(islevv1)
export(islevv2)
export(islevv3)

build("paket3")
[1] "C:/Users/erdogan/Documents/Revolution/paket3_1.0.tar.gz"

install("paket3")
Bu aşamada, “paket3-package.Rd:33: All text must be in a section” hatası alırsan, çalışma dizinindeki paket3 dizinindeki man’ın altındaki paket3-package.Rd dosyasını sil ve install("paket3") komutunu yeniden çalıştır. Bu sefer hata almazsın.

library(paket3)

islevv1(20)