Home > database >  Function argument within a string
Function argument within a string

Time:10-19

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

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

Best, Rikki

CodePudding user response:

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

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