I run a monthly code in R where the code uses the name of the month to name files, objects and dataframes. Instead of updating the name of the month in every run in multiple lines of code, I am looking to automate the process.
For example, I would like to define at the beginning of the code that current is Dec and then for R to be able to substitute current as Dec everywhere in the code.
- I have been able to do this with sprintf for filepaths. Example below where the file is saved as Dec_attendance.xls
saveWorkbook(wb, (file.path(output,sprintf("%s_attendance.xlsx", current))), overwrite = TRUE)
- However when I try this with dataframes it does not work. Based on my understanding it is because when I use sprintf the string that is created cannot be assigned anything. The error I get is "target of assignment expands to non-language object"
sprintf("%s_att",current)=read.delim(file.path(input,"Attendance_Extracts/AllSchools_Report_200110.txt"),header=FALSE, sep = "^")
Any ideas of how this could be solved?