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

Function argument within a string

I am attempting to create a function, which would make me easily plot data by a given year from dataframe with data for many years. I have succeeded in doing this. However, I also want the given year to be shown in the title, but can’t figure out how to put an argument into the title string.

For simplicity, I will just show a mock function:

my_plot <- function(x){
v1 <- x:10
return(plot(v1, main = "The year of ...y..."))
}
my_plot(x=4)

enter image description here

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

How do I replace …y… with an function argument?

Best,
Rikki

>Solution :

You can use paste() to combine two differents strings:

my_plot <- function(x, year){
v1 <- x:10
return(plot(v1, main = paste("The year of", year , sep=" , "))
}
my_plot(x=4)
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