I'm new to R and I'm just wondering if you can call function variable within a string of text so for example instead of this:
welcome <- function(name,age,location) {paste("Welcome! Your name is", name,"you are",age,"years old and live in", location)}
welcome("Emma","26","UK")
Something like this:
welcome <- function(name,age,location) {paste("Welcome! Your name is **name:** you are **age:** years old and live in **location:**")}
welcome(Emma,26,UK)
In general it is not idiomatic to program in this way in R. There are exceptions - you may wish to read the Non-standard evaluation chapter of Advanced R.
If you do need to do this, there are two ways that I'm familiar with: deparse(substitute()) and match.call().
Firstly, deparse(substitute()):
welcome2 <- function(name, age, location) {
paste(
"Welcome! Your name is",
deparse(substitute(name)),
"you are",
age, # No need to deparse an integer
"years old and live in",
deparse(substitute(location))
)
}
welcome2(Emma, 26, UK)
# [1] "Welcome! Your name is Emma you are 26 years old and live in UK"
However, this can behave surprisingly.
Alternatively, you can use match.call(), which is much cleaner:
welcome3 <- function(name, age, location) {
args <- as.list(match.call())
paste(
"Welcome! Your name is",
args$name,
"you are",
args$age,
"years old and live in",
args$location
)
}
welcome3(Emma, 26, UK)
# [1] "Welcome! Your name is Emma you are 26 years old and live in UK"
One reason not to do this, though, is that these methods do not work if calling from another function. For example:
welcome_wrapper3 <- function(name, age, location) {
welcome3(name, age, location)
}
welcome_wrapper3(Emma, 26, UK)
# [1] "Welcome! Your name is name you are age years old and live in location"
You get the same result with both approaches. Additionally, if you want your function to evaluate your arguments, it will cause problems. Compare:
welcome("Emma", 26-1, "UK")
# [1] "Welcome! Your name is Emma you are 25 years old and live in UK"
welcome3(Emma, 26-1, UK)
# [1] "Welcome! Your name is Emma you are - years old and live in UK" "Welcome! Your name is Emma you are 26 years old and live in UK"
# [3] "Welcome! Your name is Emma you are 1 years old and live in UK"
So generally this is not something you should do unless there's a very good reason.
sprintf()You can also use sprintf() instead of paste(), as suggested in the comments by Roland, which arguably can make the code a little cleaner. You need to use match.call()[-1] to remove the first argument, which is the name of the function, i.e. welcome4 in this case. You also need to use as.character() as as.list(match.call()) returns a list of symbols, which you cannot supply directly to sprintf().
welcome4 <- function(name, age, location) {
do.call(sprintf, c(
"Welcome! Your name is %s you are %s years old and live in %s",
lapply(as.list(match.call())[-1], as.character)
))
}
welcome4(Emma, 26, UK)
# [1] "Welcome! Your name is Emma you are 26 years old and live in UK"
Another option, using glue and substitute():
welcome <- function(name,age,location) glue::glue("Welcome! Your name is {substitute(name)} you are {age} years old and live in {substitute(location)}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With