包编译和相对路径 [英] Package compilation and relative path

查看:83
本文介绍了包编译和相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定很困惑.环顾四周,却找不到合适的答案,并感觉到我做错了事.

I must be very confused. Have looked around but cannot find a suitable answer and have a feeling I am doing something wrong.

这是一个极简的示例: 我的函数test从文件夹导入文件,然后对该文件进行后续分析.我在path = "inst/extdata/input_data"

Here is a minimalist example: My function test import a file from a folder and does subsequent analysis on that file. I have dozens of compressed files in the folder specified by path = "inst/extdata/input_data"

test = structure(function(path,letter) {
  file = paste0(path, "/file_",letter,".tsv.gz")
  data = read.csv(file,sep="\t",header=F,quote="\"",stringsAsFactors=F)

  return(mean(data$var1))

}, ex = function(){
  path = "inst/extdata/input_data"
  m1 = test(path,"A")
})

我正在使用包目录的文件夹R/中的功能构建一个包.

I am building a package with the function in the folder R/ of the package directory.

当我将工作目录设置为包父目录并逐行运行示例时,一切正常.但是,当我使用R CMD check检查该软件包时,它会为我提供以下信息:

When I set the working directory to the package parent and run the example line by line, everything goes fine. However when I check the package with R CMD check it gives me the following:

cannot open file 'inst/extdata/input_data/file_A.tsv.gz': No such file or directory
Error in file(file, "rt") : cannot open the connection

我认为在检查和构建程序包时,工作目录会自动设置为程序包的父目录(在我的情况下是"C:/Users/yuhu/R/Projects/ABCDpackage",但事实并非如此.

I thought in checking and building the package the working directory is automatically set to the parent directory of the package (that in my case is "C:/Users/yuhu/R/Projects/ABCDpackage" but it seems not to be the case.

在这种情况下,最佳做法是什么?由于文件太多,我将避免将所有数据转换为.rda格式,并将其放置在data文件夹中.有没有一种方法可以编译包并在函数示例中设置包所在的相对工作目录?分发软件包时(这也不应是我自己的路径),这也将很有帮助

What is the best practice in this case? I would avoid converting all data in .rda format and put it in the data folder as there are too many files. Is there a way to compile the package and set in the function example the relative working directory where the package is located? This would be helpful also when the package is distributed (therefore it should not be my own path)

非常感谢您的帮助.

推荐答案

当R CMD检查(或稍后的用户)运行示例时,您需要提供文件的完整路径!您可以使用system.filepath.package命令轻松构建该路径. 如果您的程序包名为foo,则应使用以下方法:

When R CMD check (or the user later for that matter) runs the example, you need to provide the full path to the file! You can build that path easily with the system.file or the path.package command. If your package is called foo, the following should do the trick:

    }, ex = function(){
  path = paste0(system.file(package = "foo"), "/extdata/input_data")
  m1 = test(path,"A")
  })

您可能想在不依赖操作系统的位置添加file.path命令.

You might want to add a file.path command somewhere to be OS independent.

由于read.csv只是read.table的包装器,所以我不希望有任何根本的区别.读取压缩文件.

Since read.csv is just a wrapper for read.table I would not expect any fundamental difference w.r.t. to reading compressed files.

注释:R在构建系统目录时会删除目录的"inst/"部分.此线程对inst目录进行了讨论

Comment: R removes the "inst/" part of the directory when it builds the system directory. This thread has a discussion on the inst directory

这篇关于包编译和相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆