[英]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 . 我正在尝试创建自己的R包作为练习 。 I have followed the online tutorials by Hillary Parker and have managed to get something going. 我关注了希拉里·帕克 ( Hillaryary Parker)的在线教程,并设法有所发展。
The package I am trying to make takes a csv file, and prints the head() and tail() of the dataset. 我要制作的程序包使用一个csv文件,并打印数据集的head()和tail()。 And then I wrote another function that would print the head() and tail() value in a text file. 然后,我编写了另一个函数,该函数将在文本文件中打印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))
}
My next function is to print the headValue
and the tailValue
into a text file. 我的下一个功能是将headValue
和tailValue
打印到文本文件中。 For that I use sink()
and modify the ExercisePkg
as follows: 为此,我使用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)
}
I have both this functions in the code.R file as: 我在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)
}
It is kept inside the /path/ToyPackage/R/code.R
. 它保存在/path/ToyPackage/R/code.R
。
After I installed the package. 安装完软件包后。 I tried testing it. 我尝试测试。
The ExercisePkg("/path/dataset.csv")
worked like a charm. 该ExercisePkg("/path/dataset.csv")
就像一个魅力。
But the ExercisePkgTxt("/path/dataset.csv")
gave an error like Error: could not find function "ExercisePkgTxt"
但是ExercisePkgTxt("/path/dataset.csv")
给出了类似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. 我尝试将这两个函数放在单独的R文件中( ExercisePkg()
code.R和ExercisePkgTxt()
code1.R)并重建包。 But the problem did not go away. 但是问题并没有消失。
When I try running document()
I get the following: 当我尝试运行document()
,得到以下信息:
>document()
Updating ToyPackage documentation
Loading ToyPackage
Writing NAMESPACE
Writing ExercisePkg.Rd
>
The NAMESPACE file looks like this: NAMESPACE文件如下所示:
# Generated by roxygen2: do not edit by hand
export("ExercisePkg(),ExercisePkgTxt()")
And when I try to install the package by install("ToyPackage"). 当我尝试通过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: 由于您正在使用Roxygen和devtools,因此需要执行以下操作:
#' @export
directive for every function you want to export from your package 对于要从包中导出的每个函数,请包含#' @export
指令 document()
before building/installing the package, to ensure the NAMESPACE file is updated. 在构建/安装软件包之前,请运行document()
,以确保NAMESPACE文件已更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.