假设我有file_a.R
。它通过R的基本源函数来源于其他文件file_b.R
,file_c.R
,它们位于同一文件夹或子文件夹中。是否有一种简单的方法可以在file_b.R
的路径上获得file_c.R
和file_a.R
的路径?
答案 0 :(得分:0)
编辑:
如果你想获得R文件和这些文件中的一些文件之间的所有链接,你可以使用类似的东西:
'SELECT * FROM tracks WHERE level = :level AND id IN
(SELECT max(id) FROM tracks GROUP BY artist)
ORDER BY RAND() LIMIT 4'
它将返回以下列表:
library(stringr)
#Get all R files paths in working directory and subdirectories
filelist <- lapply(list.files(
pattern = "[.]R$", recursive = TRUE
), print)
#Extract one file's sources
getSources <- function(file, pattern) {
#Store all file lines in a character vector
lines <- readLines(file, warn = FALSE)
#Extract R-filenames starting with "pattern" in all lines containing "source"
sources <- lapply(lines, function(x) {
if (length(grep("source", x) > 0)) {
str_extract(x, paste0(pattern, ".*[.]R"))
}
else{
NA
}
})
#Remove NA (lines without source)
sources <- sources[!is.na(sources)]
#Return a list
list(path = file,
pattern = pattern,
sources = unlist(sources))
}
#Example
corresp <- lapply(X = filelist, FUN = getSources, pattern = "file")
您将能够查看是否有任何来源,包括file_A。