【问题标题】:Use variable in file path in R在R中的文件路径中使用变量
【发布时间】:2015-06-23 04:34:41
【问题描述】:

在 bash 中我可以做到:

BLA=some/directory 然后

MyFavoriteFile1=/some/path/to/$BLA/myfile1.someextension
MyFavoriteFile2=/some/path/to/$BLA/myfile2.someextension

我想知道 R 中是否存在相同的情况?这样我只需在整个脚本中更改一次BLA

【问题讨论】:

    标签: r bash path


    【解决方案1】:

    使用file.path:

    dir <- file.path("some", "path")
    bla <- file.path("some", "directory")
    files <- c("file1.R", "file2.exe")
    
    file.path(dir, bla, files)
    

    生产:

    [1] "some/path/some/directory/file1.R"   "some/path/some/directory/file2.exe"
    

    您也可以使用paste 来一般地连接字符串,但file.path 确保为您的操作系统使用正确的目录分隔符,等等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      • 2011-05-19
      • 1970-01-01
      • 2023-03-24
      • 2019-03-15
      相关资源
      最近更新 更多