0

I am trying to create my own R-package as an exercise. I have followed the online tutorials by Hillary Parker and have managed to get something going.

The package I am trying to make takes a csv file, and prints the head() and tail() of the dataset. And then I wrote another function that would print the head() and tail() value in a text file.

ExercisePkg <- function(.csv) {

  csv <- read.csv(.csv)

  headValue <- head(csv)
  print("The head of the dataset is:")
  print(headValue)

  tailValue <- tail(csv)
  print("The tail of the dataset is:")
  print(tailValue)

  return(list(headValue, tailValue))
}

My next function is to print the headValue and the tailValue into a text file. For that I use sink() and modify the ExercisePkg as follows:

ExercisePkgTxt <- function(.csv) {

  sink('Report.txt')
  csv <- read.csv(.csv)

  headValue <- head(csv)
  print("The head of the dataset is:")
  print(headValue)

  tailValue <- tail(csv)
  print("The tail of the dataset is:")
  print(tailValue)

  return(list(headValue, tailValue))
  sink('Report.txt', append=TRUE)
}

I have both this functions in the code.R file as:

#' Function to see head and tail of a csv file
#'
#' Function is cool.
#' @param Do you love data? Defaults to TRUE
#' @keywords csv, data, head, tail,text.
#' @export ExercisePkg(),ExercisePkgTxt()
#' @examples no examples
#' ExercisePkg()
#' ExercisePKgTxt()

ExercisePkg <- function(.csv) {

      csv <- read.csv(.csv)

      headValue <- head(csv)
      print("The head of the dataset is:")
      print(headValue)

      tailValue <- tail(csv)
      print("The tail of the dataset is:")
      print(tailValue)

      return(list(headValue, tailValue))
    }

    ExercisePkgTxt <- function(.csv) {

      sink('Report.txt')
      csv <- read.csv(.csv)

      headValue <- head(csv)
      print("The head of the dataset is:")
      print(headValue)

      tailValue <- tail(csv)
      print("The tail of the dataset is:")
      print(tailValue)

      return(list(headValue, tailValue))
      sink('Report.txt', append=TRUE)
    }

It is kept inside the /path/ToyPackage/R/code.R.

After I installed the package. I tried testing it.

The ExercisePkg("/path/dataset.csv") worked like a charm.

But the ExercisePkgTxt("/path/dataset.csv") gave an error like Error: could not find function "ExercisePkgTxt"

I tried putting both the functions in separate R file (code.R for ExercisePkg() and code1.R for ExercisePkgTxt()) and rebuild the package. But the problem did not go away.

When I try running document() I get the following:

>document()
Updating ToyPackage documentation
Loading ToyPackage
Writing NAMESPACE
Writing ExercisePkg.Rd
> 

The NAMESPACE file looks like this:

# Generated by roxygen2: do not edit by hand

export("ExercisePkg(),ExercisePkgTxt()")

And when I try to install the package by install("ToyPackage"). I get the following error:

* installing *source* package 'ToyPackage' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
Error in namespaceExport(ns, exports) : 
  undefined exports: ExercisePkg(),ExercisePkgTxt()
Error: loading failed
Execution halted
*** arch - x64
Error in namespaceExport(ns, exports) : 
  undefined exports:  ExercisePkg(),ExercisePkgTxt()
Error: loading failed
Execution halted
ERROR: loading failed for 'i386', 'x64'
* removing 'C:/Users/user/Documents/R/win-library/3.3/ToyPackage'
Error: Command failed (1)

What am I doing wrong?

Please do not give me a whole new code altogether, just suggest some changes, if any.

Thanks.

Pragyaditya Das
  • 1,648
  • 6
  • 25
  • 44
  • 1
    How does your NAMESPACE file look like? Did you load the package after building it? You are aware that code in a function after a call to `return` is never executed? – Roland Sep 29 '16 at 10:43
  • 1
    Creating pkgs is not a trivial task and you're first attempt is very commendable! Definitely poke at what Roland suggested and I'd definitely read (or at least skim through) [R Packages](http://r-pkgs.had.co.nz) by Hadley before proceeding much further. I think you'll find it _much_ easier to use the `roxygen` and `devtools` to streamline pkg development. – hrbrmstr Sep 29 '16 at 10:50
  • @hrbrmstr Thanks for the suggestion. I ordered the book. :) – Pragyaditya Das Sep 29 '16 at 10:56
  • @Roland I added the namespace in the question. It looks like it only takes the first function into account. How do I go about this? – Pragyaditya Das Sep 29 '16 at 10:57
  • @Roland, I added more information to the question. Please have a look. – Pragyaditya Das Sep 29 '16 at 11:31
  • @hrbrmstr, I added more information to the question. Please have a look. – Pragyaditya Das Sep 29 '16 at 11:31

1 Answers1

1

Since you're using Roxygen and devtools, you need to do the following:

  • Include a #' @export directive for every function you want to export from your package
  • Run document() before building/installing the package, to ensure the NAMESPACE file is updated.
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187