Here is how we can create a simple package in R. The first thing to be done is install the libraries devtools and roxygen2.
Then, the package is created – in our case, we are calling it “economics”.
Create Package
#Load devtools and roxygen2 libraries
library("devtools")
library(roxygen2)
#Set directory and create "economics"
setwd("C:/Users/directory")
create("economics")
Upon creating the package, it is then necessary to create the R file with all the necessary functions to store the variables. We create this file in the R folder of the package, which will have been created from the above. Note that this is separate from the script above.
In this file, we are then inserting the functions as follows:
economics.R
laspeyres <-
function(price1period1,price1period2,quantity1period1,quantity1period2,price2period1,quantity2period2,price2period2,quantity2period1) {((quantity1period1*price1period2)+(quantity2period1*price2period2))/((quantity1period1*price1period1)+(quantity2period1*price2period1))}
paasche <- function(price1period1,price1period2,quantity1period1,quantity1period2,price2period1,quantity2period2,price2period2) {((quantity1period2*price1period2)+(quantity2period2*price2period2))/((quantity1period2*price1period1)+(quantity2period2*price2period1))}
fisher <- function(paasche,laspeyres) {sqrt(paasche*laspeyres)}
gdpdeflator <-function(nominalgdp,realgdp) {(nominalgdp/realgdp)}
moneymultiplier <- function(reserveratio) {(1/reserveratio)}
ruleof70 <- function(x) {(70/x)}
moneysupply <- function(velocity,avgprice,quantity) {((avgprice*quantity)/(velocity))}
Edit Description File
Then, the description file can be edited with the author details, and a description. Here’s an example:
Package: economics
Title: A package for calculating macroeconomic ratios.
Version: 0.0.0.9000
Authors@R: person("Michael", "Grogan", email = "[email protected]", role = c("aut", "cre"))
Description: Calculation of simple macroeconomic ratios, such as price indices (Fisher, Laspeyres and Paasche), along with the money multiplier, GDP Deflator, and Rule of 70.
Depends: R (>= 3.3.2)
License: What license is it under?
Encoding: UTF-8
LazyData: true
Installation
Return to the original script, and install as follows:
#Install "economics"
setwd("C:/Users/directory")
install("economics")
library(economics)
Now, you will find that you can run the calculations within R directly. For example:
Run calculations
> fisher(paasche,laspeyres) [1] 1.251714 > moneymultiplier(0.7) [1] 1.428571 > ruleof70(1.5) [1] 46.66667
Also, when the library is loaded we can see that the description file appears by selecting Packages -> economics, and the description itself appears under "Help".
The scripts can be found at my GitHub portfolio. Many thanks for your time!

