我最近用特定的工具制作了我的第一个R包,用于处理我正在处理的一大组数据。在这个项目中,有几个路径和文件,我必须在不同的点调用和访问。
是否可以编写函数,在调用时将一组预定义的路径或数据加载到我的全局环境?
例如,功能
load_foo_paths()
将返回
foo_path_1 <- "path/to/foo/1/"
foo_path_2 <- "path/to/foo/2/"
以及功能
load_foo_data()
将返回
foo_data_1 <- read.csv("foo_data_1.csv")
foo_data_2 <- read.csv("foo_data_2.csv")
我该怎么做这样的事情呢?
感谢
也许您可以根据您的用例调整以下内容:
loadHist <- function() {
HistFile <- c(".Rhistory", ".Rsession")
ruser <- Sys.getenv("R_USER") # C:cygwin64homexxxx
whome <- Sys.getenv("HOME") # C:cygwin64homexxxx
uprofile <- Sys.getenv("USERPROFILE") # C:Usersxxxx
# Setting up History Paths (to .Rhistory)
hP1 <- c(getwd(), ruser, whome, uprofile)
hP2 <- c(file.path(hP1, HistFile[1]))
fe <- file.exists(hP2) # file.exists(file.path(getwd(), HistFile[1]))
# Load first find
fen = length(fe); i=1
while (i <= fen) {
if(fe[i]) {
cat('nLoaded history from:n',hP2[i],'n', sep='')
try(utils::loadhistory(file=hP2[i]))
break
}
i = i + 1
}
#cat('nDone!n')
}