The line b <- paste0(x,".xlsx")
is wrong. That calls paste0
on the object x
itself which is not at all what you want to do. You want to call it on the name of the object.
This in general opens a can of worms because objects can have two different names in two different places. Consider: the object named bx2
in your global environment is now named x
within the function's scope. If you only want to call this function from the top level (e.g. for interactive use), you can safely get the name of the object from the parent environment (the environment you called the function from) by replacing that line with:
x_name <- deparse(substitute(x))
b <- paste0(x_name, ".xlsx")
The substitute
function gets the name of x
in the parent environment, as a special name
object. The deparse
function converts this name
into a character
vector of length one.
The reason I said this is only safe to use at the top level is that substitute
can return surprising or unintended results if not used carefully. Hadley Wickham goes into detail on this point in his book.