0% found this document useful (0 votes)
172 views9 pages

Creating R Packages with Rcmd.exe

This document provides step-by-step instructions for creating an R package. It begins by setting the working directory and loading necessary libraries. The user then creates a package called "Rpackg" with default values and copies code into the package folder. Roxygen2 is used to automatically generate documentation. The package is installed locally and tested before creating a binary distribution.

Uploaded by

ScarfaceXXX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
172 views9 pages

Creating R Packages with Rcmd.exe

This document provides step-by-step instructions for creating an R package. It begins by setting the working directory and loading necessary libraries. The user then creates a package called "Rpackg" with default values and copies code into the package folder. Roxygen2 is used to automatically generate documentation. The package is installed locally and tested before creating a binary distribution.

Uploaded by

ScarfaceXXX
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Package creation in R

Set path to directory where package creation is to take place:

> getwd()
[1] "C:/Users/<User Name>/Documents"
> setwd("C:/RPackage/packg")

I have also put the .R file with the test function into the same directory for my convenience.

The listing of the packgfn is as follows:

library(MASS);
packgfn<-function(x,y){
c(x+1,y+1,1);
#return(c);
}
package devtools is installed and loaded:
> install.packages("devtools")
> library("devtools")

Install and load library roxygen


> devtools::install_github("klutometis/roxygen")
> library(roxygen2)

Create the package


> create("Rpackg")
Creating package Rpackg in .
No DESCRIPTION found. Creating with values:
Package: Rpackg
Title: What the package does (one line)
Version: 0.1
Authors@R: "First Last <[email protected]> [aut, cre]"
Description: What the package does (one paragraph)
Depends: R (>= 3.1.1)
License: What license is it under?
LazyData: true
Adding RStudio project file to Rpackg

Rpackg is the name of the package I want to create

The R folder is empty at this stage.


The description file should ideally be updated to include the creators name and contact information.
However, I have skipped this step.
The file packgfn.R containing the code for the function to be implemented is copied and pasted
into the folder R

The working directory is set to the package directory:


> setwd("C:/RPackage/packg/Rpackg")
> document()
Updating Rpackg documentation
Loading Rpackg
First time using roxygen2 4.0. Upgrading automatically...

The package roxygen2 makes everything amazing and simple. The way it works is that you add
special comments to the beginning of each function, that will later be compiled into the correct
format for package documentation. The details can be found in the roxygen2 documentation
This automatically adds in the .Rd files to the man directory, and adds a NAMESPACE file to the
main directory. You can read up more about these, but in terms of steps you need to take, you really
dont have to do anything further.

Install the package:


> setwd("C:/RPackage/packg/")
> install("Rpackg")
Installing Rpackg
"C:/Users/wasim.ali/DOCUME~1/R/R-31~1.1/bin/x64/R" --vanilla CMD INSTALL
\
"C:\RPackage\packg\Rpackg" \
--library="C:/Users/wasim.ali/Documents/R/R-3.1.1/library" --installtests
* installing *source* package 'Rpackg' ...
** help
No man pages found in package 'Rpackg'
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
*** arch - x64
* DONE (Rpackg)
Reloading installed Rpackg

We need to download and install rtools to prepare the binary distribution of the package.
Rtools can be found at the following link:
http://cran.r-project.org/bin/windows/Rtools/

after installing the path needs to be set for rtools:


a. Raise privileged evaluation status.
b. Open my computer properties

Select advanced system settings:

Select environment variables in advanced tab:


Update the PATH variables:
C:\Rtools\bin;C:\Rtools\gcc-4.6.3\bin;C:\Rtools\gcc4.6.3\bin32;C:\Rtools\gcc-4.6.3\bin64;
Apply and exit
Load the Rpackg.Rproj that was created in the step
> create("Rpackg")

Goto build tab and click build and reload

We get the following on console:


Restarting R session...
> library(Rpackg)

Run Roxygenize:

Then build source and binary:


==> Rcmd.exe build Rpackg
* checking for file 'Rpackg/DESCRIPTION' ... OK
* preparing 'Rpackg':
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
Removed empty directory 'Rpackg/R'
Removed empty directory 'Rpackg/man'
* building 'Rpackg_0.1.tar.gz'
Source package written to C:/RPackage/packg

Rpackg_0.1.tar.gz is the built source


==> Rcmd.exe INSTALL --build --preclean Rpackg
* installing to library 'C:/Users/wasim.ali/Documents/R/R-3.1.1/library'
* installing *source* package 'Rpackg' ...
No man pages found in package 'Rpackg'
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded

*** arch - i386


*** arch - x64
* MD5 sums
packaged installation of 'Rpackg' as Rpackg_0.1.zip
* DONE (Rpackg)
Binary package written to C:/RPackage/packg

Rpackg_0.1.zip is the binary built


Close the session created by opening the >Rproj file
In the old session test the function:
> library("Rpackg")
> packgfn(1,1)
[1] 2 2 1

I can now try installing the package using the binary .zip distribution but the package has to be first
uninstalled as it is currently installed and in use.
Uninstalling:
> remove.packages("Rpackg")
Removing package from C:/Users/wasim.ali/Documents/R/R-3.1.1/library
(as lib is unspecified)

The package can be installed from the binary the following way:

Select the zip file:

> install.packages("C:/RPackage/packg/Rpackg_0.1.zip", repos = NULL)


Warning in install.packages :
package C:/RPackage/packg/Rpackg_0.1.zip is not available (for R
version 3.1.1)
Warning in install.packages :
package Rpackg is in use and will not be installed

test the function after reinstallation using the zip file:


> packgfn(1,1)
[1] 2 2 1

You might also like