Package Development in R

IN DOS MODE

  • Go to any directory in DOS C:\Users\demo
  • Start R
  • Create a function in R, lets say hw
  • Type in package.skeleton(“hw”,”hw_1”) , the first is the function name and the second is the package name
  • Quit R
  • Get in to DOS mode Remove all the rd files.
  • Type in R CMD INSTALL hw_1
  • Run R
  • And you will have the package hw_1
  • R CMD build hw_1 will build a tar file

Very important – Change the windows path variable in such a way that bin from R , bin from R tools, bin from miktex comes first.

C:\Cauldron\garage\R\R-2.13.1\bin;C:\Rtools\bin;C:\Rtools\MinGW64\bin;C:\Cauldron\garage\miktex\miktex\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files\Common Files\Microsoft Shared\Windows Live;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Windows\SysWOW64;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem C:\Cauldron\garage\ggobi;C:\GTK\bin;C:\Cauldron\garage\asciidoc;C:\Cauldron\garage\blogpost;C:\Python27;C:\Ruby192\bin;C:\Program Files (x86)\Windows Live\Shared;C:\Ruby192\bin

In StatET MODE

  • Create any directory hivin

  • Create a DESCRIPTION file

  • Create an R directory

  • Create an R file inside

  • Create R CMD Build tool in external tool configurations

  • Open the External Tools dialog, and click the `New launch configuration' button.

  • Enter a name for this tool, say `Build R Package'.

  • Select the command `Add-on Packages: Build'.

  • The R CMD Build tool can be run with specific options. These options are entered in the `Options/Argument:' field. For example, to build a pre-compiled binary package, enter the option –binary.

  • Specify the package directory. This should be the directory containing the necessary ingredients to build an R package, so for example the `DESCRIPTION' file, the sub directories: `data', `man' and `R'. If these ingredients are imported in an Eclipse project you can set the package directory to ${fproject_loc}.

  • In the `R Config' tab select a working directory. If you fill in for example ${project_loc} then the resulting R package will be created in the directory of the project.

  • -- binary is key to creating a zip file in StatET

How to document

Insert roxygen tags and then run roxygenize(path to the package directory )

Devtools

devtools package is very important as it helps you prepare a package ready for release. Today I developed my first package from scratch. Not copying pasting some existing code, but from my own work. I was writing some functions in EMAP. What are the steps I went through?

  • Open a folder named mytrials
  • Created the objects in the environment like investors data frame , rules data frame, 2 functions to rebalance 3 asset classes and 4 asset classes
  • Run the command package.skeleton() and it then creates the right folders.
  • Created a r file called optimize-package.R and wrote documentation for dataframes, documentation for package
  • Documented functions so that roxygen can be run
  • Then used the following commands to prepare a package

_

# Using devtools package

_

library(devtools)

_
# Any changes in the function is automatically updated_

load_all(“C:/Cauldron/garage/R/shawshank/PackageTest/mytrials/optimize”)

# Any change in the documentation is updated here

document(“optimize”)

_
#check whether the package is ready for installation_

check_()_

#run examples in the package

run_examples()

  • Once everything went fine, went to DOS command, ran the following commands ( all these commands are run in a folder which contains the package root folder)

  • R CMD check optimize

  • R CMD build

  • R CMD install

  • Fired up r terminal and typed

  • library(optimize)

  • could view all the data frames and functions in the library