繁体   English   中英

在R上制作玩具包装,无法使用R文件中的其他功能

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

我正在尝试创建自己的R包作为练习 我关注了希拉里·帕克Hillaryary Parker)的在线教程,并设法有所发展。

我要制作的程序包使用一个csv文件,并打印数据集的head()和tail()。 然后,我编写了另一个函数,该函数将在文本文件中打印head()和tail()值。

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))
}

我的下一个功能是将headValuetailValue打印到文本文件中。 为此,我使用sink()并按如下所示修改ExercisePkg

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)
}

我在code.R文件中具有以下两个功能:

#' 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)
    }

它保存在/path/ToyPackage/R/code.R

安装完软件包后。 我尝试测试。

ExercisePkg("/path/dataset.csv")就像一个魅力。

但是ExercisePkgTxt("/path/dataset.csv")给出了类似Error: could not find function "ExercisePkgTxt"

我尝试将这两个函数放在单独的R文件中( ExercisePkg() code.R和ExercisePkgTxt() code1.R)并重建包。 但是问题并没有消失。

当我尝试运行document() ,得到以下信息:

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

NAMESPACE文件如下所示:

# Generated by roxygen2: do not edit by hand

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

当我尝试通过install(“ ToyPackage”)安装软件包时。 我收到以下错误:

* 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)

我究竟做错了什么?

请不要完全给我一个全新的代码,只是提出一些更改(如有)。

谢谢。

由于您正在使用Roxygen和devtools,因此需要执行以下操作:

  • 对于要从​​包中导出的每个函数,请包含#' @export指令
  • 在构建/安装软件包之前,请运行document() ,以确保NAMESPACE文件已更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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