R - Создать путь к файлу относительно конкретного файла - PullRequest
0 голосов
/ 30 января 2019

Допустим, у меня есть два файла в разных каталогах:

  1. Scripts/Script.R.
  2. Templates/Template.docx

Если оба пути относительнок тому же каталогу путь второго файла относительно первого - «../Templates/Template.docx».

Используя R, как я могу автоматически создать относительные пути к файлам, как это (то есть, используя функцию)?

В идеале, я надеюсь, что есть функция, которая выглядит примерно так:

> rel_path(path = 'Templates/Template.docx', 
           rel_to = 'Scripts/Script.R')

[1] "../Templates/Template.docx"

1 Ответ

0 голосов
/ 31 января 2019

Вот функция, которую я написал, которая делает это.Вам необходимо указать каталог (main_dir), содержащий оба файла.

rel_path <- function(target_file, ref_file, main_dir){
 ## Returns the path of a file relative to a given path within a given directory.
  # Args:
  #   target_file: name of the file for which the relative path is to be returned.
  #   ref_file: name of the reference file.
  #   main_dir: path of the directory that encompases both the target and the reference files.
  #
  # Returns:
  #   String with the relative file path. 
  #

  target_path <- list.files(path = main_dir,
                            pattern = target_file,
                            recursive = TRUE)

  ref_path <- list.files(path = main_dir,
                         pattern = ref_file,
                         recursive = TRUE)


  ## Split paths into strings to check if they have common sub directories
  ref_str <- (strsplit(ref_path,"/|\\\\")[[1]])
  tar_str <- (strsplit(target_path,"/|\\\\")[[1]])

  ## compare 
  max_len <- min(length(tar_str), length(ref_str))
  matched <- which(ref_str[1:max_len] == tar_str[1:max_len])

  if (length(matched)==0){
    matched = 0
  }

  if (length(ref_str) == 1){ ## The reference file is not inside a sub directory
    rel_path = file.path(target_path)
  }else if (length(matched) == length(tar_str) && length(tar_str) == length(ref_str) ){
    rel_path = file.path(target_file)
  }else if (max(matched) == 1){ ## Both files are under same sub directory 
    rel_path = file.path(target_path)
  }else if (sum(matched) == 0){
    count_up <- paste0(rep("..", length(ref_str)-1), collapse = "/")

    rel_path = file.path(count_up, target_path)
  }else{ ## files under different sub directory
    count_up <- paste0(rep("..", max(matched)-1), collapse = "/") 
    rel_path = paste0(c(count_up, 
                        paste0(tar_str[3:length(tar_str)], collapse = "/")),
                      collapse = "/")
  }

  return(rel_path)
}

Это должно сработать при условии, что оба находятся в каталоге folder1.

> rel_path(target_file= 'Template.docx', 
           ref_file = 'Script.R', main_dir = 'folder1')

[1] "../Templates/Template.docx"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...