Let's say I have this function:
f <- function(input){
name <- "hello_"
}
I want to be able to use
f(world)
and have name
be renamed to "hello_world"
.
Important is that I don't want the input to be a string. So the solution should not involve f("world")
.
CodePudding user response:
Use substitute
like this:
f <- function(input) paste0("hello_", substitute(input))
f(world)
## [1] "hello_world"
Note that designing your functions to use nonstandard evaluation (NSE) in this way will make them less flexible for programming. For example, suppose we store "world"
in x
. Then we get the following which is clearly not what is wanted.
x <- "world"
f(x)
## [1] "hello_x"
It is possible to get around it but it is painful:
do.call("f", list(x))
## [1] "hello_world"