1

In PHP I can use the dot to paste strings:

$path = "/path/to/directory/";
$extension =".txt";
$filecounter = $i; // from some loop for creating multiple files
$file = $path . "filename_". $filecounter . $extension;

How can I do something similar in R?

path <- "/path/to/directory/"
extension <- ".txt"
filecounter <- i
file <- paste(path, paste(paste("filename", $filecounter, sep =""), extension, sep =""), sep ="")

seems like a lot of extra typing for such a simple task.

4

5 回答 5

6

您还可以使用file.pathwhich 以独立于平台的方式从组件构造文件的路径。

path = "/path/to/directory/"
file = "file.txt"

file.path(path,file)
于 2013-05-28T09:14:50.080 回答
4

您应该真正使用file.pathandpaste因为它快速且独立于平台。(另请参阅tools::file_ext, tools::file_path_sans_ext),但下面是一个用于 Linux 或 Mac 系统的小功能,可能会派上用场。我不经常使用它,因为如果我共享我的代码,我要么必须给用户这个功能或包含它的个人包,所以我通常最终编辑要使用的代码,file.path结果证明工作量更少只是不使用此功能开始。

> fpath(path, "filename", extension)
[1] "/path/to/directory/filename.txt"

#' create a filepath
#'
#' This just pastes together \code{dir} and \code{file} and optionally 
#' \code{ext}.  It is intended for Mac or Linux systems. 
#' @param dir character string of directory (with or without trailing slash)
#' @param file character string of filename (with or without extension)
#' @param ext Optional. character file extension. (with or without leading dot)
#' @return character string representing a filepath
#' @examples
#' fpath("path/to", "file", "csv")
#' fpath("path/to/", "file.csv", ".csv") 
#' #knows not to duplicatate the "/" or the ".csv"
#' fpath('path/to', 'file.csv')
#' fpath("file", ext="csv") #knows that dir is missing
#' fpath("", "file", "txt") # no leading forward slash if dir == ""
#' @export
fpath <- function(dir, file, ext) {
    lchar <- function(x) substr(x, nchar(x), nchar(x)) #last char of string
    fl <- if (missing(file)) {
      dir
    } else {
        if (missing(dir) || dir == "") {
            file
        } else {
            dir <- if (substr(dir, nchar(dir), nchar(dir)) != "/") { 
                paste(dir, "/", sep="") 
            } else dir
            file <- gsub("^/+", "", file) # remove leading forward slashes from file names
            paste(dir, file, sep="")
            #TODO: should figure out how to throw an error (or allow it to work) if called like 
            #      fpath("ftp:/", "/ftp.domain.com") or fpath('http:/', '/www.domain.com')
        }
    }
    if (lchar(fl) == "/") stop("'file' should not end with a forward slash")
    if (!missing(ext)) {
        ext <- if (substr(ext, 1, 1) != ".") {
            paste(".", ext, sep="")
        } else ext
        if (substr(fl, nchar(fl) - nchar(ext) + 1, nchar(fl)) != ext) {
            fl <- paste(fl, ext, sep="")
        }
    }
    fl
}
于 2013-05-28T12:08:05.103 回答
4

在最近的 R 版本中,您可以使用以下命令保存一些输入:

paste0(path, "file.txt")

paste0(...)等价于paste(..., sep=""),并且效率稍高。

于 2013-05-28T09:03:57.277 回答
3

如果您希望事情看起来很像PHP,您可以定义自己的中缀运算符并将其设置为paste()

"%.%" <- paste
"a" %.% "b" %.% "c" %.% "d"
## [1] "a b c d"

当然,paste0如果您想要连接而不是空格分隔的字符串,则可以使用。

于 2013-05-28T12:48:36.653 回答
2

或作为替代paste0

sprintf("%s/%s", path, 'file.txt')

对于字符串中的更多条目,@BenBolker 建议:

sprintf("%s/%s.%s", path, file, extension)
于 2013-05-28T09:04:46.780 回答