我正在尝试自动化包生成的过程,但似乎无法告诉R保存新生成的包的位置。
这里有一个更详细的解释我的问题:
首先我编写一个函数(或多个函数)并将其另存为源目录(“C:/ Users / Raphael / Documents / Stats / R / Package_Forge / testpack_SourceFiles”)中的单独文件,用于生成包裹。为了便于说明,我使用以下测试函数(file:testpack_test.R)。如你所见,我正在使用Hadley Wickham的氧气包装。
#' @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包能够做到这一点,但希望能够使用系统调用。非常感谢任何建议!
最佳, 圣拉斐尔
答案 0 :(得分:2)
tarball正在保存到工作目录中,因此您可以在系统调用之前setwd()
,然后再将其设置回来。
答案 1 :(得分:0)
您可以使用sink
吗?
sink(x)
会将输出写入您需要的目录和文件格式。