Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a function that attaches the input to the end of an existing string in the function?

Let’s say I have this function:

f <- function(input){
  name <- "hello_"
  }

I want to be able to use

f(world)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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").

>Solution :

Use substitute like this:

f <- function(input) paste0("hello_", substitute(input))
f(world)
## [1] "hello_world"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading