【发布时间】: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))
}
我的下一个功能是将headValue 和tailValue 打印到文本文件中。为此,我使用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,然后再继续进行。我想您会发现使用
roxygen和devtools来简化pkg 开发会很多轻松。 -
@hrbrmstr 感谢您的建议。我订购了这本书。 :)
-
@Roland 我在问题中添加了命名空间。看起来它只考虑了第一个功能。我该怎么办?
-
@Roland,我在问题中添加了更多信息。请看一看。
标签: r function csv text r-package