assign
似乎不是这里的正确功能,我认为您需要使用
eval(parse())
在您设置的字符串cmd上。内联注释解释了更多:
# read in all files in folder
files <- list.files(pattern = "*.csv")
# loop through the files
for (i in 1:length(files)){
# save the clean filename as a char var because it will be called again
fnClean = substr(files[i], start = 1, stop = nchar(files[i])-4)
# create a cmd as a string to be parsed and evaluated on-the-fly
# the point here is that you can use the 'fnClean' var in the string
# without knowing what it is - assign is expecting an inline string
# ...not a string saved as a var, so it can't be used in this case
loadFileCMD = paste0(fnClean,' = read.csv(files[i], stringsAsFactors =
FALSE)')
print(loadFileCMD) # check the cmd
eval(parse(text=loadFileCMD))
# create another string command to be evaluated to insert the file name
# to the 'FileFrom' field
addFnCMD = paste0(fnClean,'$FileFrom = files[i]')
print(addFnCMD) # check the cmd
eval(parse(text=addFnCMD))
}