简体   繁体   中英

Making a toy package on R, unable to use the other functions in the R file

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM