This might be something rather obvious for the real cracks here. Nevertheless! I dare asking the question. I was looking for a way to define an argument based on a second argument. I haven't found an answer for R, but this question helped me (I am not a python user, btw): python question
csv <- function(x, name = NULL) { #defines name as NULL
if (is.null(name)) { #tests if name is NULL and when yes,
#uses x for naming of the file
file <- deparse(substitute(x))
name <- paste0(file,'.csv')
write.csv(x, name, row.names = F)
} else { #if name argument is defined ,
#then this will happen
name1 <- paste0(name,'.csv')
write.csv(x, name1, row.names = F)
}
}
This works. My csv-function uses as default the name of the first argument as file name.
I can now call csv() with
csv(my_df)
or
csv(my_df, 'creative_name')
I wondered, however, if there is a more elegant way than this conditional statement to do this kind of stuff. Cheers.