【问题标题】:Making a toy package on R, unable to use the other functions in the R file在R上制作玩具包,无法使用R文件中的其他功能
【发布时间】:2017-02-07 15:16:00
【问题描述】:

我正在尝试创建自己的 R 包作为练习。我已按照Hillary 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)

我做错了什么?

请不要完全给我一个全新的代码,如果有的话,只是建议一些更改。

谢谢。

【问题讨论】:

  • 您的 NAMESPACE 文件看起来如何?您是否在构建后加载了包?您知道调用return 后函数中的代码永远不会执行吗?
  • 创建 pkgs 不是一项简单的任务,您的第一次尝试非常值得称赞!绝对戳一下 Roland 的建议,我肯定会阅读(或至少略读)Hadley 的R Packages,然后再继续进行。我想您会发现使用roxygendevtools 来简化pkg 开发会很多轻松。
  • @hrbrmstr 感谢您的建议。我订购了这本书。 :)
  • @Roland 我在问题中添加了命名空间。看起来它只考虑了第一个功能。我该怎么办?
  • @Roland,我在问题中添加了更多信息。请看一看。

标签: r function csv text r-package


【解决方案1】:

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

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

【讨论】:

  • 即使在我运行 document() 之后,NAMESPACE 仍然与我在问题中提供的相同。
  • 您还需要为要导出的所有函数包含#' @export 指令。
  • 点赞#' @export ExercisePkg(), ExercisePkgTxt() ?#' @export ExercisePkg(.csv), ExercisePkgTxt(.csv) ?
  • 我按照您的建议对代码进行了一些更改。请查看问题的修改。
  • 我玩了一些,它奏效了。请将您的评论作为答案的一部分,我会将其标记为已接受。 :)
猜你喜欢
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 2014-04-21
相关资源
最近更新 更多