Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
指定生成R包的输出目录_R_Package - Fatal编程技术网

指定生成R包的输出目录

指定生成R包的输出目录,r,package,R,Package,我试图自动生成包的过程,但似乎无法告诉R在哪里保存新生成的包 下面是对我的问题的更详细解释: 首先,我编写一个函数(或多个函数),并将其保存为源目录中的一个单独文件(“C:/Users/Raphael/Documents/Stats/R/Package\u Forge/testpack\u SourceFiles”),用于生成包。为了便于说明,我使用了以下测试函数(文件:testpack\u test.R)。正如你所看到的,我使用的是哈德利·维克汉姆的火箭包 #' @rdname f.test

我试图自动生成包的过程,但似乎无法告诉R在哪里保存新生成的包

下面是对我的问题的更详细解释:

首先,我编写一个函数(或多个函数),并将其保存为源目录中的一个单独文件(“C:/Users/Raphael/Documents/Stats/R/Package\u Forge/testpack\u SourceFiles”),用于生成包。为了便于说明,我使用了以下测试函数(文件:testpack\u test.R)。正如你所看到的,我使用的是哈德利·维克汉姆的火箭包

#' @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):

#######################
#***加载包***#
#######################
#设置库路径
.libpath(“C:/Users/Raphael/Documents/Stats/R/Package_Use”)
#安装软件包(“roxygen2”)
图书馆(摘要)
图书馆(roxygen2)
###################
#***设置路径***#
###################
#定义路径
pkForge=“C:/Users/Raphael/Documents/Stats/R/Package\u Forge”
pkUse=“C:/Users/Raphael/Documents/Stats/R/Package\u Use”
newPk=file.path(pkForge,“testpack”)
newPkS=file.path(pkForge,“testpack\u SourceFiles”)
newPkR=file.path(newPk,“R”)#“R”文件夹,其中将包含函数
newPkD=file.path(newPk,“说明”)#说明文件
############################################
#***生成目录并添加文件***#
############################################
#生成新包的主目录
if(file.exists(newPk)){
cat(“\n现有目录已删除!”)
取消链接(newPk,recursive=T)#删除旧目录
cat(“\n已生成新目录!\n”,newPk)
目录创建(newPk)
}否则{
cat(“\n已生成新目录!\n”,newPk)
目录创建(newPk)
}
#生成新包的“R”子目录
创建目录(newPkR)
#将源目录中的所有脚本添加到“R”子目录
#注:函数注释应使用roxygen代码
allScripts=list.files(newPkS,“^testpack.*?\\.R$”,full.names=T,ignore.case=T)#使用正则表达式仅选择某些文件;返回整个路径
file.copy(所有脚本,newPkR)
#在包主目录中生成新的描述文件
fileConn=file(newPkD,open=“w”)
writeLines(c(“包:testpack”),
“类型:包”,
“标题:测试包”,
“版本:1.0”,
“日期:2013-08-04”,
“作者:XYZ”,
“维护者:XYZ”,
“说明:此软件包包含一个测试功能”,
“许可证:GPL-2”),fileConn)
关闭(文件连接)
#file.show(newPkD)#显示新文件的内容
############################
#***氧化包***#
############################
#列表文件(MyPackages)
羟基化(newPk)
#######################
#***构建包***#
#######################
cmd=paste(“R cmd build”,shQuote(newPk),“--无手动--无重新保存数据”,sep=”“)
system(cmd)#使用系统调用构建包
最后一次系统调用正确构建了源程序包。然而,问题是由于某些原因,“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\u Forge/testpack\”--无手册--无重新保存数据),但它总是给出一个错误。有人知道如何在上面的系统调用中指定输出目录吗?我知道devtools包能够做到这一点,但希望能够使用系统调用。非常感谢你的建议

最好的,
拉斐尔你能用水槽吗


sink(x)
会将输出写入您需要的目录和文件格式。

tarball正在保存到工作目录中,因此您可以在系统调用之前
setwd()
,然后将其设置回原位

#######################
#*** 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