Steps for writing R packages

Source: Internet
Author: User

Basic steps and considerations for writing R packages [abbreviated version]

Home about publication Guestbook Vitae categories Tags Links Subscribe

This article has expired, please refer to the latest article on the statistical capital: the development of the R Package Ninja Chapter.

These two days are busy writing an R package " animation ", so forced to learn the basic wording of the R package, a cursory look at the "Writing R Extensions" This manual, now provides the approximate steps are as follows:

  1. System readiness: For Linux systems there is little need to be prepared, but Windows users must first install a series of "Windows Toolset", see "R Installation and administration" for details Appendix E; Rtools is a must, but in order to successfully compile Latex documents (Help files and vignette, etc.), the system is best to install MiKTeX. When installing these software, try to follow the default directory installation, otherwise you must understand what "environment variables" mean (mainly the path). After the installation is complete, you can open a command window (start-to-run: cmd ) to test, such as the Linux Command ls window (Linux-like "terminal") will be able to run properly, ls Command a command similar to Windows dir , and for Latex, you can latex -help test whether it works correctly.
  2. If the test commands above do not work properly after installing the software, check that the path of the environment variable contains the appropriate paths (some paths are automatically added after installing Rtools). "My Computer"-"Properties"-"advanced"-"Environment variables"-"System Variables"-->path, which should contain a series of paths such as, c:\Rtools\bin; c:\Rtools\perl\bin; c:\Rtools\MinGW\bin; in order to wait for the R CMD INSTALL command to function, C:\Program Files\R\bin Such a path should also be included here (depending on where R is installed and where I installed it). It's enough to get the basic work done here.
  3. R itself provides a function to build the framework of the package: package.skeleton() This function is very handy for writing a package because it will automatically help you set up the package's shelves, and all you have to do is write the function's source program (. R) and help documentation (. Rd); function usage See my Blog "Write your own package with R", mainly refer to r help, see for yourself. Note that the path parameter, otherwise constructs a framework itself can not find where to go (if you do not specify the path parameter, then the structure of the framework is placed getwd() below). The packaged function puts your functions under the R folder and places the data (such as a frame) in the Files folder.
  4. I'm not going to say any of the functions in the package. After you've written a series of functions (and possibly data), package.skeleton() you can start modifying and supplementing the details of the package under the folder with a good shelf. (1) Description file, which is a basic description of your package, including version, author, copyright, maintainer, related packages, which need to see the document may be only depends, Imports, suggests, enhances and other items, a little bit of the brain ; (2) Help document, this is under the Man folder, with Rd as the extension (R documentation), just the package.skeleton() function has been the basic project has been automatically generated, we need to fill in the corresponding content, such as function description, parameter description, detailed description, return value, example, Keywords (selected in the keywords provided by R) and so on. These texts can be used in links \link{}, font decoration \emph{} \code{}, etc., see the r-exts document in detail.
  5. After you have modified it, you can start compiling it; open a command window cd to the folder where your package source files are located, for example, if your package is written C:\pkg here, then use the cd C:\ R CMD series commands, such as checking the package for errors: R CMD check pkg ; Compiling the package: R CMD build pkg (If you need to compile to binary, add the appropriate option --binary ); Install package: R CMD install pkg If you have any errors in your package, you will be prompted when you check.

Such an R package is basically completed, the build *.tar.gz (or binary form *.zip ) can be handed over to others (provided that check is completely passed). Further you can write vignette for your package, which is a PDF document, similar to the form of an article. If you want to write vignette, it is best to use Sweave, which is described in r-exts, the general purpose is to use Latex documents and R code together, you can only put R code in the latex document, Without having to prepare the code output (text or graphics), the Sweave document will automatically execute your r code when it is compiled and insert it into the latex document, which is very convenient--generate reports dynamically. The vignette document ( *.Rnw ) is placed in the directory of the package inst/doc , and the document is automatically compiled to generate a PDF when the package is built (actually converted to latex), and the document is loaded into the doc directory when the package is installed. The usage of Sweave can also be found on the COS forum: Http://cos.name/cn/topic/8434,momozilla is familiar with Emacs, ESS and Sweave, and can consult with him.

In the process of writing a package, you might want to refer to someone else's source file, to see how it is written.

Precautions:

    1. When writing code, do not put true/false shorthand for t/f, although they are basically equivalent, but this is written in the package will give a warning and error message! It took me a half-day to figure this out.
    2. Note that the latex syntax, when writing Help files, some symbols such as underline _ is not directly used, because Latex syntax requires that such characters must be guided by \. If the RD file does not conform to latex syntax, the build will also get an error. The underscore is mentioned because the package.skeleton() resulting data file has an underscore in the RD file (you do not know if you should write a message to the R core), and these underscores should be deleted.

Steps for writing R packages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.