我正在尝试自动化包生成过程,但似乎无法告诉 R 在哪里保存新生成的包。
这里更详细地解释了我的问题:
首先,我编写一个函数(或多个函数)并将其作为单独的文件保存在源目录(“C:/Users/Raphael/Documents/Stats/R/Package_Forge/testpack_SourceFiles”)中,用于生成包。出于说明目的,我使用以下测试函数(文件:testpack_test.R)。如您所见,我正在使用 Hadley Wickham 的 roxygen 包。
#' @rdname f.test
#' @title Test function
#' @description This function squares a given number.
#' @param x Number
#' @return The function returns a number
#' @export
#'
f.test=function(x){
x=x^2
return(x)
}
然后我使用下面的脚本来生成包,在这个例子中它只包含一个函数(f.test):
#######################
#*** Load packages ***#
#######################
# Set library path
.libPaths("C:/Users/Raphael/Documents/Stats/R/Package_Use")
#install.packages("roxygen2")
library(digest)
library(roxygen2)
###################
#*** Set paths ***#
###################
# Define Path
pkForge="C:/Users/Raphael/Documents/Stats/R/Package_Forge"
pkUse="C:/Users/Raphael/Documents/Stats/R/Package_Use"
newPk=file.path(pkForge,"testpack")
newPkS=file.path(pkForge,"testpack_SourceFiles")
newPkR=file.path(newPk,"R") #"R" folder that will contain functions
newPkD=file.path(newPk,"DESCRIPTION") #Description file
############################################
#*** Generate directories and add files ***#
############################################
# Generate main directory of new package
if(file.exists(newPk)){
cat("\nExisting directory deleted!")
unlink(newPk,recursive=T) #deletes old directory
cat("\nNew directory generated!\n",newPk)
dir.create(newPk)
}else{
cat("\nNew directory generated!\n",newPk)
dir.create(newPk)
}
# Generate "R" sub directory of new package
dir.create(newPkR)
# Add all scripts in the source directory to "R" sub directory
# Note: roxygen code should be used for function annotation
allScripts=list.files(newPkS,"^testpack_.*?\\.R$", full.names=T, ignore.case=T) #uses regex to only select certain files; returns the entire path
file.copy(allScripts, newPkR)
# Generate a new description file in the package main directory
fileConn=file(newPkD,open="w")
writeLines(c("Package: testpack",
"Type: Package",
"Title: Test package",
"Version: 1.0",
"Date: 2013-08-04",
"Author: XYZ",
"Maintainer: XYZ <xyz@gmail.com>",
"Description: This package contains one test function",
"License:GPL-2"),fileConn)
close(fileConn)
# file.show(newPkD) #shows the content of new file
############################
#*** Roxygenize package ***#
############################
# list.files(MyPackages)
roxygenize(newPk)
#######################
#*** Build package ***#
#######################
cmd=paste("R CMD build ", shQuote(newPk)," --no-manual --no-resave-data", sep="")
system(cmd) #using a system call to build the package
最后一个系统调用正确地构建了源包。但是,问题是由于某些原因,“tarball”(testpack_1.0.tar.gz)总是保存到 C:/Users/Raphael/Documents 并且我似乎无法指定输出目录。我想将 tarball 直接保存到 pkUse 目录(“C:/Users/Raphael/Documents/Stats/R/Package_Use”),这是我用于所有已安装库的文件夹。我尝试在“cmd”字符串的各个位置添加 pkUse 目录(“R CMD build \"C:/Users/Raphael/Documents/Stats/R/Package_Forge/testpack\" --no-manual --no- resave-data") 但它总是给出一个错误。有谁知道如何在上述系统调用中指定输出目录?我知道 devtools 包能够做到这一点,但希望能够使用系统调用。
最好的,拉斐尔