Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R variable inside a string

I would like to know if it is possible to pass a variable inside a string with R like in php. For example:

args <- commandArgs(TRUE)
variable1 <- args[1]

variable2 <- "I am argument:",variable1,"text continue"
like image 447
user979974 Avatar asked Aug 07 '26 00:08

user979974


2 Answers

variable2 <- paste0("I am argument:",variable1,"text continue")
like image 150
user2974951 Avatar answered Aug 09 '26 17:08

user2974951


There are variety of ways to get the output :

variable1 <- args[1]

1) Using sprintf as mentioned by @Roland

sprintf("I am argument: %s text continue",variable1) 

2) Using glue

glue::glue("I am argument: {variable1} text continue")

3) a) str_c

stringr::str_c("I am argument:",variable1,"text continue")

b) stri_c

stringi::stri_c("I am argument:",variable1,"text continue")
like image 40
Ronak Shah Avatar answered Aug 09 '26 17:08

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!