在R上制作玩具包,无法使用R文件中的其他功能
Making a toy package on R, unable to use the other functions in the R file
我正在尝试创建我自己的 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 文件中(code.R 用于 ExercisePkg()
,code1.R 用于 ExercisePkgTxt()
)并重建包。但是问题并没有消失。
当我尝试时 运行 document()
我得到以下信息:
>document()
Updating ToyPackage documentation
Loading ToyPackage
Writing NAMESPACE
Writing ExercisePkg.Rd
>
命名空间文件如下所示:
# 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()
在 building/installing 包之前,以确保更新 NAMESPACE 文件。
我正在尝试创建我自己的 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 文件中(code.R 用于 ExercisePkg()
,code1.R 用于 ExercisePkgTxt()
)并重建包。但是问题并没有消失。
当我尝试时 运行 document()
我得到以下信息:
>document()
Updating ToyPackage documentation
Loading ToyPackage
Writing NAMESPACE
Writing ExercisePkg.Rd
>
命名空间文件如下所示:
# 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()
在 building/installing 包之前,以确保更新 NAMESPACE 文件。