If I understand your question correct you want to use the name of an object as a part of a file name. You can use deparse(substitute(obj)):
ftest <- function(df) {
paste0(deparse(substitute(df)), ".tif")
}
ftest(iris)
# Output:
# [1] "iris.tif"
See:
How to convert variable (object) name into String
If you want to use a list of strings as file names:
ftest2 <- function(lst) {
for (i in 1:length(lst)) {
filename <- lst[[i]]
filename <- paste0(filename, ".tif")
print(filename)
}
}
rasnames = list("Wheat","Sbarley","Potato","OSR","fMaize")
ftest2(rasnames)
# Output:
# [1] "Wheat.tif"
# [1] "Sbarley.tif"
# [1] "Potato.tif"
# [1] "OSR.tif"
# [1] "fMaize.tif"
Here is a alternative version without using deparse(substitute()). This code read files from a directory and saves them with the prefix "df_" in a directory called "mynewfiles".
# create some text files in your working directory using the the "iris" data
write.table(iris, file = "test1.txt", sep = "\t")
write.table(iris, file = "test2.txt", sep = "\t")
# get the file names
myfiles <- dir(pattern = "txt")
# create a directory called "mynewfiles" in your working directory
# if it doesn't exists
if (!file.exists("mynewfiles")) {
dir.create("mynewfiles")
}
for (i in 1:length(myfiles)) {
dftmp <- read.table(myfiles[i], header = TRUE, sep = "\t")
# insert code to do something with the data frame...
filename <- paste0("df_", myfiles[i])
print(filename)
write.table(dftmp, file = file.path("mynewfiles", filename), sep = "\t")
}